Search code examples
cgcccmakelinker-errorsfann

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


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

Solution

  • 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)