2

I am using CMake to build my project. It shows the error

./../x86_64-conda_cos6-linux-gnu/bin/ld: /home/lee/bin/ants/lib/libitkgdcmopenjp2-5.0.a(tcd.c.o): in function 'opj_tcd_init_encode_tile': tcd.c:(.text+0x1d42): undefined reference to '__pow_finite'

I googled and it said that I have to include extra libs that is the math lib lm. I have tried two ways:

  1. Build with cmake CMAKE_SHARED_LINKER_FLAGS="-lm" .. However, it is not work

  2. Add lm in the CMakeLists.txt: target_link_libraries(lm myprogram ${ITK_LIBRARIES}). But it shows the error Cannot specify link libraries for target "lm" which is not built by this project.

Could you tell me any suggestion to fix it?

Kevin
  • 16,549
  • 8
  • 60
  • 74
KimHee
  • 728
  • 2
  • 12
  • 22

1 Answers1

1
target_link_libraries(lm myprogram ${ITK_LIBRARIES})

You got the order wrong. This asks to link lm with the libraries libmyprogram and whatever's contain in ITK_LIBRARIES.


Try:

target_link_libraries(myprogram m)

Which asks cmake to link myprogram with libm. This implies libm is installed in a system directory. If it is not, you must either:

YSC
  • 38,212
  • 9
  • 96
  • 149
  • Thanks YSC. I can build cmake sucessful but It show error in the make . I think we should add one command to find `lm` lib. This is error when run make `/home/lee/anaconda3/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld: cannot find -llm` – KimHee Sep 23 '19 at 14:37
  • The name of the library is `m`. Therefore it is `target_link_libraries(myprogram m)` – Thomas Sablik Sep 23 '19 at 14:42
  • The `l` (L) in `lm` is a part of the linker arguments syntax that is not necessary. `m` is the name of the library so for CMake `target_link_libraries(myprogram m ...)` is sufficient. – Romen Sep 23 '19 at 14:42
  • Thanks. I tried the solution in https://stackoverflow.com/questions/32816646/can-cmake-detect-if-i-need-to-link-to-libm-when-using-pow-in-c But it does not work.Note that, the libm is located at `/lib/x86_64-linux-gnu/libm.so.6` – KimHee Sep 23 '19 at 14:52