cmake单目录构建项目

单文件

1.1 源码:

.
├── build
├── CMakeLists.txt
└── main.c

1 directory, 2 files

// main.c

#include <stdio.h>

int main(int argc, char **argv){
    printf("Hello World from t1 Main
");
    return 0;
}

# CMakeLists.txt

cmake_minimum_required(VERSION 3.11)

project(hello)
message(STATUS "This is BINARY dir" ${PROJECT_BINARY_DIR})
message(STATUS "This is SOURCE dir" ${PROJECT_SOURCE_DIR})

set(SRC_LIST main.c)
message(STATUS ${SRC_LIST})

add_executable(hello main.c)
add_executable(hello2 main.c)

1.2 运行

fl@fl:~/Qt/test_code/cmake/t1/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
-- This is BINARY dir/home/fl/Qt/test_code/cmake/t1/build
-- This is SOURCE dir/home/fl/Qt/test_code/cmake/t1
-- main.c
-- Configuring done
-- Generating done
-- Build files have been written to: /home/fl/Qt/test_code/cmake/t1/build

fl@fl:~/Qt/test_code/cmake/t1/build$ make
Scanning dependencies of target hello2
[ 25%] Building C object CMakeFiles/hello2.dir/main.c.o
[ 50%] Linking C executable hello2
[ 50%] Built target hello2
Scanning dependencies of target hello
[ 75%] Building C object CMakeFiles/hello.dir/main.c.o
[100%] Linking C executable hello
[100%] Built target hello

fl@fl:~/Qt/test_code/cmake/t1/build$ ll
total 84
drwxrwxr-x 4 fl fl  4096 10月 30 16:17 ./
drwxrwxr-x 4 fl fl  4096 10月 28 00:20 ../
drwxrwxr-x 3 fl fl  4096 10月 27 23:52 .cmake/
-rw-rw-r-- 1 fl fl 13723 10月 30 16:17 CMakeCache.txt
drwxrwxr-x 6 fl fl  4096 10月 30 16:17 CMakeFiles/
-rw-rw-r-- 1 fl fl  1512 10月 30 16:17 cmake_install.cmake
-rwxrwxr-x 1 fl fl 16696 10月 30 16:17 hello*
-rwxrwxr-x 1 fl fl 16696 10月 30 16:17 hello2*
-rw-rw-r-- 1 fl fl  5344 10月 30 16:17 Makefile

1.3 注意事项

  1. 采用外部编译的方式,方便代码管理中间文件清理。

    mkdir build          # CMakeLists.txt同级目录
    cd build
    cmake ..             # 定位CMakeLists.txt文件位置
    make
    
  2. cmake_minimum_required(VERSION <min>[...<max>] [FATAL_ERROR]) # 限制使用cmake的最低版本

    1. 最高版本限制可忽略
    2. 自测(仅供参考),FATAL_ERROR没起作用,低于指定版本都是停止。
  3. project(<PROJECT-NAME> [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]] [DESCRIPTION <project-description-string>] [HOMEPAGE_URL <url-string>] [LANGUAGES <language-name>...]) # 设置项目名称

    1. 必填项为project_name

    2. 执行完成project指令后,默认设置的变量包括

      PROJECT_BINARY_DIR
      PROJECT_SOURCE_DIR
      
      PROJECT_VERSION
      PROJECT_VERSION_MAJOR
      PROJECT_VERSION_MINOR
      PROJECT_VERSION_PATCH
      PROJECT_VERSION_TWEAK
      
      PROJECT_DESCRIPTION
      
      PROJECT_HOMEPAGE_URL
      
    3. 构建项目支持的语言包括C,CXX,CUDA,OBJC,OBJCXX, Fortran ASM。不指明时默认启用C和CXX

    4. 在顶层调用project,但是应该在cmake_minimum_required之后。

  4. message([<mode>] "message to display" ...) # 向用户提示一条信息

    1. 可选mode包括FATAL_ERROR/SEND_ERROR/WARNING/AUTHOR_WARNING/DEPRECATION/NOTICE/STATUS/VERBOSE/DEBUG/TRACE

    2. FATAL_ERROR
      CMake Error, stop processing and generation.

      SEND_ERROR
      CMake Error, continue processing, but skip generation.

  5. set(<variable> <value>... [PARENT_SCOPE]) # 为普通变量/缓存/环境变量设置指定值

    1. 设置普通变量 set( ... [PARENT_SCOPE])

    2. 设置缓存条目 set( ... CACHE [FORCE])

    3. 设置环境变量 set(ENV{} [])

  6. add_executable(<name> [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL]
    [source1] [source2 ...]) # 使用指定的源文件生成可执行文件`

    1. name对应逻辑目标名称,必须是全局的,在一个项目中是独一无二的。
    2. source可以使用生成表达式
  7. 使用相同的源文件可以生成多个目标文件。。。测试方便了

