CMake build error with added header file - fatal error: file not found

别等时光非礼了梦想. 提交于 2021-02-07 18:17:58

问题


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.

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

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} )

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

Please help me. Is my CmakeLists.txt wrong?


回答1:


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} )

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



来源:https://stackoverflow.com/questions/26794074/cmake-build-error-with-added-header-file-fatal-error-file-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!