cmake指定clang为默认编译器的方法

Linux/Unix

方法1:修改CMakeLists.txt文件

在CMakeLists.txt文件中的project 之前添加CMAKE_C_COMPILER和CMAKE_CXX_COMPILER。
注意:一定要放在project之前。

  1. SET(CMAKE_C_COMPILER /usr/bin/clang)
  2. SET(CMAKE_CXX_COMPILER /usr/bin/clang++)

注意:使用源码安装的clang,一定要填写clang的绝对路径,非/usr/bin/clang这样的路径,如下:

  1. SET(CMAKE_C_COMPILER /home/xxx/soft_ware/llvm/build/bin/clang)
  2. SET(CMAKE_CXX_COMPILER /home/xxx/soft_ware/llvm/build/bin/clang++)

方法 2:在cmake命令后面附加-D选项

如果知道clang版本(可通过clang -v查询),可以指定clang版本

  1. cmake .. -DCMAKE_C_COMPILER=clang-3.6 -DCMAKE_CXX_COMPILER=clang++-3.6

也可以不指定版本号

  1. cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

构建示例:

  1. (pytorch) kevinx@oasis:~/dev/test/build$ cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
  2. -- The C compiler identification is Clang 6.0.0
  3. -- The CXX compiler identification is Clang 6.0.0
  4. -- Check for working C compiler: /usr/bin/clang
  5. -- Check for working C compiler: /usr/bin/clang -- works
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info - done
  8. -- Detecting C compile features
  9. -- Detecting C compile features - done
  10. -- Check for working CXX compiler: /usr/bin/clang++
  11. -- Check for working CXX compiler: /usr/bin/clang++ -- works
  12. -- Detecting CXX compiler ABI info
  13. -- Detecting CXX compiler ABI info - done
  14. -- Detecting CXX compile features
  15. -- Detecting CXX compile features - done
  16. -- Configuring done
  17. -- Generating done
  18. -- Build files have been written to: /home/VASTAI/kevinx/dev/test/build
  19. (pytorch) kevinx@oasis:~/dev/test/build$ make -j10
  20. Scanning dependencies of target test_cmake
  21. [ 50%] Building C object CMakeFiles/test_cmake.dir/main.c.o
  22. [100%] Linking C executable test_cmake
  23. [100%] Built target test_cmake
  24. (pytorch) kevinx@oasis:~/dev/test/build$ ls
  25. CMakeCache.txt CMakeFiles cmake_install.cmake Makefile test_cmake
  26. (pytorch) kevinx@oasis:~/dev/test/build$ ./test_cmake
  27. Hello world!

windows

How do I tell CMake to use Clang on Windows?
说明:本机安装的是vs2019

方法1:修改CMakeLists.txt文件——无效

  1. SET(CMAKE_C_COMPILER "C:\\Dev\\LLVM\\bin\\clang-cl.exe")
  2. SET(CMAKE_CXX_COMPILER "C:\\Dev\\LLVM\\bin\\clang-cpp.exe")

实际测试,windows不起作用?C compiler和CXX compiler还是MSVC

  1. C:\Src\XDemo\demo2>mkdir build
  2. C:\Src\XDemo\demo2>cd build
  3. C:\Src\XDemo\demo2\build>cmake ..
  4. -- Building for: Visual Studio 17 2022
  5. -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
  6. -- The C compiler identification is MSVC 19.33.31630.0
  7. -- The CXX compiler identification is MSVC 19.33.31630.0
  8. -- Detecting C compiler ABI info
  9. -- Detecting C compiler ABI info - done
  10. -- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped
  11. -- Detecting C compile features
  12. -- Detecting C compile features - done
  13. -- Detecting CXX compiler ABI info
  14. -- Detecting CXX compiler ABI info - done
  15. -- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped
  16. -- Detecting CXX compile features
  17. -- Detecting CXX compile features - done
  18. -- Configuring done
  19. -- Generating done
  20. -- Build files have been written to: C:/Src/XDemo/demo2/build

方法 2:在cmake命令后面附加-D选项——无效

  1. cmake .. -DCMAKE_C_COMPILER="C:\Dev\LLVM\bin\clang-cl.exe" -DCMAKE_CXX_COMPILER="C:\Dev\LLVM\bin\clang-cpp.exe"

实际测试,windows不起作用?C compiler和CXX compiler还是MSVC

  1. C:\Src\XDemo\demo2\build>cmake .. -DCMAKE_C_COMPILER="C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\Llvm\\x64\\bin\\clang-cl.exe" -DCMAKE_CXX_COMPILER="C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\Llvm\\x64\\bin\\clang-cpp.exe"
  2. -- Building for: Visual Studio 17 2022
  3. -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
  4. -- The C compiler identification is MSVC 19.33.31630.0
  5. -- The CXX compiler identification is MSVC 19.33.31630.0
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info - done
  8. -- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped
  9. -- Detecting C compile features
  10. -- Detecting C compile features - done
  11. -- Detecting CXX compiler ABI info
  12. -- Detecting CXX compiler ABI info - done
  13. -- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped
  14. -- Detecting CXX compile features
  15. -- Detecting CXX compile features - done
  16. -- Configuring done
  17. -- Generating done
  18. -- Build files have been written to: C:/Src/XDemo/demo2/build

方法3:cmake命令添加-T ClangCL

假设本机安装了Visual Studio 2019,那么可以使用以下命令:

  1. # Build with the clang-cl toolset
  2. cmake .. -G "Visual Studio 16 2019" -A x64 -T ClangCL

其中,-G “Visual Studio 16 2019”可以省略。

  1. vastai@DESKTOP-7I9GGH2 D:\kevinx\src\test>cd build
  2. vastai@DESKTOP-7I9GGH2 D:\kevinx\src\test\build>cmake .. -G "Visual Studio 16 2019" -T ClangCL
  3. -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
  4. -- The C compiler identification is Clang 12.0.0 with MSVC-like command-line
  5. -- The CXX compiler identification is Clang 12.0.0 with MSVC-like command-line
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info - done
  8. -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe - skipped
  9. -- Detecting C compile features
  10. -- Detecting C compile features - done
  11. -- Detecting CXX compiler ABI info
  12. -- Detecting CXX compiler ABI info - done
  13. -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe - skipped
  14. -- Detecting CXX compile features
  15. -- Detecting CXX compile features - done
  16. -- Configuring done
  17. -- Generating done
  18. -- Build files have been written to: D:/kevinx/src/test/build
  19. vastai@DESKTOP-7I9GGH2 D:\kevinx\src\test\build>cmake --build .
  20. 用于 .NET Framework Microsoft (R) 生成引擎版本 16.11.2+f32259642
  21. 版权所有(C) Microsoft Corporation。保留所有权利。
  22. Checking Build System
  23. Building Custom Rule D:/kevinx/src/test/CMakeLists.txt
  24. test_cmake.vcxproj -> D:\kevinx\src\test\Debug\test_cmake.exe
  25. Building Custom Rule D:/kevinx/src/test/CMakeLists.txt

注意:在 Visual Studio 中使用 cl.exe 和 clang-cl.exe 进行生成。 必须安装适用于 Windows 组件的 C++ Clang 工具,才能使用 clang-cl 进行生成。

Visual Studio 项目中的 Clang/LLVM 支持

image.png