0

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?

BoshWash
  • 5,320
  • 4
  • 30
  • 50
  • Looks like your variable isn't being expanded somewhere. Are you explicitly calling out ````${CFLAGS}```` in a non-cmake context? – mascoj Feb 26 '17 at 19:34
  • the link.txt generated by cmake includes the line `/usr/bin/c++ ${CFLAGS} CMakeFiles/cmTC_fef7e.dir/testCXXCompiler.cxx.o -o cmTC_fef7e ``, if i replace it with just -O2 it works – BoshWash Feb 26 '17 at 19:39
  • 2
    Please, provide your code **in the question post**, so it can be checked without going to the linked tutorial. Referencing to `CFLAGS` in the CMake link script looks weird: CMake by itself should substitute value of the flags. Are you intentionally trying to pass flags via environment variable? – Tsyvarev Feb 26 '17 at 20:33
  • @Tsyvarev: no, CFLAGS seems to be used by cmake's generator for Unix Makefiles. Looking at the error log just before the linking, CFLAGS is used as well, but there it expanded the variable correctly. – BoshWash Feb 26 '17 at 20:59
  • 1
    You must look for the problem furthere up!! Your C++ compiler is not recognize and my guess is that if you solve this it will solve your other problem. – oLen Feb 26 '17 at 21:12
  • Look http://stackoverflow.com/questions/9699930/cmake-complains-the-cxx-compiler-identification-is-unknown how to solve this – oLen Feb 26 '17 at 21:14
  • @oLen: I was wondering about that as well, however the generator successfully compiles testCXXCompiler.cxx, so the compiler was found. I also tried the solution from the thread you linked, unfortunately it didn't change the result. – BoshWash Feb 26 '17 at 21:38

0 Answers0