CMake构建错误并添加了头文件-致命错误:找不到文件 [英] CMake build error with added header file - fatal error: file not found

查看:82
本文介绍了CMake构建错误并添加了头文件-致命错误:找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CMake在Ubuntu 14.04中构建C ++源文件.

I used CMake to build C++ source files in Ubuntu 14.04.

我有一个主要的源文件.其中包括一个头文件,该头文件在另一个源文件中包含一个函数.

I has a main source file. This includes a header file, which contains a function in another source file.

我的主要源文件是DisplayImage.cpp,头文件是Camera.h,源文件是Camera.cpp.

My main source file is DisplayImage.cpp, and my header file is Camera.h with a source file Camera.cpp.

每个文件都位于一个文件夹中.我有一个CmakeLists.txt:

Every file is located in one folder. And I have a CmakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp Camera.cpp )
target_link_libraries( DisplayImage  ${OpenCV_LIBS} )

当我在终端中执行命令 cmake.时,它配置成功.之后,我执行命令 make ,然后收到一个致命错误:

When I execute the command cmake . in the terminal, it configures successfully. After that, I execute command make, and I get a fatal error:

fatal error: Camera.h: No such file or directory

请帮助我.我的CmakeLists.txt错误吗?

Please help me. Is my CmakeLists.txt wrong?

推荐答案

您应使用

You should use target_include_directories() to tell CMake to associate specific include directories containing your headers with the DisplayImage target. Assuming your Camera.h file is in the same directory as Camera.cpp, you can use CMAKE_CURRENT_SOURCE_DIR to specify this directory. You should also add the ${OpenCV_INCLUDE_DIRS} here, as shown in the "Using OpenCV with CMake" tutorial.

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp Camera.cpp )
target_include_directories(DisplayImage PRIVATE 
    ${CMAKE_CURRENT_SOURCE_DIR} 
    ${OpenCV_INCLUDE_DIRS}
)
target_link_libraries( DisplayImage PRIVATE ${OpenCV_LIBS} )

注意:总是 指定作用域参数(例如 PUBLIC PRIVATE INTERFACE )中的 target _ * 命令.

Note: it is good CMake practice to always specify the scoping argument (e.g. PUBLIC, PRIVATE, or INTERFACE) in the target_* commands.

这篇关于CMake构建错误并添加了头文件-致命错误:找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