-2

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.

Courier
  • 920
  • 2
  • 11
  • 36
  • 1
    did you remember to `target_link_libraries()` ? – Richard Hodges May 31 '16 at 07:13
  • What about `add_library(irRNG irRNG.cpp)` in place of `add_library(irRNG irRNG)`? If that is the case, I wonder what CMake is understanding out of the line you used, that could be interesting... – Antonio May 31 '16 at 07:17
  • 1
    Error message involves library `irWGN`, but you show only creation of library `irRNG`. Without viewing related code we cannot help you. – Tsyvarev May 31 '16 at 07:49
  • @RichardHodges Yes, already includes like `target_link_libraries(${file2link} #${MY_LIB} ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${gsl_LIBRARIES} ${OpenCV_LIBRARIES} ${VISP_LIBRARIES} ${MY_LIB} ) add_dependencies(${file2link} project_generate_messages_cpp)` – Courier May 31 '16 at 07:49
  • @Tsyvarev I did not notice that. I will include the source of irWGN – Courier May 31 '16 at 07:53
  • @Tsyvarev just updated the question – Courier May 31 '16 at 07:56
  • 1
    @polar You would also need to report the **cmake** irWGN related code. What about the missing ".cpp" I pointed out? – Antonio May 31 '16 at 07:58
  • @Antonio I added .cpp for add_library but still the same problem.... – Courier May 31 '16 at 08:02
  • 1
    Swap irRNG and irWGN in the `set(MYLIB...` If it doesn't work, please report the complete failing line, `make VERBOSE=1` if you are using make – Antonio May 31 '16 at 08:24
  • @Antonio This seems solving the problem at this stage. But have similar problem for another library. Will try to swap again an let you know In the fact I did something like `target_link_libraries(${file2link} ${MY_LIB} ${MY_LIB} ...)` But it on longer works now.... – Courier May 31 '16 at 08:38
  • @polar See http://stackoverflow.com/q/45135/2436175 – Antonio May 31 '16 at 08:41
  • @Antonio In fact in the past I always did something like `target_link_libraries(${file2link} ${MY_LIB} ${MY_LIB} ...)` and it always worked and on different machines. I do not understand why it does no longer work now; gcc version maybe? – Courier May 31 '16 at 08:45
  • 1
    @polar Yes. Nowadays, if you do it correctly (depending library first, depended library after) you won't need to repeat libraries. It is not your case, but cmake can also handle [circular dependencies](https://cmake.org/cmake/help/v3.3/command/target_link_libraries.html#cyclic-dependencies-of-static-libraries). – Antonio May 31 '16 at 08:51
  • You shouldn't remove the source code, at least not the one of WGN, because it shows the dependency to RNG. In this moment the question wouldn't be answerable. – Antonio May 31 '16 at 09:32

1 Answers1

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.

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • I would like to highlight that this is not the "most elegant way". But thanks a lot!! In fact one should not care at all about the order. Larger is your library (number of files), more troubles you will have when adopting your solution – Courier May 31 '16 at 08:58
  • Since I think there should be a better solution, I will wait if someone proposes something before confirming your solution. But I voted up your solution. – Courier May 31 '16 at 09:05
  • @polar If one library depends on another library, like your libirWGN depends on your libirRNG, it must be stated somewhere. And this is the most elegant way. There is no connection with the number of files composing your library. BTW, see the very first point "A library target name" of this guide: https://cmake.org/cmake/help/v3.5/command/target_link_libraries.html – Antonio May 31 '16 at 09:28