Error “undefined reference to `sin'” when compiling (with -lm)

本秂侑毒 提交于 2019-12-10 09:22:36

问题


I've downloaded and compiled: http://leenissen.dk/fann/wp/

  • cmake version 2.8.11.2
  • gcc (Ubuntu/Linaro 4.8.1-10ubuntu8) 4.8.1

Command used to compile:

cmake -D CMAKE_INSTALL_PREFIX:PATH=/usr .

Installation:

sudo make && sudo make install

Then I go to examples/ directory inside fann project and try to compile examples by running:

make all

I'm getting an error:

gcc -O3 xor_train.c -o xor_train -lfann -lm
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `sin'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `exp'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `cos'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `log'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `pow'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `sqrt'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so: undefined reference to `floor'
collect2: error: ld returned 1 exit status
make: *** [xor_train] Error 1

Update:

  • I've followed an instruction given by a library
  • I've checked on another machine and provided instruction works as intended so I guess my environment is in a some way misconfigured.

Some more info about shared library dependencies:

ldd /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libfann.so
    linux-vdso.so.1 =>  (0x00007fff3abfe000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6f3997c000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f6f39f84000)

As suggested by @michael-burr compiled with -Wl,-v

/usr/bin/ld --sysroot=/ \
  --build-id --eh-frame-hdr -m elf_x86_64 \
  --hash-style=gnu --as-needed \
  -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
  -z relro -o xor_train \
  /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o \
  /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o \
  /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o \
  -L/usr/lib/gcc/x86_64-linux-gnu/4.8 \
  -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu \
  -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib \
  -L/lib/x86_64-linux-gnu \
  -L/lib/../lib -L/usr/lib/x86_64-linux-gnu \
  -L/usr/lib/../lib \
  -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. \
  -v /tmp/cc0AHZgU.o -lfann -lm -lgcc --as-needed -lgcc_s --no-as-needed \
  -lc -lgcc --as-needed -lgcc_s --no-as-needed \
  /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o \
  /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
GNU ld (GNU Binutils for Ubuntu) 2.23.52.20130913

回答1:


Root cause: missing dependencies in FANN library (Will send a patch to author). Such a dependency is called "inter library dependency".

It may happen that one build a shared library A and doesn't have correct dependencies set (let's say B). In such a case a shared library A will be build without any error msg as it's not required to provide implementation during compiling.

The problem will appear as a lack of library B when one try to create an executable file which depends on A.

In this specific case a solution is to modify a CMake configuration file according to CMake manual

Example changeline:

TARGET_LINK_LIBRARIES(fann m)



回答2:


It looks like you're compiling your own program as 64-bit, but the FANN library is 32-bit. You might need to specify an architecture for FANN when you compile, which might mean modifying GCC flags in the makefile, unless there are autoconf settings to do it for you. Assuming that you want 64-bit FANN.

Alternatively, you could specify 32-bit architecture when you compile your own code.



来源:https://stackoverflow.com/questions/19693753/error-undefined-reference-to-sin-when-compiling-with-lm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!