C++ 在 CMake 中设置默认编译器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7081820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:27:08  来源:igfitidea点击:

Setting default compiler in CMake

c++compiler-constructioncmake

提问by Amani

I'm using CMake version 2.8 on WinXP SP3. Whenever i run my CMakeLists script by default CMake use Visual Studio 10 compiler. I've tried to do:

我在 WinXP SP3 上使用 CMake 2.8 版。每当我默认运行 CMakeLists 脚本时,CMake 都会使用 Visual Studio 10 编译器。我试过这样做:

SET( CMAKE_CXX_COMPILER "C:/MinGW/bin/g++" )

without success. How can i set MinGW as my default compiler so that i do not have to worry about setting compiler in the CMakeLists?

没有成功。如何将 MinGW 设置为我的默认编译器,以便我不必担心在 CMakeLists 中设置编译器?

采纳答案by ComicSansMS

With CMake version 3.15 or later, you can set the CMAKE_GENERATORenvironment variable to specify the default generator to be used on your system.

对于 CMake 3.15 或更高版本,您可以设置CMAKE_GENERATOR环境变量以指定要在系统上使用的默认生成器。

回答by sakra

Under Windows CMake uses the newest Visual Studio installationas default generator, unless the generator is explicitly specified upon invoking CMake. This behavior is hard coded and cannot be changed.

在 Windows 下,CMake使用最新的 Visual Studio 安装作为默认生成器,除非在调用 CMake 时明确指定生成器。此行为是硬编码的,无法更改。

As a work-around you can use a batch wrapper script titled cmake.batwith the following contents:

作为解决方法,您可以使用标题cmake.bat为以下内容的批处理包装器脚本:

@cmake.exe -G "MinGW Makefiles" %*

The script should be placed in a directory on the system PATH and should take precedence over the CMake executable cmake.exe.

该脚本应放置在系统 PATH 上的目录中,并应优先于 CMake 可执行文件cmake.exe

The script invokes cmake.exewith MinGW as a generator and forwards all other parameters to it.

该脚本cmake.exe使用 MinGW 作为生成器进行调用,并将所有其他参数转发给它。

回答by Mario

You only have to set the toolchain/output format once, typically you'd do this upon running cmake for the first time:

您只需设置一次工具链/输出格式,通常您会在第一次运行 cmake 时执行此操作:

cmake -G "MinGW Makefiles" .

Instead of the dot you can use your own parameters (if any) and/or the path to the source.

您可以使用自己的参数(如果有)和/或源路径来代替点。

As an alternative, especially when you're new to CMake, use the GUI version under windows (run cmake-guiwithout parameters instead of cmake).

作为替代方案,尤其是当您不熟悉 CMake 时,请在 Windows 下使用 GUI 版本(cmake-gui不带参数而不是运行cmake)。

Once opened, set your paths and click on "Configure". If there's no compiler set, it will ask you to pick one (otherwise you have to clear the cache to make it reappear).

打开后,设置路径并单击“配置”。如果没有设置编译器,它会要求您选择一个(否则您必须清除缓存以使其重新出现)。

Updated configuration values will appear in red and it will also allow you to select files and paths using the common Windows dialog boxes.

更新的配置值将显示为红色,它还允许您使用常见的 Windows 对话框选择文件和路径。

Once configuration is complete and without errors you can hit "generate" to create your makefiles or project files. To update these later on, you can use cmake-guiagain or just use the usual command line version cmake.

一旦配置完成并且没有错误,您就可以点击“生成”来创建您的 makefile 或项目文件。要稍后更新这些,您可以cmake-gui再次使用或仅使用通常的命令行版本cmake