【问题标题】:CMAKE ignores all settings about specifying the compilerCMAKE 忽略所有关于指定编译器的设置
【发布时间】: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 吗?

标签: c++ cmake


【解决方案1】:

这可能是由于 Windows 中默认的 generatorCMake 造成的。

-- Building for: Visual Studio 15 2017
...

您可以通过在命令行中添加-G &lt;generator name&gt; 来更改生成器。在你的情况下,我认为-G 'MSYS Makefiles' 可能有效。

【讨论】:

  • CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
  • 对不起,我复制错了,试试-G 'MSYS Makefiles'吧。我认为它应该适用于 git bash
  • CMake Error: CMake was unable to find a build program corresponding to "MSYS Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
【解决方案2】:

这是因为 CMAKE_CXX_COMPILER 是从您的生成器中推断出来的(Windows 上默认使用最新的 Visual Studio),所以它是 cl.exe

为了切换到clang,您应该使用其他生成器,例如NinjaUnix Makefiles

如果你想通过clang-cl.exe使用VS,你需要通过添加标志-TLLVM-vs2014将平台工具集设置为适当的版本。

请参阅this instructions 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 2013-04-14
    • 1970-01-01
    • 2017-01-19
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多