小麦克 发表于 2024-11-4 19:42

《CMake构建实战》CMake 构建目标和属性

# CMake 构建目标和属性

## 二进制构建目标

在构建过程中被构建的可执行文件、库文件或目标文件都可以作为二进制构建目标。

### 可执行文件目标

```cmake
add_executable(<目标名称>
       
       
        [源文件...])
```

该命令会创建一个可执行文件类型的构建目标,其中第一个参数<目标名称>是必选参数,且应当在项目中唯一。

创建一个小工程实践一下。创建`008`文件夹

创建`CMakeLists.txt`

```cmake
cmake_minimum_required(VERSION 3.20)

project(myProgram)
add_executable(myProgram main.c)
add_executable(myProgramExcludedFromAll
      EXCLUDE_FROM_ALL
      main.c)
```

创建`main.c`

```c
#include <stdio.h>
int main() {
      printf("hello world\n");
      return 0;
}
```

执行命令

```bash
bruce@ubuntu:~/Documents/study/cmake/008$ touch CMakeLists.txt
bruce@ubuntu:~/Documents/study/cmake/008$ vi CMakeLists.txt
bruce@ubuntu:~/Documents/study/cmake/008$ vi main.c
bruce@ubuntu:~/Documents/study/cmake/008$ mkdir build
bruce@ubuntu:~/Documents/study/cmake/008$ cd build/
bruce@ubuntu:~/Documents/study/cmake/008/build$ cmake ..
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/bruce/Documents/study/cmake/008/build
bruce@ubuntu:~/Documents/study/cmake/008/build$ make
[ 50%] Building C object CMakeFiles/myProgram.dir/main.c.o
Linking C executable myProgram
Built target myProgram
bruce@ubuntu:~/Documents/study/cmake/008/build$ ./myProgram
hello world
```



### 一般库目标

一般库目标包括静态库目标、动态库目标和模块库目标。

```cmake
add_library(<目标名称> <库类型>
       
        [<源文件>...])
```

该命令会创建一个一般库类型的构建目标,其中<目标名称>参数是必选项,且应在项目中唯一。

<库类型>参数有以下三个取值。

* STATIC

* SHARED

* MODULE

实践,创建`009`文件夹

创建 `CMakeLists.txt`

```cmake
cmake_minimum_required(VERSION 3.20)

project(mylib)
add_library(mylib lib.c)
```

创建`lib.c`

```c
int add(int a, int b) { return a+b; }
```

操作过程如下

```bash
bruce@ubuntu:~/Documents/study/cmake/009$ vi CMakeLists.txt
bruce@ubuntu:~/Documents/study/cmake/009$ ls
CMakeLists.txt
bruce@ubuntu:~/Documents/study/cmake/009$ touch lib.c
bruce@ubuntu:~/Documents/study/cmake/009$ vi lib.c
bruce@ubuntu:~/Documents/study/cmake/009$ ls
CMakeLists.txtlib.c
bruce@ubuntu:~/Documents/study/cmake/009$ mkdir build
bruce@ubuntu:~/Documents/study/cmake/009$ cd build/
bruce@ubuntu:~/Documents/study/cmake/009/build$ cmake ..
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/bruce/Documents/study/cmake/009/build
bruce@ubuntu:~/Documents/study/cmake/009/build$ make
[ 50%] Building C object CMakeFiles/mylib.dir/lib.c.o
Linking C static library libmylib.a
Built target mylib
bruce@ubuntu:~/Documents/study/cmake/009/build$ ls
CMakeCache.txtCMakeFilescmake_install.cmakelibmylib.aMakefile
bruce@ubuntu:~/Documents/study/cmake/009/build$
```

可以看到最后生成了 `libmylib.a`

可以看到不定义`BUILD_SHARED_LIBS`变量时,默认构建静态库。接下来试验定义`BUILD_SHARED_LIBS`

```bash
bruce@ubuntu:~/Documents/study/cmake/009/build$ cmake -DBUILD_SHARED_LIBS=ON ..
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/bruce/Documents/study/cmake/009/build
bruce@ubuntu:~/Documents/study/cmake/009/build$ cmake --build .
[ 50%] Building C object CMakeFiles/mylib.dir/lib.c.o
Linking C shared library libmylib.so
Built target mylib
bruce@ubuntu:~/Documents/study/cmake/009/build$ ls
CMakeCache.txtCMakeFilescmake_install.cmakelibmylib.alibmylib.soMakefile
bruce@ubuntu:~/Documents/study/cmake/009/build$
```

定义了`BUILD_SHARED_LIBS`后生成了`libmylib.so`动态库。



页: [1]
查看完整版本: 《CMake构建实战》CMake 构建目标和属性