自学内容网 自学内容网

CmakeLists

set() 在CmakeLists中可以设置变量,变量值类型是string

aux_source_directory(${PROJECT_SOURCE_DIR} SRC)

PROJECT_SOURCE_DIR:执行cmake时后面携带的路径 CMakeLists.txt对应的路径

file(GLOB SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

CMAKE_CURRENT_SOURCE_DIR执行cmake时后面携带的路径 CMakeLists.txt对应的路径

include_directories(${PROJECT_SOURCE_DIR}/include) 指定头文件的路径

include_directories(include)

动态库:libxxx.so SHARED 有可执行权限

静态库:libxxx.a STATIC

发布给客户需要给include 和库文件

如果使用的静态库, libcalc.a 可被打包到生成的可执行文件当中,动态库里面的数据不会打包到可执行文件里面,当程序一启动,可执行文件和静态库都会被加载到内存中,但是动态库不会,动态库只是执行到的时候才会被加载到内存中

target_link_libraries(app calc) 写到CmakeLists.txt最后,动态库在物理内存只保存一份

cmake_minimum_required(VERSION 3.15)
project(test)
set(LIBPATH &{PROJECT_SOURCE_DIR}/lib)
set(EXECPARH ${PROJECT_SOURCE_DIR}/bin)
set(HREADPATH ${PROJECT_SOURCE_DIR}/include)
set(CALCLIB calc)
set(SORTLIB sort)
set(APPNAME1 app1)
set(APPNAME2 app2)
#给当前节点添加子目录
add_subdirectory(calc)
add_subdirectory(sort)
add_subdirectory(test1)
add_subdirectory(test2)


cmake_minimum_required(VERSION 3.15)
project(calc)

#搜索源文件
aux_source_directory(./ SRC)
include_directories(${HREADPATH})
set(LIBRARY_OUTPUT_PATH ${LIBPATH})
add_library(${CALCLIB} STATIC ${SRC})


cmake_minimum_required(VERSION 3.15)
project(sort)
#搜索源文件
aux_source_directory(./ SRC)
include_directories(${HREADPATH})
set(LIBRARY_OUTPUT_PATH ${LIBPATH})
add_library(${SORTLIB} STATIC ${SRC})


cmake_minimum_required(VERSION 3.15)
project(test1)
#搜索源文件
aux_source_directory(./ SRC)
include_directories(${HREADPATH})
link_directories(${LIBPATH})
link_libraries(${CALCLIB})
set(EXECUTABLE_OUTPUT_PATH ${EXECPARH})
add_executable(${APPNAME1} ${SRC})


cmake_minimum_required(VERSION 3.15)
project(test1)
#搜索源文件
aux_source_directory(./ SRC)
include_directories(${HREADPATH})
set(SORTLIB sort)
link_directories(${LIBPATH})
link_libraries(${LIBSORT})
set(EXECUTABLE_OUTPUT_PATH ${EXECPARH})
add_executable(${APPNAME2} ${SRC})

CMake 保姆级教程(下) | 爱编程的大丙


原文地址:https://blog.csdn.net/qq_33089547/article/details/140248533

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!