gpt4 book ai didi

c++ - “Cannot find -l”错误-CMake

转载 作者:行者123 更新时间:2023-12-02 10:30:39 25 4
gpt4 key购买 nike

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

编辑:也许我没有指定:我的目标是将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目录, header 和源文件都位于该目录中。 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})

关于c++ - “Cannot find -l<directory>”错误-CMake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62359896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com