Linux/Unix
方法1:修改CMakeLists.txt文件
在CMakeLists.txt文件中的project 之前添加CMAKE_C_COMPILER和CMAKE_CXX_COMPILER。
注意:一定要放在project之前。
SET(CMAKE_C_COMPILER /usr/bin/clang)
SET(CMAKE_CXX_COMPILER /usr/bin/clang++)
注意:使用源码安装的clang,一定要填写clang的绝对路径,非/usr/bin/clang这样的路径,如下:
SET(CMAKE_C_COMPILER /home/xxx/soft_ware/llvm/build/bin/clang)
SET(CMAKE_CXX_COMPILER /home/xxx/soft_ware/llvm/build/bin/clang++)
方法 2:在cmake命令后面附加-D选项
如果知道clang版本(可通过clang -v查询),可以指定clang版本
cmake .. -DCMAKE_C_COMPILER=clang-3.6 -DCMAKE_CXX_COMPILER=clang++-3.6
也可以不指定版本号
cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
构建示例:
(pytorch) kevinx@oasis:~/dev/test/build$ cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
-- The C compiler identification is Clang 6.0.0
-- The CXX compiler identification is Clang 6.0.0
-- Check for working C compiler: /usr/bin/clang
-- Check for working C compiler: /usr/bin/clang -- 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/clang++
-- Check for working CXX compiler: /usr/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/VASTAI/kevinx/dev/test/build
(pytorch) kevinx@oasis:~/dev/test/build$ make -j10
Scanning dependencies of target test_cmake
[ 50%] Building C object CMakeFiles/test_cmake.dir/main.c.o
[100%] Linking C executable test_cmake
[100%] Built target test_cmake
(pytorch) kevinx@oasis:~/dev/test/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile test_cmake
(pytorch) kevinx@oasis:~/dev/test/build$ ./test_cmake
Hello world!
windows
How do I tell CMake to use Clang on Windows?
说明:本机安装的是vs2019
方法1:修改CMakeLists.txt文件——无效
SET(CMAKE_C_COMPILER "C:\\Dev\\LLVM\\bin\\clang-cl.exe")
SET(CMAKE_CXX_COMPILER "C:\\Dev\\LLVM\\bin\\clang-cpp.exe")
实际测试,windows不起作用?C compiler和CXX compiler还是MSVC
C:\Src\XDemo\demo2>mkdir build
C:\Src\XDemo\demo2>cd build
C:\Src\XDemo\demo2\build>cmake ..
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The C compiler identification is MSVC 19.33.31630.0
-- The CXX compiler identification is MSVC 19.33.31630.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- 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
-- 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: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Src/XDemo/demo2/build
方法 2:在cmake命令后面附加-D选项——无效
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
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"
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The C compiler identification is MSVC 19.33.31630.0
-- The CXX compiler identification is MSVC 19.33.31630.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- 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
-- 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: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Src/XDemo/demo2/build
方法3:cmake命令添加-T ClangCL
假设本机安装了Visual Studio 2019,那么可以使用以下命令:
# Build with the clang-cl toolset
cmake .. -G "Visual Studio 16 2019" -A x64 -T ClangCL
其中,-G “Visual Studio 16 2019”可以省略。
vastai@DESKTOP-7I9GGH2 D:\kevinx\src\test>cd build
vastai@DESKTOP-7I9GGH2 D:\kevinx\src\test\build>cmake .. -G "Visual Studio 16 2019" -T ClangCL
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The C compiler identification is Clang 12.0.0 with MSVC-like command-line
-- The CXX compiler identification is Clang 12.0.0 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe - 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: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/kevinx/src/test/build
vastai@DESKTOP-7I9GGH2 D:\kevinx\src\test\build>cmake --build .
用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.11.2+f32259642
版权所有(C) Microsoft Corporation。保留所有权利。
Checking Build System
Building Custom Rule D:/kevinx/src/test/CMakeLists.txt
test_cmake.vcxproj -> D:\kevinx\src\test\Debug\test_cmake.exe
Building Custom Rule D:/kevinx/src/test/CMakeLists.txt
注意:在 Visual Studio 中使用 cl.exe 和 clang-cl.exe 进行生成。 必须安装适用于 Windows 组件的 C++ Clang 工具,才能使用 clang-cl 进行生成。