Question

When trying to compile my code inside QEMU using make, I get an undefined reference error during the linking phase. Afaik the configure parameters are correct, so the shared library should be used correctly for the linking, but maybe I have overlooked something.

Any help would be appreciated!

Below is a lot of additional information I already collected:


Solution: See Zach's answer

I managed it now by adding LIBS+=-lcity to the Makefile.target file.

Thanks to all!


Update:

make V=1 output:

cc -Werror -fPIE -DPIE -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
-D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef
-Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -I/home/user/cityhash
-Wendif-labels -Wmissing-include-dirs -Wempty-body -Wnested-externs
-Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers
-Wold-style-declaration -Wold-style-definition -Wtype-limits
-fstack-protector-all -I/usr/include/p11-kit-1     -I/usr/include/libpng12
-I/usr/include/pixman-1     -I../linux-headers -I..
-I/home/user/qemu/target-i386 -DNEED_CPU_H -I/home/user/qemu/include -pthread
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include   -O2
-D_FORTIFY_SOURCE=2 -g  -Wl,--warn-common -Wl,-z,relro -Wl,-z,now -pie -m64 -g
-L/home/user/lib -Wl,-rpath=/home/user/lib -lcity -o qemu-system-x86_64 [… all
*.o files … ] ../libqemuutil.a ../libqemustub.a   -lrt -pthread -lgthread-2.0
-lrt -lglib-2.0    -lutil -lrbd -lrados -lbluetooth   -lcurl   -lncurses -ltinfo
-lbrlapi  -luuid -lpng12   -ljpeg -lsasl2 -lgnutls   -lSDL   -lX11  -lz -laio
-lpixman-1   -lm

This is followed by the normal error output I've written below (except for the LINK, which is missing in this case).


Error:

~/qemu$ make
...
  LINK  x86_64-softmmu/qemu-system-x86_64
my_code.o: In function `function`:
/home/user/qemu/my_code.c:982: undefined reference to `CityHash64'
...
collect2: error: ld returned 1 exit status
make[1]: *** [qemu-system-x86_64] Error 1
make: *** [subdir-x86_64-softmmu] Error 2

Configure paramters:

./configure [...] \
    --extra-cflags='-I/home/user/cityhash' \
    --extra-ldflags='-L/home/user/lib -Wl,-rpath=/home/user/lib -lcity'

Folder structure:

~
├─ cityhash/
│  ├─ city.h
│  └─ ...
├─ lib/
│  ├─ libcity.so
│  └─ ...
└─ qemu/

Header include and function usage (my_code.c):

[...]
 11  #include "city.h"

982  hash = CityHash64(buf, len);
[...]

Nm output for the shared library:

~/lib$ nm libcity.so
[...]
0000000000000fdc T CityHash64
[...]

Strace output during make:

~/qemu$ strace -f -o ../strace.out -- make
[...]
16111 stat("/home/user/lib/libcity.so", {st_mode=S_IFREG|0755, st_size=18468, ...}) = 0
16111 open("/home/user/lib/libcity.so", O_RDONLY) = 7
16111 fcntl(7, F_GETFD)                 = 0
16111 fcntl(7, F_SETFD, FD_CLOEXEC)     = 0
16111 fstat(7, {st_mode=S_IFREG|0755, st_size=18468, ...}) = 0
16111 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2aaaaaad9000
16111 lseek(7, 0, SEEK_SET)             = 0
16111 read(7, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\7\0\0\0\0\0\0"..., 4096) = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 brk(0x2129000)                    = 0x2129000
16111 lseek(7, 12288, SEEK_SET)         = 12288
16111 read(7, "9\23\2%\25\0\2\4\1\364\0\2\4\1\2E\23\0\2\4\1\2>\23\0\2\4\1\203\0\2\4"..., 4096) = 4096
16111 lseek(7, 16384, SEEK_SET)         = 16384
16111 lseek(7, 16384, SEEK_SET)         = 16384
16111 lseek(7, 0, SEEK_SET)             = 0
16111 read(7, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\7\0\0\0\0\0\0"..., 4096) = 4096
16111 lseek(7, 12288, SEEK_SET)         = 12288
16111 read(7, "9\23\2%\25\0\2\4\1\364\0\2\4\1\2E\23\0\2\4\1\2>\23\0\2\4\1\203\0\2\4"..., 4096) = 4096
16111 lseek(7, 0, SEEK_SET)             = 0
16111 read(7, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\7\0\0\0\0\0\0"..., 4096) = 4096
16111 lseek(7, 8192, SEEK_SET)          = 8192
16111 read(7, "\6\2\233\f\7\10\0\0$\0\0\0\314\1\0\0\314\354\377\377\210\0\0\0\0A\16\20\206\2C\r"..., 4096) = 4096
16111 lseek(7, 12288, SEEK_SET)         = 12288
16111 lseek(7, 12288, SEEK_SET)         = 12288
16111 lseek(7, 12288, SEEK_SET)         = 12288
16111 lseek(7, 12288, SEEK_SET)         = 12288
16111 read(7, "9\23\2%\25\0\2\4\1\364\0\2\4\1\2E\23\0\2\4\1\2>\23\0\2\4\1\203\0\2\4"..., 4096) = 4096
16111 lseek(7, 8192, SEEK_SET)          = 8192
16111 read(7, "\6\2\233\f\7\10\0\0$\0\0\0\314\1\0\0\314\354\377\377\210\0\0\0\0A\16\20\206\2C\r"..., 4096) = 4096
16111 lseek(7, 0, SEEK_SET)             = 0
16111 read(7, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\7\0\0\0\0\0\0"..., 4096) = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
16111 lseek(7, 4096, SEEK_SET)          = 4096
[...]
16111 close(7)                          = 0
[...]
Était-ce utile?

La solution

OK, this appears to be a link ordering issue. The key bits of the link command line are:

-g -L/home/user/lib -Wl,-rpath=/home/user/lib -lcity -o qemu-system-x86_64 

and then

[... all *.o files ...] ../libqemuutil.a ../libqemustub.a 
-lrt -pthread -lgthread2.0 [... more libraries ...]

The problem is that -lcity needs to come after all of the .o files on the command line, because order matters: libraries are only searrched for unresolved symbols from object files that precede them on the command line.

This is probably an Automake-generated Makefile, which means that you need to get -lcity into LDADD for the offending link operation, rather than LDFLAGS. It may be possible to accomplish this with an --extra-libs=-lcity configure option (remove -lcity from --extra-ldflags, but keep everything else you have in there). If not, try make LDADD=-lcity. If that doesn't work (it has fairly high odds of clobbering other stuff that needs to be in LDADD) you are going to have to dig into the generated Makefile (it won't be apparent from Makefile.am) and see if there's another variable you can set on the command line.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top