我正在尝试遵循这个cmake 教程。但是当第一次调用 cmake 时,我收到以下错误:
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
当我调用echo $CFLAGS
bash 时,值为:
-march=native -O2 -pipe -fstack-protector-strong
但是在执行 cmake_link_script 时看起来${CFLAGS}
没有定义。为了验证我将${CFLAGS}
link.txt 更改为-march=native -O2 -pipe -fstack-protector-strong
并手动调用:
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7a86e.dir/link.txt
哪个有效。
接下来我尝试将以下行添加到 CMakeLists.txt:
set (CFLAGS -O2)
这没有帮助。
也没有打电话:
cmake -DCFLAGS=-O2 -G "Unix Makefiles" ..
所以我的问题是,如何定义 CFLAGS 以便在 cmake 的命令模式下调用 cmake_link_script 时定义它?
编辑
看起来 cmake 使用“Unix Makefiles”生成器失败了,甚至没有得到教程中的代码。
CMakeLists.txt 看起来像这样:
project("To Do List")
add_executable(toDo main.cc
ToDo.cc)
编辑2
我找到了解决方法。出于某种原因CMAKE_CXX_FLAGS:STRING='${CFLAGS} '
,CMakeCache.txt 中的生成器集。但是当生成器被调用时:
cmake -DCMAKE_CXX_FLAGS:STRING="${CFLAGS} " -G "Unix Makefiles" ..
一切正常。甚至“CXX 编译器标识未知”消息也消失了。但是,我仍然想知道为什么之前生成器失败了?