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 6807349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:49:17+00:00 2026-05-26T19:49:17+00:00

Why am I getting an undefined reference error using gcc? I am trying to

  • 0

Why am I getting an “undefined reference” error using gcc?

I am trying to create a shared object (.so) that exports one function, “external()”. I then try to link against the .so but get “undefined reference ‘external'”. What am I doing wrong here?

File: external.c

int external() {
    return 5;
}

File: program.c

int external();
int main(char** argv, int* argc) {
    return external();
}

Commands:

$ gcc -fPIC -c external.c
$ gcc -shared -o libexternal.so external.o
$ gcc -L. -lexternal -o program program.c
/tmp/cc3MmhAE.o: In function `main':
program.c:(.text+0x7): undefined reference to `external'
collect2: ld returned 1 exit status

I can even run nm and see that the .so is defining ‘external’:

Command:

$ nm libexternal.so | grep external
0000040c T external

What am I missing here?

  • 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-05-26T19:49:18+00:00Added an answer on May 26, 2026 at 7:49 pm

    Recent versions of gcc/ld default to linking with --as-needed.

    This means if you write -lexternal before the C file the library will automatically get excluded (the order matters when testing if things are “needed” like this)

    You can fix this with either of:

    • gcc -L. -o program program.c -lexternal
    • gcc -L. -Wl,--no-as-needed -lexternal -o program program.c

    The latter of which passes --no-as-needed to the linker, which would cause the library to still be linked, even if you didn’t call external() from it.

    Note: -Wl,--no-as-needed isn’t applied globally to everything that’s linked, it’s only applied to things that follow it in the command line order. So -lexternal -Wl,--no-as-needed also wouldn’t work. This does mean that you can mix and match behaviours though, for example gcc -L. -Wl,--no-as-needed -lexternal -Wl,--as-needed -o program program.c -lmightneed would always link against external, but only link against mightneed if one or both of program.c/libexternal.so caused it to be needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting a Can't use an undefined value as a HASH reference error trying
I am getting an error message that says undefined reference to encrypt(int, std::list<int, std::allocator<int>
I'm getting an Undefined reference error message, on this statement: GlobalClass *GlobalClass::s_instance = 0;
Possible Duplicate: Undefined reference - C++ linker error Now, I'm getting an Undefined reference
Getting this error while compiling C++ code: undefined reference to `__stack_chk_fail' Options already tried:
So I am getting the error: "undefined reference to sem_open()" even though I have
getting error: neljastest.cpp: undefined reference to Vector2::Vector2(float, float) neljastest.cpp: #include <cstdlib> #include <iostream> #include
I'm trying to create a very simple function in c++ however I keep getting
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
I've been getting this undefined symbol building with this command line: $ gcc test.cpp

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.