【问题标题】:No object file from cmake没有来自 cmake 的目标文件
【发布时间】:2014-07-15 16:41:06
【问题描述】:

我有一个 C++ 程序,我正在尝试使用 CMake 制作。 cmake 部分似乎进展顺利,当我在构建目录中运行 cmake .. 时收到以下消息:

-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/program/build

但是当我运行 make -i 时,我得到以下错误:

Scanning dependencies of target project
[100%] Building CXX object CMakeFiles/project.dir/project.cpp.o
Linking CXX executable project
c++: error: CMakeFiles/project.dir/project.cpp.o: No such file or directory
[100%] Built target project

所以我在某个地方错过了一步吗?我是否必须在某处手动创建 CMakeLists.txt 中的目标文件?谢谢!

CMakeLists.txt:

cmake_minimum_required (VERSION 3.0)
project (Project)

#include header files
include_directories(~/project)
set(SOURCES project.cpp ~/project/pmddatadescription.h ~/project/pmdsdk2.h ~/project/pmdsdk2common.h ~/project/stdafx.h)

# PCL
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

# VTK
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

# add the executable
add_executable(project project.cpp ${SOURCES})
#link installed libraries
target_link_libraries (project ${PCL_LIBRARIES})
target_link_libraries(project ${VTK_LIBRARIES})
#link .so libraries
target_link_libraries(project ~/project/libpmdaccess2.so)

【问题讨论】:

  • 只为SOURCES指定.cpp文件
  • @πάνταῥεῖ 包含 .h 文件应该没问题(事实上,我一直这样做,所以它们会出现在 IDE 中)。我认为这不是问题的原因,但他确实双重声明了 project.cpp。您还应该只有一个 target-link-libraries 语句。
  • 您的可执行文件中还包含两次 project.cpp:一次在变量 SOURCES 中,一次手动列出。
  • @MadScienceDreams 我也不认为这会是问题的原因。我只是在旁注中提到了这一点。
  • 感谢您的建议。这是我更新的 CMakeLists.txt。我仍然得到同样的错误。 pastebin.com/Mv3By1q3

标签: c++ linux makefile cmake object-files


【解决方案1】:

也许,问题出在-i 选项中?

-i, --ignore-errors
    Ignore all errors in commands executed to remake files.

【讨论】:

  • 我这样做是为了忽略编译 program.cpp 时出现的 3 个错误。不要认为这是问题所在。如果我不包含 -i,它甚至不会到达那一步。
  • @user3738294 修复 program.cpp 中的错误,很可能由于构建 program.cpp 中的错误而跳过构建 project.o。确切地说,它不会到达那一步,因为您必须在项目可以成功链接之前修复编译错误。
  • +1 我相信你是对的,我以前从未见过有人使用过这个命令。但是,是的,如果 project.cpp 中有错误,您究竟希望它如何生成 project.o?
  • 好的,我会尝试解决这个问题。当我之前使用 Makefile 时,它​​只是一个警告,所以代码编译得很好。但是现在使用 CMake,它似乎已经升级为错误。
  • CMake 只是 GNU Make 之上的一个新抽象层(在您的情况下)。每个警告都值得关注。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多