I'm trying to follow this cmake tutorial. But when it comes to calling cmake for the first time I get the following error:
cmake -G "Unix Makefiles" .. --debug-trycompile
debug trycompile on
-- The C compiler identification is GNU 6.3.1
-- The CXX compiler identification is unknown
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- broken
CMake Error at /usr/share/cmake-3.7/Modules/CMakeTestCXXCompiler.cmake:44 (message):
The C++ compiler "/usr/bin/c++" is not able to compile a simple test
program.
It fails with the following output:
Change Dir: /home/user01/code/tests/cmake_tutorial_02/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_7a86e/fast"
/usr/bin/make -f CMakeFiles/cmTC_7a86e.dir/build.make
CMakeFiles/cmTC_7a86e.dir/build
make[1]: Entering directory
'/home/user01/code/tests/cmake_tutorial_02/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_7a86e.dir/testCXXCompiler.cxx.o
/usr/bin/c++ -march=native -O2 -pipe -fstack-protector-strong -o
CMakeFiles/cmTC_7a86e.dir/testCXXCompiler.cxx.o -c
/home/user01/code/tests/cmake_tutorial_02/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
Linking CXX executable cmTC_7a86e
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7a86e.dir/link.txt
--verbose=1
/usr/bin/c++ ${CFLAGS} CMakeFiles/cmTC_7a86e.dir/testCXXCompiler.cxx.o -o
cmTC_7a86e
c++: error: ${CFLAGS}: No such file or directory
When I call echo $CFLAGS
in bash the value is:
-march=native -O2 -pipe -fstack-protector-strong
But it looks like ${CFLAGS}
is not defined while executing cmake_link_script. To verify I changed ${CFLAGS}
in link.txt to -march=native -O2 -pipe -fstack-protector-strong
and manually called:
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7a86e.dir/link.txt
which works.
Next I tried adding the following line to CMakeLists.txt:
set (CFLAGS -O2)
which didn't help.
Neither did calling:
cmake -DCFLAGS=-O2 -G "Unix Makefiles" ..
So my question is, how to define CFLAGS so it is defined when calling the cmake_link_script in cmake's command mode?
EDIT
It looks like cmake fails withe the "Unix Makefiles" generator and doesn't even get to the code from the Tutorial.
the CMakeLists.txt just looks like this:
project("To Do List")
add_executable(toDo main.cc
ToDo.cc)
EDIT2
I found a workaround.
For some reason the generator sets CMAKE_CXX_FLAGS:STRING='${CFLAGS} '
in CMakeCache.txt.
But when the generator gets invoked with:
cmake -DCMAKE_CXX_FLAGS:STRING="${CFLAGS} " -G "Unix Makefiles" ..
everything works. Even the "The CXX compiler identification is unknown" message is gone. However, I would still like to know why the generator failed before?