C++ CMake如何选择gcc和g++进行编译?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12475056/
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-27 16:18:21  来源:igfitidea点击:

How does CMake choose gcc and g++ for compiling?

c++cmake

提问by feelfree

I am newbie of CMake, and I was wondering for a C++/C project in the LINUX environmental how CMake can choose compilers between gcc and g++. More specifically, my questions are as follows:

我是 CMake 的新手,我想知道 LINUX 环境中的 C++/C 项目,CMake 如何在 gcc 和 g++ 之间选择编译器。更具体地说,我的问题如下:

  1. If a project is consisted of .c and .cpp file, is it true that the .c files will be compiled by gcc while the .cpp files will be compiled by g++?
  2. If a project has only c files or cpp files, what's the default compiling operation for CMake? Will it be possible to change it?
  1. 如果一个项目是由.c和.cpp文件组成的,那么.c文件会被gcc编译而.cpp文件会被g++编译吗?
  2. 如果一个项目只有c文件或cpp文件,CMake的默认编译操作是什么?有可能改变它吗?

采纳答案by luk32

Shortly, yes to both.

很快,两者都是。

You can mangle with pretty much everything. There are flags and variables that bind extensions to language; and then language to compiler options/executables that define toolsets and build targets.

你几乎可以处理所有事情。有将扩展绑定到语言的标志和变量;然后将语言转换为定义工具集和构建目标的编译器选项/可执行文件。

Check following links to documentation. Those are some pleasant short readings.

检查以下文档链接。这些是一些令人愉快的简短阅读。

  1. Change compiler/toolset
  2. Per-language extensions
  3. LANGUAGE variable
  1. 更改编译器/工具集
  2. 每种语言的扩展
  3. 语言变量

Note: The wiki might be outdated but it should hold in case of important and educational matter.

注意:维基可能已经过时,但它应该在重要和教育问题的情况下保持不变。

PS. There is whole bunch of related options. For some longer read you can check following sections of documentation: Properties on Source Filesand Variables for Languages. 2. and 3. come from these sections.

附注。有一大堆相关的选项。如需更长的阅读时间,您可以查看文档的以下部分:源文件的属性语言变量。2. 和 3. 来自这些部分。

回答by martiert

As far as I know CMake only look at the file extensions. So if you rename your .c file to .cpp it will, as far as I know, be compiled with g++.

据我所知,CMake 只看文件扩展名。因此,如果您将 .c 文件重命名为 .cpp,据我所知,它将使用 g++ 进行编译。

It is easy to change that behaviour. CMake uses environment variables to see which compiler to use. If you would like to change compiler to e.g. clang and clang++, you can just do

改变这种行为很容易。CMake 使用环境变量来查看要使用的编译器。如果您想将编译器更改为例如 clang 和 clang++,您可以这样做

export CC=clang export CXX=clang++

export CC=clang export CXX=clang++

before running cmake.

在运行 cmake 之前。