16

I'm trying to force cmake to build my cpp code with g++, as by default it uses clang instead. So I use: cmake -D CMAKE_CXX_COMPILER=g++ ../src/CMakeLists.txt after which cmake checks for gcc and g++ (with success), but nonetheless make VERBOSE=1 yields

/usr/bin/c++     -o CMakeFiles/trial_cpp.dir/trial.cpp.o -c "/Users/Kuba/Code/Sketchpad/Trial     project/src/trial.cpp"
Linking CXX executable trial_cpp
/opt/etlocal/bin/cmake -E cmake_link_script CMakeFiles/trial_cpp.dir/link.txt --verbose=1
/usr/bin/c++    -Wl,-search_paths_first -Wl,-headerpad_max_install_names   CMakeFiles/trial_cpp.dir/trial.cpp.o  -o trial_cpp 

As it calls /usr/bin/c++ not /usr/bin/g++ I concur it still uses clang. Any idea what's the problem? I know I have g++ and it's in /usr/bin/. I'm running Mac OS X 10.8.2

Puchatek
  • 1,477
  • 3
  • 15
  • 33
  • 3
    Probably a silly question but many people forget. Did you remove the entire contents of the build directory before running cmake with that configuration option? Once that variable is set once for a given build directory (automatically or manually) it is ignored on future runs so that option has to be used on the first run and only on the first run. – John5342 Dec 13 '12 at 02:54
  • @John5342 - that did the trick! I'm a cmake newbie, so didn't know about the need to clean build dir. Thank you. – Puchatek Dec 13 '12 at 03:32
  • Since I was by chance correct I have added it as a proper answer including the source too. When I first learned cmake I found the manual and the quoted page very useful. – John5342 Dec 13 '12 at 13:02

2 Answers2

16

CMAKE_CXX_COMPILER can only be set the first time cmake is run in a given build directory. On subsequent runs it is ignored. In order to change CMAKE_CXX_COMPILER you first need to delete the contents of the build directory and then run cmake again with that option.

Source: http://www.cmake.org/Wiki/CMake_Useful_Variables

I believe the reasoning for only using that variable on the first run is because changing it later would potentially invalidate everything already built including the configuration checks so cmake would have to start from scratch anyway.

John5342
  • 1,129
  • 1
  • 10
  • 13
2

I do it like this instead:

CXX=/usr/bin/g++ cmake ../src/CMakeLists.txt
Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
  • 1
    Doesn't work either... echo $CXX yeilds /usr/bin/g++, but when compiling I still see /usr/bin/c++ is used :| – Puchatek Dec 13 '12 at 03:30