# undefined reference to a static library

hello, I'm having a lot of trouble with this one: (ros fuerte, ubuntu 12.04 LTS)

I'm working on a C++ package that uses a C library libccv.a. When linking, I am getting this error:

undefined reference to 'ccv_enable_default_cache()'


and more undefined references to the stuff from the library

libccv.a comes with a header file ccv.h and it depends on libjpeg and libpng.

here is the relevant part of the CMakeLists.txt

set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) rosbuild_add_executable(${PROJECT_NAME} src/TDetector.cpp src/TRecognizer.cpp src/${PROJECT_NAME}.cpp) FIND_LIBRARY(ccv libccv.a PATHS${LIBRARY_OUTPUT_PATH}))
include_directories(${PROJECT_SOURCE_DIR}/src) FIND_LIBRARY(jpeg libjpeg.a PATHS usr/lib) FIND_LIBRARY(png libpng.a PATHS usr/lib) target_link_libraries(${PROJECT_NAME} ${ccv}${jpeg} ${png})  the library is in /lib folder of my project. everything else (source and headers) is in /src. I also tried the link_directories way instead of find_library, without success. I believe the linker finds the library because there is no cannot find message I read some resources on rosbuild but I can't make it work. What am I doing wrong? Any help is greatly appreciated, thanks! edit retag close merge delete ## 2 Answers Sort by » oldest newest most voted The problem was in linking a C library into a C++ program. The solution was to wrap ccv.h into ccv.hpp which looks as follows extern "C" { #include "ccv.h" }  (add include guard as you like) Then, I included ccv.hpp in the sources. If you want to know more, google name mangling. And second, use find_package command to load the jpeg and png libraries. more ## Comments Thx vonovak! it works ( 2015-12-01 04:53:24 -0500 )edit I think the issue is that you have specified the full name of libccv.a and should instead use the shortened ccv form. Also, according to the cmake documentation, find_library won't display an error message if the library is not found. Try this, instead: set(LIBRARY_OUTPUT_PATH${PROJECT_SOURCE_DIR}/lib)
rosbuild_add_executable(${PROJECT_NAME} src/TDetector.cpp src/TRecognizer.cpp src/${PROJECT_NAME}.cpp)

FIND_LIBRARY(ccv ccv PATHS ${LIBRARY_OUTPUT_PATH})) if (CCV) message(STATUS "Library(CCV) found in${CCV}")
else()
endif()

include_directories(${PROJECT_SOURCE_DIR}/src) FIND_LIBRARY(jpeg libjpeg.a PATHS usr/lib) FIND_LIBRARY(png libpng.a PATHS usr/lib) target_link_libraries(${PROJECT_NAME} ${ccv}${jpeg} \${png})


You should probably be using find_package(jpeg REQUIRED) and find_package(png REQUIRED) instead, but what you have listed above will probably work also.

more

Thanks, this looks better. However, I am getting "Library (CCV) not found", even though the path provided is correct. Changing between libccv.a and ccv doesn't help. Neither does moving the library to different location. Any ideas? Thanks

( 2013-05-13 09:23:39 -0500 )edit