PROJECT(common) MESSAGE("Begin to build " ${PROJECT_NAME}) MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR}) MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR}) INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/..) #find_file() 只会找一个文件 #AUX_SOURCE_DIRECTORY(. SRC_LIST) #FOREACH(F ${SRC_LIST}) # MESSAGE(${F}) #ENDFOREACH(F) FILE(GLOB_RECURSE ALL_SRC *.cpp) FOREACH(F ${ALL_SRC}) SET(SRC_LIST ${SRC_LIST} ${F}) MESSAGE("Use " ${F}) ENDFOREACH(F) #SHARED,动态库 #STATIC,静态库 ADD_LIBRARY(common SHARED ${ALL_SRC} ) ADD_LIBRARY(common_static STATIC ${ALL_SRC}) # 没有SET_TARGET_PROPERTIES, 是不能同时build 静态库和动态库, 如果只build 一种库, 就不需要这个功能 SET_TARGET_PROPERTIES(common_static PROPERTIES OUTPUT_NAME "common") GET_TARGET_PROPERTY(OUTPUT_VALUE common_static OUTPUT_NAME) MESSAGE(STATUS "This is the func_static OUTPUT_NAME:" ${OUTPUT_VALUE}) # 编译静态库时,自动会把同名的动态库给删除, 因此需要临时设置一下 SET_TARGET_PROPERTIES(common PROPERTIES CLEAN_DIRECT_OUTPUT 1) SET_TARGET_PROPERTIES(common_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) # 设置版本号 VERSION指代动态库版本,SOVERSION指代API版本 SET(MAIJOR_VER 1) SET(MINOR_VER 0) SET(PATCH_VER 0) SET(OTHER_VER 1) ADD_DEFINITIONS(-DMAIJOR_VER=${MAIJOR_VER} -DMINOR_VER=${MINOR_VER} -DPATCH_VER=${PATCH_VER} -DOTHER_VER=${OTHER_VER}) SET_TARGET_PROPERTIES(common PROPERTIES VERSION ${MAIJOR_VER}.${MINOR_VER}.${PATCH_VER} SOVERSION ${MAIJOR_VER}) SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../lib) # Target 必须在定义 ADD_EXECUTABLE 之后, programs 不受这个限制 # TARGETS和PROGRAMS 的默认权限是OWNER_EXECUTE, GROUP_EXECUTE, 和WORLD_EXECUTE,即755权限, programs 都是处理脚步类 # 类型分为RUNTIME/LIBRARY/ARCHIVE, prog INSTALL(TARGETS common common_static LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) # 小心 安装的时候,不要变成include/func.h, 如果这样,第二次安装的时候会变成include/func.h/func.h # INSTALL(FILES func.h DESTINATION include/) FILE(GLOB_RECURSE ALL_HEADER *.h) FOREACH(F ${ALL_HEADER}) file(RELATIVE_PATH RELAPATH_HEADER ${PROJECT_SOURCE_DIR} ${F}) # 获取相对路径 get_filename_component(headDir ${RELAPATH_HEADER} DIRECTORY) MESSAGE("Install " ${RELAPATH_HEADER} " to " ${CMAKE_INSTALL_PREFIX} "/" ${PROJECT_NAME} "/include/" ${headDir}) INSTALL(FILES ${RELAPATH_HEADER} DESTINATION include/${headDir}) ENDFOREACH(F)