Question

I am a newbie in Linux gcc. I am writing a simple code to learn the weak attribute in Linux gcc.

See my sample code:

weakref.c, the main file. I want to the file could work with or without foo method being defined.

#include <stdio.h>
extern void foo(void) __attribute__((weak));

int main() {
  if (foo){
    foo();
    printf ("foo is defined\n");
  } else {
    printf("foo is not defined\n");
  }
}

so, I run the following command to compile it and run it:

gcc weakref.c -o main_static
./main_static

and the output is "foo is not defined", which is what I expected.

Then I created a new file libfoo.c, see below:

#include <stdio.h>

void  foo() {
  printf("Print in foo.\n");
}

I attempted 3 ways to try to make main file work with the libfoo.c:

  1. Compile the libfoo.c and weakref.c and link the object files.
  2. Compile the libfoo.c as a static library, and link it with the object file of weakref.c
  3. Compile the libfoo.c as a shared library, and link it with the object file of weakref.c

Only the 3rd way works and get the following output:

Print in foo.
foo is defined

Could you please let me know if the weak ref only works with a shared library, and why? Thanks a lot!

Was it helpful?

Solution

let me know if the weak ref only works with a shared library

Not at all.

Both 1. and 3. are expected to produce "foo is defined".

The 2. is not expected to pull libfoo.o from libfoo.a, because unresolved weak reference from weakref.o does not count as unresolved symbol for the purpose of pulling an object from an archive library. However, you can force libfoo.o to be pulled into the link like so: gcc weakref.o -lfoo -ufoo, and then you should again get "foo is defined".

Now that I stated what should happen, let's see if the practice agrees. Transcript from a Linux/x86_64 system, using gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3 and GNU ld (GNU Binutils for Ubuntu) 2.20.1-system.20100303:

$ gcc -c weakref.c libfoo.c
$ ar ruv libfoo.a libfoo.o
ar: creating libfoo.a
a - libfoo.o
$ gcc weakref.o && ./a.out
foo is not defined

This is your #1:

$ gcc weakref.o libfoo.o && ./a.out
Print in foo.
foo is defined

As you can see #1 "works" as I expected, and not as you claimed. You must have made some mistake somewhere.

This is your #2:

$ gcc weakref.o -L. -lfoo && ./a.out
foo is not defined

Your #2 again, but forcing libfoo.o to be pulled into the link:

$ gcc weakref.o -L. -lfoo -ufoo && ./a.out
Print in foo.
foo is defined
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top