【发布时间】:2018-09-06 23:26:14
【问题描述】:
在安装了 VS2017 和 LLVM 的 Windows 10 上
而 CMAKE 只是将 VC 编译器作为默认值,并忽略 CMakeLists.txt 中的 set 命令和命令行 -DCMAKE_CXX_COMPILER
我不知道如何让它使用 LLVM Clang
我检查了许多现有问题,但它们对我不起作用
CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
set(CMAKE_CXX_COMPILER "C:\\Program Files\\LLVM\\bin\\Clang++")
project(CMakeTest1 VERSION 0.0.0 LANGUAGES CXX)
aux_source_directory(. MAINDIR)
add_executable(CMakeTest1 ${MAINDIR})
main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
用 git bash 运行命令
***@DESKTOP-8KJ4J0H MINGW64 /d/CMakeTest1
$ cmake -DCMAKE_CXX_COMPILER="C:\Program Files\LLVM\bin\Clang++" .
-- Building for: Visual Studio 15 2017
-- The CXX compiler identification is MSVC 19.13.26129.0
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe -- 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: D:/CMakeTest1
我的 CXX 编译器标识被强制为 MSVC,不能通过任何方法更改。
不知道为什么
编辑: 我要强烈声明的是,我发誓并且清楚地知道我从文件夹中清除了所有生成的文件,并且只有 CMakeLists.txt 和 main.cpp 存在。而且我不想被认为没有删除旧的缓存文件
【问题讨论】:
-
CMakeCache.txt的内容是什么?您是否尝试在运行 cmake 之前删除此文件? -
如the documentation 中所述,一旦设置就无法更改。您必须删除 所有 CMake 生成的项目文件,并将
CMAKE_CXX_COMPILER设置为您要使用的编译器,重新制作它。 -
稍微相关一点,不建议在与源代码树的其余部分相同的目录中创建 CMake 构建/项目文件。创建一个子目录(例如
mkdir build),转到该目录(cd build)并在那里生成构建文件(cmake ..)。这将使您在更改编译器时根据需要从头开始重新生成项目变得非常更容易,因为您只需删除单独的构建目录。 -
我要强烈声明的是,我发誓并且清楚地知道我从文件夹中清除了所有生成的文件,并且只有 CMakeLists.txt 和 main.cpp 存在@hgminh
-
在 linux 上使用 Cmake v3.10 对我来说效果很好,使用了 Clang。您可以使用 CMAKE_EXPORT_COMPILE_COMMANDS 来确保不使用 clang 吗?