Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:46:00+00:00 2026-06-07T03:46:00+00:00

I am a newbie in Linux gcc. I am writing a simple code to

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:46:03+00:00Added an answer on June 7, 2026 at 3:46 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie to C. I'm writing an ANSI C program (on Linux using gcc 4.4.6)
I'm complete Linux newbie, but still want to provide a simple way for Linux
hey I am newbie to scripting in linux.I want to take a sqldump of
I am newbie on the Linux signals, please help. The following code get core
Hello I am newbie in kernel dev, Created a simple program: #include <linux/module.h> #include
i am a newbie to assembly and program in c (use GCC in Linux)
I am a newbie in GCC and Linux. I have been using Visual Studio
I have a newbie question for writing makefile for java in Linux I have
I'm a newbie to linux scripting and am having an issue with a script
I'm a linux newbie coming from a Windows background and I am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.