Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make CMake use GCC instead of Clang on Mac OS X?

Tags:

c++

cmake

I can't find any info on it, but only the other way around (e.g., how to set CMake to use clang).

I've installed gcc-4.8 using brew, setup all dependencies, headers, etc, and now CMake refuses to use gcc.

I've set my bash profile with both aliases and actual entries:

export CC=/usr/bin/gcc export CXX=/usr/bin/g++ alias gcc='gcc-4.8' alias cc='gcc-4.8' alias g++='g++-4.8' alias c++='c++-4.8' 

Yet CMake stubbornly refuses to use gcc and instead reverts back to clang:

air:build alex$ cmake -DCMAKE_BUILD_TYPE=DEBUG .. -- The C compiler identification is Clang 5.1.0 -- The CXX compiler identification is Clang 5.1.0 -- Check for working C compiler: /usr/bin/gcc -- Check for working C compiler: /usr/bin/gcc -- works 
like image 523
Ælex Avatar asked Jun 24 '14 07:06

Ælex


People also ask

Can CMake use GCC?

Usually under Linux, one uses CMake to generate a GNU make file which then uses gcc or g++ to compile the source file and to create the executable.

Does Mac use Clang or GCC?

Apple ships the clang/LLVM compiler with macOS. Clang is a "front-end" that can parse C , C++ and Objective-C down to something that LLVM (referred to as a "back-end") can compile. Clang/LLVM is located in /Applications/Xcode. app/somewhere.

Is clang better than GCC?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.


2 Answers

CMake doesn't (always) listen to CC and CXX. Instead use CMAKE_C_COMPILER and CMAKE_CXX_COMPILER:

cmake -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ ... 

See also the documentation.

Alternatively, you can provide a toolchain file, but that might be overkill in this case.

like image 110
rubenvb Avatar answered Sep 28 '22 00:09

rubenvb


Current versions of CMake do not respect the CC and CXX environment variables like one would expect. Specifically if they are absolute paths to the compiler binaries they seem to be ignored. On my system with a freshly compiled cmake 3.7.1 I have to do cmake -H. -Bbuild -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX.

As others have stated it is not a great idea to force a compiler choice within your CMakeLists.txt, however if this is required for your use case here's how you do it:

cmake_minimum_required(VERSION 3.5) # Or whatever version you use  # THIS HAS TO COME BEFORE THE PROJECT LINE set(CMAKE_C_COMPILER "gcc") set(CMAKE_CXX_COMPILER "g++") # THIS HAS TO COME BEFORE THE PROJECT LINE  project(my_project VERSION 0.0.0 LANGUAGES C CXX) 

In this case cmake will fail if the indicated compiler is not found. Note that you must set these variables before the project line as this command is what finds and configures the compilers.

like image 39
Norman B. Lancaster Avatar answered Sep 27 '22 23:09

Norman B. Lancaster