0x01 介绍
当使用 CMake 构建时,可以设置 C 和 C++ 编译器。此示例与 hello-cmake 示例相同,只是它显示了将编译器从默认的 GCC 更改为 clang 的最基本方法。
CMake 中使用 Clang 编译 C/C++ 代码,你可以使用以下方法:
- 首先,确保你已经安装了 Clang 编译器。
- 在你的 CMakeLists.txt 文件中,使用 set 命令设置 CMAKE_C_COMPILER 和 CMAKE_CXX_COMPILER 变量,分别指向 Clang 的 C 和 C++ 编译器:
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
- 如果你需要指定 Clang 编译器的特定版本,你可以使用以下方法:
set(CMAKE_C_COMPILER "clang-9")
set(CMAKE_CXX_COMPILER "clang++-9")
- 如果你需要指定 Clang 编译器的特定路径,你可以使用以下方法:
set(CMAKE_C_COMPILER "/path/to/clang")
set(CMAKE_CXX_COMPILER "/path/to/clang++")
- 最后,使用 CMake 生成和编译你的项目,就可以使用 Clang 编译器了。
例如,你可以在终端中运行以下命令:
mkdir build cd build cmake .. make
0x02 示例
本教程中的文件如下:
ln28@DESKTOP-FS9U3GT:/mnt/d/Project/Cmake_examples/cmake_basics_09$ tree . ├── CMakeLists.txt ├── build.clang └── main.cpp 1 directory, 2 files
- [CMakeLists.txt] - 包含要运行的 CMake 命令。
# Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version cmake_minimum_required(VERSION 3.1) # cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ && make -j24 VERBOSE=1 set(CMAKE_C_COMPILER "clang") set(CMAKE_CXX_COMPILER "clang++") # Set the project name project (hello_cmake_bin) # Add an executable add_executable(hello_cmake_bin main.cpp)
- [main.cpp] - 一个简单的C++ 文件。
#include <iostream> int main(int argc, char *argv[]) { std::cout << "Hello CMake!" << std::endl; return 0; }
ln28@DESKTOP-FS9U3GT:/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang$ cmake .. && make -j24 VERBOSE=1 -- Configuring done -- Generating done -- Build files have been written to: /mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang /usr/bin/cmake -S/mnt/d/Project/Cmake_examples/cmake_basics_09 -B/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang --check-build-system CMakeFiles/Makefile.cmake 0 /usr/bin/cmake -E cmake_progress_start /mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang/CMakeFiles /mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang/CMakeFiles/progress.marks make -f CMakeFiles/Makefile2 all make[1]: Entering directory '/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang' make -f CMakeFiles/hello_cmake_bin.dir/build.make CMakeFiles/hello_cmake_bin.dir/depend make[2]: Entering directory '/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang' cd /mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /mnt/d/Project/Cmake_examples/cmake_basics_09 /mnt/d/Project/Cmake_examples/cmake_basics_09 /mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang /mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang /mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang/CMakeFiles/hello_cmake_bin.dir/DependInfo.cmake --color= Dependee "/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang/CMakeFiles/hello_cmake_bin.dir/DependInfo.cmake" is newer than depender "/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang/CMakeFiles/hello_cmake_bin.dir/depend.internal". Dependee "/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang/CMakeFiles/hello_cmake_bin.dir/depend.internal". Scanning dependencies of target hello_cmake_bin make[2]: Leaving directory '/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang' make -f CMakeFiles/hello_cmake_bin.dir/build.make CMakeFiles/hello_cmake_bin.dir/build make[2]: Entering directory '/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang' [ 50%] Building CXX object CMakeFiles/hello_cmake_bin.dir/main.cpp.o /usr/bin/clang++ -o CMakeFiles/hello_cmake_bin.dir/main.cpp.o -c /mnt/d/Project/Cmake_examples/cmake_basics_09/main.cpp [100%] Linking CXX executable hello_cmake_bin /usr/bin/cmake -E cmake_link_script CMakeFiles/hello_cmake_bin.dir/link.txt --verbose=1 /usr/bin/clang++ -rdynamic CMakeFiles/hello_cmake_bin.dir/main.cpp.o -o hello_cmake_bin make[2]: Leaving directory '/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang' [100%] Built target hello_cmake_bin make[1]: Leaving directory '/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang' /usr/bin/cmake -E cmake_progress_start /mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang/CMakeFiles 0 ln28@DESKTOP-FS9U3GT:/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang$ ./hello_cmake_bin Hello CMake!
1.编译器选项
CMake 公开了用于控制编译和链接代码的程序的选项。这些程序包括:
- CMAKE_C_COMPILER - 用于编译 c 代码的程序.
- CMAKE_CXX_COMPILER - 用于编译 c++ 代码的程序.
- CMAKE_LINKER - 用于链接二进制文件的程序.
在本例中,clang通过命令sudo apt-get install clang安装的。查看ls -ls /usr/bin/clang*
这是调用 clang 的最基本、最简单的方式。未来的示例将展示调用编译器的更好方法。
2.设置标志
通过命令ls /usr/bin/clang*查看系统安装clang的版本。
ln28@DESKTOP-FS9U3GT:/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang$ ls /usr/bin/clang* /usr/bin/clang /usr/bin/clang++ /usr/bin/clang++-10 /usr/bin/clang-10 /usr/bin/clang-cpp-10
下面是通过命令行向编译器传递参数的示例。
cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
下面是通过新增CMakeLists.txt的标志设置clang示例。
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
在设置这些选项之后,当你运行 make 时,clang 将用于编译你的二进制文件。这可以从 make 输出中的以下几行看出。
/usr/bin/clang++ -o CMakeFiles/hello_cmake_bin.dir/main.cpp.o -c /mnt/d/Project/Cmake_examples/cmake_basics_09/main.cpp [100%] Linking CXX executable hello_cmake_bin /usr/bin/cmake -E cmake_link_script CMakeFiles/hello_cmake_bin.dir/link.txt --verbose=1 /usr/bin/clang++ -rdynamic CMakeFiles/hello_cmake_bin.dir/main.cpp.o -o hello_cmake_bin make[2]: Leaving directory '/mnt/d/Project/Cmake_examples/cmake_basics_09/build.clang' [100%] Built target hello_cmake_bin