cmake 多级CMakeLists.txt调用

Cmake 多级CMakeLists.txt 如何调用?

这里做一个demo来演示,我的demo文件结构如下:

mul_cmake_demo
├── CMakeLists.txt
├── common
│   ├── CMakeLists.txt
│   ├── common.c
│   └── common.h
├── inc
│   └── main.h
└── src
    └── main.c

根目录下CMakeLists.txt 编写如下:

# 定义cmake的最低版本
cmake_minimum_required(VERSION 2.8)

# 定义工程名称
project(mul_cmake_demo)

# 打印工程定级目录(非必要)
message("src_dir:" ${PROJECT_SOURCE_DIR})

# 包含子目录头文件
include_directories("${PROJECT_SOURCE_DIR}/common")

# 添加子目录,这样子目录中的CMakeLists.txt才会被调用
add_subdirectory(common)

# 将./src 下的所有文件名保存到DIR_SRCS变量
aux_source_directory(./src DIR_SRCS)

# 编译时打印
message("DIR_SRCS:" ${DIR_SRCS})

# 生成可执行文件
add_executable(mul_cmake_demo ${DIR_SRCS})

# 添加链接静态库
target_link_libraries(mul_cmake_demo CommonFuncs)

子目录下的CMakeLists.txt 编写如下:

# 将本目录下的所有.c 文件添加到DIR_LIB_SRCS变量
aux_source_directory(. DIR_LIB_SRCS)

# 打印信息
message("common_path_cmake:" ${DIR_LIB_SRCS})

# 生成静态库
add_library(CommonFuncs ${DIR_LIB_SRCS})

创建编译文件夹:

mkdir build
cd build

执行编译指令:

runner@LAPTOP-MCEBPVFJ:~/workspace/rd_test/cmake_learn/mul_cmake_demo/build$ cmake ..
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
src_dir:/home/runner/workspace/rd_test/cmake_learn/mul_cmake_demo
common_cmake:
common_path_cmake:./common.c
DIR_SRCS:./src/main.c
-- Configuring done
-- Generating done
-- Build files have been written to: /home/runner/workspace/rd_test/cmake_learn/mul_cmake_demo/build
runner@LAPTOP-MCEBPVFJ:~/workspace/rd_test/cmake_learn/mul_cmake_demo/build$ make
Scanning dependencies of target CommonFuncs
[ 25%] Building C object common/CMakeFiles/CommonFuncs.dir/common.c.o
[ 50%] Linking C static library libCommonFuncs.a
[ 50%] Built target CommonFuncs
Scanning dependencies of target mul_cmake_demo
[ 75%] Building C object CMakeFiles/mul_cmake_demo.dir/src/main.c.o
[100%] Linking C executable mul_cmake_demo
[100%] Built target mul_cmake_demo
runner@LAPTOP-MCEBPVFJ:~/workspace/rd_test/cmake_learn/mul_cmake_demo/build$ 

执行目标文件:

runner@LAPTOP-MCEBPVFJ:~/workspace/rd_test/cmake_learn/mul_cmake_demo/build$ ls
CMakeCache.txt  CMakeFiles  Makefile  cmake_install.cmake  common  mul_cmake_demo
runner@LAPTOP-MCEBPVFJ:~/workspace/rd_test/cmake_learn/mul_cmake_demo/build$ ./mul_cmake_demo 
hello cmake.
my_common_func
runner@LAPTOP-MCEBPVFJ:~/workspace/rd_test/cmake_learn/mul_cmake_demo/build$ 

查看生成的静态库:

.
├── CMakeLists.txt
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.16.3
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   │   ├── CMakeCCompilerId.c
│   │   │   │   ├── a.out
│   │   │   │   └── tmp
│   │   │   └── CompilerIdCXX
│   │   │       ├── CMakeCXXCompilerId.cpp
│   │   │       ├── a.out
│   │   │       └── tmp                                                                    "LAPTOP-MCEBPVFJ" 17:10 24-Jun-21
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── Makefile.cmake
│   │   ├── Makefile2
│   │   ├── TargetDirectories.txt
│   │   ├── cmake.check_cache
│   │   ├── mul_cmake_demo.dir
│   │   │   ├── C.includecache
│   │   │   ├── DependInfo.cmake
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── depend.internal
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   ├── progress.make
│   │   │   └── src
│   │   │       └── main.c.o
│   │   └── progress.marks
│   ├── Makefile
│   ├── cmake_install.cmake
│   ├── common
│   │   ├── CMakeFiles
│   │   │   ├── CMakeDirectoryInformation.cmake
│   │   │   ├── CommonFuncs.dir
│   │   │   │   ├── C.includecache
│   │   │   │   ├── DependInfo.cmake
│   │   │   │   ├── build.make
│   │   │   │   ├── cmake_clean.cmake
│   │   │   │   ├── cmake_clean_target.cmake
│   │   │   │   ├── common.c.o
│   │   │   │   ├── depend.internal
│   │   │   │   ├── depend.make
│   │   │   │   ├── flags.make
│   │   │   │   ├── link.txt
│   │   │   │   └── progress.make
│   │   │   └── progress.marks
│   │   ├── Makefile
│   │   ├── cmake_install.cmake
│   │   └── libCommonFuncs.a                # 静态库
│   └── mul_cmake_demo
├── common
│   ├── CMakeLists.txt
│   ├── common.c
│   └── common.h
├── inc
│   └── main.h
└── src
    └── main.c

你可能感兴趣的:(cmake 多级CMakeLists.txt调用)