cmake多层级目录调用

通过多个cmake层级编译
目录树

➜  test_pro6 tree -h
.
├── [ 351]  CMakeLists.txt
├── [4.0K]  app_hello
│   ├── [  97]  CMakeLists.txt
│   ├── [ 280]  app_hello.c
│   └── [ 218]  app_hello.h
├── [ 276]  app_main.c
└── [4.0K]  build

2 directories, 5 files

顶层cmake

cmake_minimum_required(VERSION 3.16.1)
project(test)

set(PROJECT_NAME test)

set(APP_HELLO_DIR ${PROJECT_SOURCE_DIR}/app_hello)

include_directories(${APP_HELLO_DIR})

add_subdirectory(${APP_HELLO_DIR})

link_directories(${APP_HELLO_DIR})

add_executable(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/app_main.c)
target_link_libraries(${PROJECT_NAME} hello)

子目录cmake

aux_source_directory(. SRC)

message("hello src dir :" ${SRC})

add_library(hello STATIC ${SRC})

测试

➜  build cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.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
hello src dir :./app_hello.c
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zw/test_pro/test_pro6/build
➜  build make clean all
Scanning dependencies of target hello
[ 25%] Building C object app_hello/CMakeFiles/hello.dir/app_hello.c.o
[ 50%] Linking C static library libhello.a
[ 50%] Built target hello
Scanning dependencies of target test
[ 75%] Building C object CMakeFiles/test.dir/app_main.c.o
[100%] Linking C executable test
[100%] Built target test
➜  build ./test 
app_hello in 
posted @ 2023-01-07 15:43  tccxy  阅读(441)  评论(0编辑  收藏  举报