问题
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