【发布时间】:2016-03-07 00:58:02
【问题描述】:
我在使用 cmake 链接 fftw 库时遇到问题。我使用 findFFTW.cmake 文件来查找库。我知道这是成功找到库,因为我将 REQUIRED 标志设置为 true 以查找库并且制作过程顺利进行。
尽管将它与我的可执行文件链接,我仍然收到未定义的引用错误。一些相关的帖子,我尝试过他们的解决方案。
Undefined reference to "function name from external library"
http://answers.ros.org/question/171326/catkin-linking-order-undefined-reference-to-symbol/
更新
感谢 ComicSansMS,下面的 CMake 现在应该正确地对依赖项建模。
项目的 CMake 文件(3/7 更新)
cmake_minimum_required(VERSION 2.8.3)
project(gist_extractor)
## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
image_transport
cv_bridge
sensor_msgs
cmake_modules
)
find_package(OpenCV REQUIRED)
find_package(Eigen REQUIRED)
find_package(FFTW REQUIRED)
###########
## Build ##
###########
## Set GIST variables for building library
set(GIST_PATH /home/andy/Development/lear_gist-1.2)
## Specify additional locations of header files
include_directories(include ${catkin_INCLUDE_DIRS} ${GIST_PATH} ${FFTW_INCLUDES})
## Declare a gist library
add_library(gist SHARED ${GIST_PATH}/standalone_image.c ${GIST_PATH}/gist.c) # THIS IS NOT BEING BUILT
target_link_libraries(gist ${FFTW_LIBRARIES})
## Add cmake target dependencies of the library
#MESSAGE( STATUS "GIST_LIBRARY_PATH: " ${GIST_PATH})
## Declare a C++ executable
add_executable(gist_extractor src/gist_extractor.cpp)
target_link_libraries(gist_extractor ${catkin_LIBRARIES} gist)
编辑 2
如果我们使用上述 CMake 文件,现在会出现链接错误。具体来说,当我尝试运行时,make 过程会失败
target_link_libraries(gist_extractor ${catkin_LIBRARIES} gist)
我有几点意见。首先,我的 gist 库正在根据以下控制台消息正确构建。
Linking C shared library /home/andy/Projects/ROS/robot_ws/devel/lib/libgist.so
[ 80%] Built target gist
Scanning dependencies of target gist_extractor
[100%] Building CXX object
`gist_extractor/CMakeFiles/gist_extractor.dir/src/gist_extractor.cpp.o
但是当我们尝试将可执行文件与 gist 库链接时,我们可以看到存在未定义的引用错误。
Linking CXX executable gist_extractor
: undefined reference to `color_gist_scaletab'
这就是为什么我不明白为什么会发生这种情况。在 gist_extractor.cpp 中,我包含了包含“color_gist_scaletab”函数的头文件。具体来说,这个“color_gist_scaletab”在“gist.h”中定义并在“gist.c”中实现。我认为建立我的图书馆要点应该让我可以访问“color_gist_scaletab”。我已经在下面发布了相关文件。
gist.h
#ifndef GIST_H_INCLUDED
#define GIST_H_INCLUDED
#include "standalone_image.h"
/*! Graylevel GIST for various scales. Based on Torralba's Matlab
* implementation. http://people.csail.mit.edu/torralba/code/spatialenvelope/
*
* Descriptor size is w*w*sum(n_orientations[i],i=0..n_scale-1)
*
* @param src Source image
* @param w Number of bins in x and y axis
*/
float *bw_gist_scaletab(image_t *src, int nblocks, int n_scale, const int *n_orientations);
/*! @brief implementation of grayscale GIST descriptor.
* Descriptor size is w*w*(a+b+c)
*
* @param src Source image
* @param w Number of bins in x and y axis
*/
float *bw_gist(image_t *scr, int nblocks, int a, int b, int c);
/*! @brief implementation of color GIST descriptor.
*
* @param src Source image
* @param w Number of bins in x and y axis
*/
float *color_gist(color_image_t *src, int nblocks, int a, int b, int c);
/*! Color GIST for various scales. Based on Torralba's Matlab
* implementation. http://people.csail.mit.edu/torralba/code/spatialenvelope/ */
float *color_gist_scaletab(color_image_t *src, int nblocks, int n_scale, const int *n_orientations);
#endif
gist_extractor.cpp
// color_gist_scaletab is defined in gist.h
// I'm including relevant header file
#include "/home/andy/Development/lear_gist-1.2/gist.h"
//SOME MORE STUFF
// This is where I call the function
float *gist_descriptor = color_gist_scaletab(im, nblocks, n_scale, orientations_per_scale);
【问题讨论】: