Can someone explain why my cmake file fails to bui

2019-09-17 06:40发布

问题:

So I've got the following CMakeLists.txt file to try and build my main.cpp file.

     project(shell)

     set(extra_flags = -std=c++11 -Werror -Wall -Wextra)
     set(CMAKE_CXX_FLAGS $(CMAKE_CXX_FLAGS) $(extra_flags))
     add_executable(shell main.cpp)

At this point, I am just trying to build it with a Hello World program just to make sure everything works. But the flags are there for the rest of the program that I need to write. The actual process of calling cmake . works just fine, but when I use make I get the following error:

[100%] Building CXX object CMakeFiles/shell.dir/main.cpp.o
c++: fatal error: no input files
compilation terminated.
/bin/sh: 1: -o: not found 
CMakeFiles/shell.dir/build.make:54: recipe for target 'CMakeFiles/shell.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/shell.dir/main.cpp.o] Error 127
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/shell.dir/all' failed
make[1]: *** [CMakeFiles/shell.dir/all] Error 2
Makefile:72: recipe for target 'all' failed
make: *** [all] Error 2

I'm new to Ubuntu and the command line, so any help would be greatly appreciated.

回答1:

  • Make sure you use curly brackets ${} (comment above)
  • Make sure you delete your CMakeCache.txt if you want a "reset" (comment above)
  • I also developed the habit to always use set(CMAKE_CXX_FLGAS "${CMAKE_CXX_FLAGS} ${extra_flags}") instead of set(CMAKE_CXX_FLGAS ${CMAKE_CXX_FLAGS} ${extra_flags}) which proved to cause less headache in any of my applications. Try that as well.
  • Use make VERBOSE=1 to see which commands CMake actually runs. You then can mess with the command directly and see how you get it to work.
  • If it's just HelloWorld!, try leaving out the -std=c+11 flag (or any flags, really) and see.


标签: c++ cmake