0

These both questions did not solve my problem: glib-undefined-reference-to-anything-from-glib and undefined-reference-to-pthread-create

I am writing a personalized version of the snprintf function from glibc-2.21. When I want to link my main.o which is calling the new function snpprintf() then I receive dozens of undefined reference to errors from linker for each call/reference of a glibc-2.21 function.

This is the command:

ar rvs libFaFsprintf.a snpprintf.o vfpprintf.o vsnpprintf.o iovspprintf.o
gcc main.o libFaFsprintf.a -lc -o main

I have my object files in the library libFafsprintf.a - it does not matter if I provide the library or I specify each object file. The source code is compiled without any problem. I tried also to change the order of my library and -lc - nothing helps.

It only works when I use the -static option but I do not want to link libc statically with the executable.

How do I have to link the program?

Community
  • 1
  • 1
Peter VARGA
  • 4,780
  • 3
  • 39
  • 75
  • Could it be that your glib architecture doesn't match that of your objects? – n. m. could be an AI Feb 26 '15 at 12:48
  • When I link it statically then it works. This means that far all is OK. – Peter VARGA Feb 26 '15 at 12:49
  • No, when you link statically you are using a physically different library. – n. m. could be an AI Feb 26 '15 at 12:53
  • OK. This I did not know. My personalized version of `snprintf()` is a copy from the GNU glib-2.2 package. I link it with glib-2.0 - can this be the problem? But why it works when linked as static. How can I checked which library is used when linking statically? – Peter VARGA Feb 26 '15 at 12:56
  • To troubleshoot, 1. Use a full path to `libglib-2.x.y.so` instead of `-lglib-2.0`. 2. `nm | grep `, what do you see? – n. m. could be an AI Feb 26 '15 at 12:58
  • 1) adding the full path does not change anything. in my system is only one version of libglib-2.0.so 2) When running with path-to-glib then I get `nm: /usr/lib64/libglib-2.0.so: no symbols` – Peter VARGA Feb 26 '15 at 13:05
  • Your glib is stripped and cannot be linked with. You need to install the development package of glib. Look at your package manager, search for packages ending in -dev or -devel. – n. m. could be an AI Feb 26 '15 at 13:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71780/discussion-between-al-bundy-and-n-m). – Peter VARGA Feb 26 '15 at 13:11
  • I think you're confusing GLib with glibc (see https://en.wikipedia.org/wiki/GLib and https://en.wikipedia.org/wiki/GNU_C_Library). snprintf is not in glib. – nemequ Feb 26 '15 at 19:37
  • @nemequ: Yes, you are totally right! Can you tell me which library I have to provide the linker to compile it - **NOT** static. – Peter VARGA Feb 26 '15 at 19:54

1 Answers1

0

How do I have to link the program?

Like this:

gcc main.o libFaFsprintf.a -lglib-2.0 -o main
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • as user nemequ realized, I mismatched in the 1st version of the question glib with libc. Usually I do not need to provided the standard C library for the link process – Peter VARGA Mar 01 '15 at 18:19