“找不到-l <目录>”错误-CMake

罕见的都铎

我的程序包含一个子目录,因为这里需要头文件和源文件。我的项目如下所示:

编辑:也许我没有指定:我的目标是将frame_cap.cpp链接到头文件handle_cut.h,因为我需要它的功能。

[-] raspicam
 |  [-] main_folder
 |       frame_cap.cpp
 |       CMakeLists.txt
 |       [-] opencv-plus
 |            grab_cut.cpp
 |            grab_cut.h
 |            CMakeLists.txt

现在,我已经使用main_folder中的CMakeLists.txt链接了两者。它看起来如下:

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当然可以实现这一点。但是您似乎遇到了设计问题,因为假设两个文件都包含一个函数grab_cut.cpp,则使用frame_cap可执行文件编译文件可能会引起问题main您需要函数拆分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 no library to 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_cut grab_cut.cpp ${SOURCES}) TARGET_LINK_LIBRARIES(grab_cut ${OpenCV_LIBS})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章