概述
生成sort\calc的静态库,并生成app1.exe app2.exe
目录结构
CMakeLists.txt 位置以及配置
根CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(mulitiple_modules)
set(CMAKE_CXX_STANDARD 17)
# define variables
# LIBPATH 库存储位置
set(LIBPATH ${PROJECT_SOURCE_DIR}/lib)
# EXECPATH 可执行文件位置
set(EXECPATH ${PROJECT_SOURCE_DIR}/bin)
# HEADER 头文件存储位置
set(HEADPATH ${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)
calc目录下CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(sub_modules_calc)
set(CMAKE_CXX_STANDARD 17)
# 搜索源文件
aux_source_directory(./ SRC)
# 搜索头文件
include_directories(${HEADPATH})
# 生成动态库位置
set(LIBRARY_OUTPUT_PATH ${LIBPATH})
# 生成动态库
add_library(${CALCLIB} STATIC ${SRC})
sort目录下CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(sub_modules_sort)
set(CMAKE_CXX_STANDARD 17)
# 搜索源文件
aux_source_directory(./ SRC)
# 搜索头文件
include_directories(${HEADPATH})
# 生成动态库位置
set(LIBRARY_OUTPUT_PATH ${LIBPATH})
# 生成动态库
add_library(${SORTLIB} STATIC ${SRC})
test1目录下CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(sub_modules_test1)
set(CMAKE_CXX_STANDARD 17)
# 搜索源文件
aux_source_directory(./ SRC)
# 搜索头文件
include_directories(${HEADPATH})
# 可执行文件位置
set(EXECUTABLE_OUTPUT_PATH ${EXECPATH})
# 链接静态库
link_directories(${LIBPATH})
link_libraries(${CALCLIB})
add_executable(${APPNAME1} ${SRC})
test2目录下CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(sub_modules_test2)
set(CMAKE_CXX_STANDARD 17)
# 搜索源文件
aux_source_directory(./ SRC)
# 搜索头文件
include_directories(${HEADPATH})
# 可执行文件位置
set(EXECUTABLE_OUTPUT_PATH ${EXECPATH})
# 链接静态库
link_directories(${LIBPATH})
link_libraries(${SORTLIB})
add_executable(${APPNAME2} ${SRC})
在跟目录下新建build
mkdir build
cd build
# 生成makefiel
cmake ..
# 生成静态库,exe
make