Strange error: undefined reference to `class::clas

2019-09-25 05:53发布

问题:

I have a library irWGN dependent of another library irRNG.

My CMakeFiles.txt's are as follows. The one is src/signals is

add_library(irRNG irRNG.cpp)
add_library(irWGN irWGN.cpp)

and the main CMakeFile

include_directories(${SRC}/signals)
SET(MY_LIB
  ${MY_LIB}
  irRNG
  irWGN
  )
....

foreach(file2link ${FILES_to_RUN})

  target_link_libraries(${file2link}
    ${catkin_LIBRARIES}
    ${Boost_LIBRARIES}
    ${gsl_LIBRARIES}
    ${OpenCV_LIBRARIES}
    ${MY_LIB}
    )
   add_dependencies(${file2link} project_generate_messages_cpp)
endforeach(file2link)

Am getting this error

./devel/lib/libirWGN.so: undefined reference to `irRNG::irRNG()'
../devel/lib/libirWGN.so: undefined reference to `irRNG::~irRNG()'

The strange thing is that I was using this on ubuntu 12.04 without any issue. Only now that this problem appears.

回答1:

Since the problem is that your irWGN depends on your irRNG, the most elegant way to fix your issue would be to add:

target_link_libraries(irWGN irRNG)

Creating an interdependency among your libraries. It works also for static libraries! So, if you try to link an executable to irWGN, cmake will automatically also link to irRNG, and in the correct order.