Ask Your Question
0

undefined reference to a static library

asked 2013-05-12 20:48:32 -0500

vonovak gravatar image

updated 2013-11-14 11:10:09 -0500

tfoote gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-05-21 14:00:31 -0500

vonovak gravatar image

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.

edit flag offensive delete link more

Comments

Thx vonovak! it works

redheli gravatar imageredheli ( 2015-12-01 04:53:24 -0500 )edit
1

answered 2013-05-13 02:44:17 -0500

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()
  message(FATAL_ERROR "Library (CCV) not found")
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.

edit flag offensive delete link more

Comments

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-05-12 20:48:32 -0500

Seen: 2,581 times

Last updated: May 21 '13