cmake_minimum_required(VERSION 3.9 FATAL_ERROR) #获得所在文件夹的名字 get_filename_component(FLODER_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) set(PROJECT_NAME "${FLODER_NAME}") #文件夹名作为工程名 project(${PROJECT_NAME} LANGUAGES CXX) IF(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) ENDIF() set(${PROJECT_NAME}_ROOT ${CMAKE_CURRENT_SOURCE_DIR}) set(${PROJECT_NAME}_SRC_DIR ${${PROJECT_NAME}_ROOT}/xxx) set(${PROJECT_NAME}_INC_DIR ${${PROJECT_NAME}_ROOT}/xxx) #设置标志,相当于添加到visual studio的属性->C/C++->命令行->其他选项(D) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj") #添加编预译定义,相当于添加到visual studio的属性->C/C++->预处理器->预处理器定义 add_compile_definitions(NOMINMAX) #设置使用文件夹属性 set_property(GLOBAL PROPERTY USE_FOLDERS ON) #源文件 file(GLOB Srcs xxx/*.cpp ) #头文件 file(GLOB Incs xxx/*.h) # add resource set(RESOURCE ${CMAKE_CURRENT_SOURCE_DIR}/SiltSLAM.qrc) qt5_add_resources(RESOURCE_ADDED ${RESOURCE}) #添加库 add_library(${PROJECT_NAME} ${Srcs} ${Incs} ) target_link_libraries(${PROJECT_NAME} ${qt_link}) #执行文件 add_executable( ${PROJECT_NAME} ${Srcs} ${Incs} ${RESOURCE_ADDED}) target_link_libraries(${PROJECT_NAME} xxx) #将工程放入apps下,在visual studio里可将同类文件放在相同目录下,方便IDE可视化。 set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "apps" ) #将库头文件路径写入CMakeCache.txt,方便其他模块使用 set(${PROJECT_NAME}_INCLUDE_DIRS "${${PROJECT_NAME}_INC_DIR}" CACHE INTERNAL "${PROJECT_NAME} dirs" ) #查找库 find_library(${PROJECT_NAME} NAMES lib${PROJECT_NAME}.a PATHS ${${PROJECT_NAME}_INC_DIR}/xxx) set( LIBS ${PROJECT_NAME} lib2 lib3 ) #将库路径写入CMakeCache.txt,方便其他模块使用 set(${PROJECT_NAME}_LIBRARIES "${LIBS}" CACHE INTERNAL "${PROJECT_NAME} libs" )
或者
set(LIB_DIR xxx)
set(LIB_DIRS "${LIB_DIR}" CACHE INTERNAL "${PROJECT_NAME} dirs" )
link_directories(${LIB_DIRS})
指定安装路径 :cmake .. -DCMAKE_INSTALL_PREFIX=绝对路径