├── app
│ ├── app1
│ │ ├── app1.c
│ │ ├── app1.h
│ │ └── CMakeLists.txt
│ ├── app2
│ │ ├── app2.c
│ │ ├── app2.h
│ │ └── CMakeLists.txt
│ ├── app3
│ │ ├── app3.c
│ │ ├── app3.h
│ │ └── CMakeLists.txt
│ └── CMakeLists.txt
├── CMakeLists.txt
├── include
│ └── Static.h
├── lib
│ └── libhello_library.a
├── main
│ ├── CMakeLists.txt
│ └── main.c
└── public
├── CMakeLists.txt
├── public1
│ ├── CMakeLists.txt
│ ├── public1.c
│ └── public1.h
└── public2
├── CMakeLists.txt
├── public2.c
└── public2.h
app目录存放应用层代码
public中存放公共代码
main中存放主函数代码
lib中存放三方静态库(本节使用CMake系列(三) CMake编译出静态库和动态库并使用产生的静态库)
include存放三方库需要用到的头文件
├──app1.c
│
├──app2.c
│ └──public2.c
│
├──app3.c
│ └──libstatic_library.a
main.c──│
├──public1.c
│
├──public2.c
│
└──libstatic_library.a
main.c
#include "app1.h"
#include "app2.h"
#include "app3.h"
#include "public1.h"
#include "public2.h"
#include "Static.h"
int main(int argc, char *argv[])
{
printf("main: cmake\r\n");
App1_print();
App2_print();
App3_print();
Public1_print();
Public2_print();
Static_print();
printf("main: end\r\n");
return 0;
}
app1.c
#include "app1.h"
void App1_print(void)
{
printf("app1: cmake\r\n");
printf("app1: end\r\n");
}
app2.c
#include "app2.h"
#include "public2.h"
void App2_print(void)
{
printf("app2: cmake\r\n");
Public2_print();
printf("app2: end\r\n");
}
app3.c
#include "app3.h"
#include "Static.h"
void App3_print(void)
{
printf("app3: cmake\r\n");
Static_print();
printf("app3: end\r\n");
}
public1.c
#include "public1.h"
void Public1_print(void)
{
printf("public1: cmake\r\n");
printf("public1: end\r\n");
}
public2.c
#include "public2.h"
void Public2_print(void)
{
printf("public2: cmake\r\n");
printf("public2: end\r\n");
}
cmake_minimum_required(VERSION 3.5)
project(subprojects)
# Add sub directories
# 定义子目录src,用以递归的调用src中的MakeLists.txt
add_subdirectory(public)
add_subdirectory(app)
add_subdirectory(main)
最后三行,使用add_subdirectory调用子目录的MakeLists.txt
project(main)
# Create the executable
add_executable(${PROJECT_NAME} main.c)
target_include_directories( ${PROJECT_NAME}
PUBLIC ${subprojects_SOURCE_DIR}/include
)
#link_directories( ${subprojects_SOURCE_DIR}/lib )
# Link the static library from subproject1 using it's alias sub::lib1
# Link the header only library from subproject2 using it's alias sub::lib2
# This will cause the include directories for that target to be added to this project
target_link_libraries(${PROJECT_NAME}
lib::app1
lib::app2
lib::app3
lib::public1
lib::public2
${subprojects_SOURCE_DIR}/lib/libstatic_library.a
)
最后一行使用target_link_libraries 连接其他文件。
#定义子目录
add_subdirectory(app1)
add_subdirectory(app2)
add_subdirectory(app3)
)
# Set the project name
project (app1)
# Add a library with the above sources
add_library(${PROJECT_NAME} app1.c)
add_library(lib::app1 ALIAS ${PROJECT_NAME})
target_include_directories( ${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}
)
# Set the project name
project (app2)
# Add a library with the above sources
add_library(${PROJECT_NAME} app2.c)
add_library(lib::app2 ALIAS ${PROJECT_NAME})
target_include_directories( ${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}
)
target_link_libraries(${PROJECT_NAME}
lib::public2
)
# Set the project name
project (app3)
# Add a library with the above sources
add_library(${PROJECT_NAME} app3.c)
add_library(lib::app3 ALIAS ${PROJECT_NAME})
target_include_directories( ${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}
${subprojects_SOURCE_DIR}/include
)
target_link_libraries(${PROJECT_NAME}
${subprojects_SOURCE_DIR}/lib/libstatic_library.a
)
#定义子目录
add_subdirectory(public1)
add_subdirectory(public2)
)
# Set the project name
project (public1)
# Add a library with the above sources
add_library(${PROJECT_NAME} public1.c)
add_library(lib::public1 ALIAS ${PROJECT_NAME})
target_include_directories( ${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}
)
# Set the project name
project (public2)
# Add a library with the above sources
add_library(${PROJECT_NAME} public2.c)
add_library(lib::public2 ALIAS ${PROJECT_NAME})
target_include_directories( ${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}
)
$ mkdir build
$ cd build/
$ cmake ..
$ make
$ cd main
./main
main: cmake
app1: cmake
app1: end
app2: cmake
public2: cmake
public2: end
app2: end
app3: cmake
static hello: cmake
app3: end
public1: cmake
public1: end
public2: cmake
public2: end
static hello: cmake
main: end