43

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:

  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?
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
feelfree
  • 11,175
  • 20
  • 96
  • 167

2 Answers2

21

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

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 Files and Variables for Languages. 2. and 3. come from these sections.

Eric
  • 19,525
  • 19
  • 84
  • 147
luk32
  • 15,812
  • 38
  • 62
  • What are the valid language options? Specifically, what do I type as the LANGUAGE ???? for Objective-C++ (.mm file) in Ubuntu Linux? – gone Jul 02 '14 at 14:43
  • This is a completely different question. IMO you should ask it separately. Especially since I don't know anything about Objective-C++ =). The thing is, I don't know if it supported by default. If not, you would probably end up having to set up everything by yourself, i.e. all `**` variables, but you would get to pick the variable name as you want =). – luk32 Jul 02 '14 at 14:46
15

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++.

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

export CC=clang export CXX=clang++

before running cmake.

martiert
  • 741
  • 4
  • 13