find_program(CMAKE_C_COMPILER NAMES gcc cc) if(NOT CMAKE_C_COMPILER) message(FATAL_ERROR "C compiler not found!") endif() set(OUTPUT_FILE ${CMAKE_BINARY_DIR}/libhello_c.so) add_custom_command( OUTPUT ${OUTPUT_FILE} COMMAND ${CMAKE_C_COMPILER} -shared -fPIC -o ${OUTPUT_FILE} ${CMAKE_CURRENT_SOURCE_DIR}/hello.c DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/hello.c COMMENT "Compiling hello_c shared library..." ) add_custom_target(hello_c ALL DEPENDS ${OUTPUT_FILE})