【问题标题】:Can someone explain why my cmake file fails to build my cpp file?有人可以解释为什么我的 cmake 文件无法构建我的 cpp 文件吗?
【发布时间】:2015-01-31 02:41:34
【问题描述】:

所以我有以下 CMakeLists.txt 文件来尝试构建我的 main.cpp 文件。

     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)

此时,我只是尝试使用 Hello World 程序构建它,以确保一切正常。但是对于我需要编写的程序的其余部分,这些标志是存在的。调用cmake . 的实际过程工作正常,但是当我使用make 时出现以下错误:

[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

我是 Ubuntu 和命令行的新手,因此非常感谢任何帮助。

【问题讨论】:

  • 您不希望在 set 命令中使用 =。将set(extra_flags = -std=c++11 -Werror -Wall -Wextra) 更改为set(extra_flags -std=c++11 -Werror -Wall -Wextra)
  • 还将set(CMAKE_CXX_FLAGS $(CMAKE_CXX_FLAGS) $(extra_flags)) 更改为set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${extra_flags})
  • 感谢您的回复,我已经尝试了这两个更改,但我仍然看到相同的错误,但这次有额外的行/bin/sh: 1: -Werror: not found/bin/sh: 1: -Wall: not found/bin/sh: 1: -Wextra: not found。如果有帮助,我的 CMakeLists.txt 和 main.cpp 也在同一个目录中。
  • 尝试删除 CMakeCache.txt 并使用 cmake 重新配置并重建。
  • 也许这会有所帮助:stackoverflow.com/a/21561742/487892

标签: c++ cmake


【解决方案1】:
  • 确保使用大括号 ${}(以上评论)
  • 如果您想要“重置”,请确保删除您的 CMakeCache.txt(上面的评论)
  • 我还养成了始终使用set(CMAKE_CXX_FLGAS "${CMAKE_CXX_FLAGS} ${extra_flags}") 而不是set(CMAKE_CXX_FLGAS ${CMAKE_CXX_FLAGS} ${extra_flags}) 的习惯,事实证明,这在我的任何应用程序中都会减少头痛。也试试吧。
  • 使用make VERBOSE=1 查看 CMake 实际运行的命令。然后,您可以直接使用该命令并查看如何使其工作。
  • 如果只是 HelloWorld!,请尝试省略 -std=c++11 标志(或任何标志,真的)然后看看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-25
    • 2020-06-11
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多