2. 多文件

2.1 源码

.
├── build
├── CMakeLists.txt
├── main.c
└── math.c

1 directory, 3 files

// math.c
int math_add(int a, int b) {
    return a + b;
}

// main.c
#include <stdio.h>

extern int math_add(int a, int b);

int main(int argc, char **argv){
    printf("Hello World from t1 Main
");
    printf("3 + 2 = %d
", math_add(3, 2));
    return 0;
}

# CMakeLists.txt
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

project(hello)

message(STATUS "This is BINARY dir" ${PROJECT_BINARY_DIR})
message(STATUS "This is SOURCE dir" ${PROJECT_SOURCE_DIR})

# set(SRC_LIST math.c main.c)
aux_source_directory(. SRC_LIST)
message(STATUS ${SRC_LIST})

add_executable(hello ${SRC_LIST})
add_executable(hello2 ${SRC_LIST})

2.2 运行

fl@fl:~/Qt/test_code/cmake/t1/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
-- This is BINARY dir/home/fl/Qt/test_code/cmake/t1/build
-- This is SOURCE dir/home/fl/Qt/test_code/cmake/t1
-- ./main.c./math.c
-- Configuring done
-- Generating done
-- Build files have been written to: /home/fl/Qt/test_code/cmake/t1/build

fl@fl:~/Qt/test_code/cmake/t1/build$ make
Scanning dependencies of target hello2
[ 16%] Building C object CMakeFiles/hello2.dir/math.c.o
[ 33%] Building C object CMakeFiles/hello2.dir/main.c.o
[ 50%] Linking C executable hello2
[ 50%] Built target hello2
Scanning dependencies of target hello
[ 66%] Building C object CMakeFiles/hello.dir/math.c.o
[ 83%] Building C object CMakeFiles/hello.dir/main.c.o
[100%] Linking C executable hello
[100%] Built target hello
fl@fl:~/Qt/test_code/cmake/t1/build$ ll
total 80
drwxrwxr-x 3 fl fl  4096 10月 30 17:16 ./
drwxrwxr-x 4 fl fl  4096 10月 30 17:12 ../
-rw-rw-r-- 1 fl fl 13723 10月 30 17:15 CMakeCache.txt
drwxrwxr-x 6 fl fl  4096 10月 30 17:16 CMakeFiles/
-rw-rw-r-- 1 fl fl  1512 10月 30 17:15 cmake_install.cmake
-rwxrwxr-x 1 fl fl 16800 10月 30 17:16 hello*
-rwxrwxr-x 1 fl fl 16800 10月 30 17:16 hello2*
-rw-rw-r-- 1 fl fl  6157 10月 30 17:16 Makefile

2.3 注意事项

  1. aux_source_directory(<dir> <variable>) # 查找目录中的所有源文件
    1. 自动搜集以避免手动列出说有实例。
    2. 结果等同于set( var1 var2…)

本文来自博客园,作者:faithlocus,转载请注明原文链接:https://www.cnblogs.com/faithlocus/p/15486640.html

原文地址:https://www.cnblogs.com/faithlocus/p/15486640.html