其他分享
首页 > 其他分享> > CMake基础(二) - 使用子目录管理项目文件和文件夹

CMake基础(二) - 使用子目录管理项目文件和文件夹

作者:互联网

多个子目录的构建

复制一份文件夹cp -r module2 module2,删除文件夹 my_build_dir,新建文件夹build

/module3$ ls
addition.cpp  build           division.cpp  main.cpp  my_math_dir   print_result.cpp
addition.h    CMakeLists.txt  division.h    makefile  my_print_dir  print_result.h

移动 addition.cppdivision.cpp到新建文件夹my_math_dir

移动 print_result.cpp到新建文件夹my_print_dir

/module3$ ls
addition.h  CMakeLists.txt  main.cpp  my_math_dir   print_result.h
build       division.h      makefile  my_print_dir

编辑CMakeLists.txt,将新建的子目录添加到构建数中

cmake_minimum_required(VERSION 3.0.0)

project(Calculator_Project VERSION 1.0.0)

#我们告诉cmake进入这些子目录,然后找到另一个CMakeLists.txt
add_subdirectory(my_math_dir)
add_subdirectory(my_print_dir)

add_executable(calculator
main.cpp)

target_link_libraries(calculator my_math my_print)

编辑 /module3/my_print_dir/CMakeLists.txt

add_library(my_print
print_result.cpp)

编辑 /module3/my_math_dir/CMakeLists.txt

add_library(my_math
addition.cpp
division.cpp)

此时的目录树看起来就是这样的:

进入/build,执行cmake .. , make

/module3/build$ make
Scanning dependencies of target my_print
[ 14%] Building CXX object my_print_dir/CMakeFiles/my_print.dir/print_result.cpp.o
[ 28%] Linking CXX static library libmy_print.a
[ 28%] Built target my_print
Scanning dependencies of target my_math
[ 42%] Building CXX object my_math_dir/CMakeFiles/my_math.dir/addition.cpp.o
[ 57%] Building CXX object my_math_dir/CMakeFiles/my_math.dir/division.cpp.o
[ 71%] Linking CXX static library libmy_math.a
[ 71%] Built target my_math
Scanning dependencies of target calculator
[ 85%] Building CXX object CMakeFiles/calculator.dir/main.cpp.o
[100%] Linking CXX executable calculator
[100%] Built target calculator

生成可执行文件

/module3/build$ ls
calculator      CMakeFiles           Makefile     my_print_dir
CMakeCache.txt  cmake_install.cmake  my_math_dir
/module3/build$ ./calculator 
Enter first number      65
Enter second number     21
Addition result:        86
Division result:        3.09524

头文件管理

将头文件依次移动到子目录中的include文件夹,源代码移动到src中

依次为每个子目录源代码引入头文件:

/module3/my_math_dir/src/addition.cpp

#include "addition.h"

float addition(float num1, float num2)
{return num1 + num2;}

/module3/my_math_dir/src/division.cpp

#include "division.h"

float division(float num1, float num2)
{return num1 / num2;}

/module3/my_math_dir/src/print_result.cpp

#include <iostream>
#include "print_result.h"

using namespace std;
void print_result(string result_type, float result_value)
{cout << result_type << " result:\t" << result_value << "\n";}

修改子目录CMakeLists.txt ,指定头文件目录

target_include_directories(TargetName | Scope | Arguments)

/module3/my_math_dir/CMakeLists.txt

add_library(my_math
src/addition.cpp
src/division.cpp)
#指定头文件目录
target_include_directories(my_math PUBLIC include)

/module3/my_print_dir/CMakeLists.txt

add_library(my_print
src/print_result.cpp)

target_include_directories(my_print PUBLIC include)

再次执行 cmake .. 与 make 可以得到:

/module3/build$ ls
calculator      cmake_install.cmake  my_print_dir
CMakeCache.txt  Makefile
CMakeFiles      my_math_dir

目标属性与范围

简单来说就是:

有多个不同权限的引入也可以这样使用:

target_include_directories(Target Private xxx PUBLIC yyy)

标签:CMake,子目录,cpp,文件夹,math,print,my,dir,result
来源: https://www.cnblogs.com/anqwjoe/p/16497816.html