正确设置目标链接:使用target_link_libraries确保所有相关库都被正确链接。 add_library(my_library code.cpp) add_executable(my_app main.cpp) target_link_libraries(my_app my_library) 管理依赖关系:明确项目内部各部分之间的依赖关系,确保链接顺序正确。 3.6.4 最佳实践 清晰的依赖结构:在项目中建立清晰的依赖...
2. 添加需要链接的库文件目录 LINK_DIRECTORIES 它相当于 g++ 命令的 -L 选项的作用,也相当于环境变量中增加 LD_LIBRARY_PATH 的路径的作用 link_directories("/home/server/third/lib") 3. 宏定义 CMakeLists.txt 之 多重判断宏定义 比如设置为 CPU_ONLY add_definitions(-DCPU_ONLY) 它相当于 g++ 命令...
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) if(WIN32) set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) elseif(UNIX) set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) endif() add_subdirectory(messagebridge) option(...
cmake_minimum_required (VERSION 2.6) INCLUDE_DIRECTORIES(../../thirdparty/comm) FIND_LIBRARY(COMM_LIB comm ../../thirdparty/comm/lib NO_DEFAULT_PATH) FIND_LIBRARY(RUNTIME_LIB rt /usr/lib /usr/local/lib NO_DEFAULT_PATH) link_libraries(${COMM_LIB} ${RUNTIME_LIB}) ADD_DEFINITIONS( -...
不管是SUBDIRS还是ADD_SUBDIRECTORY指令(不论是否指定编译输出目录),我们都可以通过SET指令重新定义EXECUTABLE_OUTPUT_PATH和LIBRARY_OUTPUT_PATH变量来指定最终的目标二进制的位置(指最终生成的hello或者最终的共享库,不包括编译生成的中间文件) SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) SET(LIBRARY_OUTPUT...
add_executable(demo main.cpp ) 最后贴一个完整的例子 cmake_minimum_required(VERSION2.6)INCLUDE_DIRECTORIES(../../thirdparty/comm)FIND_LIBRARY(COMM_LIB comm ../../thirdparty/comm/lib NO_DEFAULT_PATH)FIND_LIBRARY(RUNTIME_LIB rt /usr/lib /usr/local/lib NO_DEFAULT_PATH)link_libraries(${COMM...
FIND_LIBRARY(RUNTIME_LIB rt /usr/lib /usr/local/lib NO_DEFAULT_PATH) link_libraries(${COMM_LIB} ${RUNTIME_LIB}) ADD_DEFINITIONS( -O3 -g -W -Wall -Wunused-variable -Wunused-parameter -Wunused-function-Wunused -Wno-deprecated -Woverloaded-virtual -Wwrite-strings ...
add_executable(demo main.cpp) 7.最后贴一个完整的例子 代码语言:javascript 复制 cmake_minimum_required(VERSION2.6)INCLUDE_DIRECTORIES(../../thirdparty/comm)FIND_LIBRARY(COMM_LIBcomm../../thirdparty/comm/libNO_DEFAULT_PATH)FIND_LIBRARY(RUNTIME_LIBrt/usr/lib/usr/local/libNO_DEFAULT_PATH)link_li...
如果要指定libhello.so 生成的位置,可以通过在主工程文件CMakeLists.txt 中修改 ADD_SUBDIRECTORY(lib)指令来指定一个编译输出位置或者在lib/CMakeLists.txt 中添加 SET(LIBRARY_OUTPUT_PATH <路径>)来指定一个新的位置。 四、ADD_LIBRARY指令 语法: ADD_LIBRARY(libname [SHARED|STATIC|MODULE] [EXCLUDE_FROM_AL...
list(APPEND CMAKE_PREFIX_PATH "${Protobuf_PREFIX_PATH}") # 查找 Protobuf find_package(Protobuf REQUIRED) # 包含头文件 include_directories(${Protobuf_INCLUDE_DIR}) add_executable(Demo demo.cpp) # 链接选项 target_link_libraries(Demo ${Protobuf_LIBRARIES} pthread) ...