我的程序包含一个子目录,因为那里需要标头和源文件。我的项目如下所示:
编辑:也许我没有指定:我的目标是frame_cap.cpp链接到标头grab_cut.h,因为我需要它的功能。
[-] raspicam
| [-] main_folder
| frame_cap.cpp
| CMakeLists.txt
| [-] opencv-plus
| grab_cut.cpp
| grab_cut.h
| CMakeLists.txt
现在,我已经使用 CMakeList 链接了两者.txt main_folder。它看起来如下:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(frame_cap)
SET(OpenCVPLUS_DIR "/home/pi/Desktop/raspicam/main_folder/opencv-plus")
FIND_PACKAGE( OpenCV REQUIRED )
SET(OpenCV_PACKAGE ${OpenCV_LIBRARIES})
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )
INCLUDE_DIRECTORIES(${OpenCVPLUS_DIR})
LINK_DIRECTORIES(${OpenCVPLUS_DIR})
ADD_SUBDIRECTORY(${OpenCVPLUS_DIR})
ADD_EXECUTABLE( frame_cap frame_cap.cpp )
TARGET_LINK_LIBRARIES( frame_cap ${OpenCV_LIBS} ${OpenCVPLUS_DIR})
如您所见,OpenCVPLUS_DIR
是 opencv-plus 目录,标头文件和源文件都位于该目录。opencv-plus 文件夹中的 CMakeLists.txt 文件如下所示:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(grab_cut)
SET(OpenCVPLUS_DIR "/home/pi/Desktop/raspicam/main_folder/opencv-plus")
FIND_PACKAGE(OpenCV REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
SET(SOURCES grab_cut.cpp ${OpenCVPLUS_DIR}/grab_cut.h)
ADD_EXECUTABLE(grab_cut grab_cut.cpp ${SOURCES})
TARGET_LINK_LIBRARIES(grab_cut ${OpenCV_LIBS})
最后,这是我得到的确切错误:
[ 25%] Linking CXX executable frame_cap
/usr/bin/ld: cannot find -lraspicam/main_folder/opencv-plus/
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/frame_cap.dir/build.make:102: frame_cap] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/frame_cap.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
我很高兴听到您的建议,因为我是CMake的新手,并且不确定我做错了什么。
错误:
/usr/bin/ld: cannot find -lraspicam/main_folder/opencv-plus/
发生是因为您尝试在此处将目录链接到frame_cap
可执行文件:
TARGET_LINK_LIBRARIES( frame_cap ${OpenCV_LIBS} ${OpenCVPLUS_DIR})
这没有意义;只有库应该链接到可执行文件。
从您的评论中,听起来您想构建两个可执行文件,但在两个可执行文件中使用grab_cut
函数。这当然可以通过CMake实现。但是您似乎遇到了设计问题,因为假设两个文件都包含main
函数,使用frame_cap
可执行文件编译grab_cut.cpp
文件可能会导致问题。您需要将main
函数拆分为一个单独的文件(例如,main.cpp
(,然后您可以将grab_cut.cpp
包含在两个可执行文件的编译中。
顺便说一句,您可以使用CMAKE_CURRENT_LIST_DIR
来引用当前 CMakeLists.txt 文件的目录,因此无需显式列出源代码树中的目录。另外,我祈祷您使用的是比 2.8 版更新的 CMake。CMake 文件中的某些步骤似乎没有必要,因此以下是可能适合您的简化文件。
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(frame_cap)
# There is no need to spell out this directory since it exists
# in the source-tree already.
SET(OpenCVPLUS_DIR "/home/pi/Desktop/raspicam/main_folder/opencv-plus")
FIND_PACKAGE( OpenCV REQUIRED )
# Seems unused, remove it.
SET(OpenCV_PACKAGE ${OpenCV_LIBRARIES})
# Include the OpenCV and opencv-plus headers in this executable.
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS} ${CMAKE_CURRENT_LIST_DIR}/opencv-plus)
# There is nolibraryto link from this directory, so we don't need this call.
LINK_DIRECTORIES(${OpenCVPLUS_DIR})
ADD_SUBDIRECTORY(opencv-plus)
# Add the grap_cut.cpp file to the frame_cap executable.
ADD_EXECUTABLE( frame_cap frame_cap.cpp opencv-plus/grab_cut.cpp)
# We don't link anything from opencv-plus, we compiling it directly.
TARGET_LINK_LIBRARIES( frame_cap ${OpenCV_LIBS}${OpenCVPLUS_DIR})
您的opencv-plus/CMakeLists.txt
文件可以简化为如下所示:
# If this CMake file is always called as part of the top-level CMake
# file, don't need this.
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(grab_cut)
# Don't need this, as explained earlier.
SET(OpenCVPLUS_DIR "/home/pi/Desktop/raspicam/main_folder/opencv-plus")
# The top-level CMake file already found OpenCV, so the variables
# we need are already defined.
FIND_PACKAGE(OpenCV REQUIRED)
# The include directories get initialized from the parent CMake file, so no need
# for this call if the include directories are the same for both executables.
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
# Separate 'main' function into separate file, and add it here.
SET(SOURCES grab_cut.cpp grab_cut.h main.cpp)
# Don't need to add 'grab_cut.cpp' to the executable twice! It was
# already added using the 'SOURCES' variable.
ADD_EXECUTABLE(grab_cutgrab_cut.cpp${SOURCES})
TARGET_LINK_LIBRARIES(grab_cut ${OpenCV_LIBS})