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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:37:10+00:00 2026-06-06T10:37:10+00:00

I have seen a GCC link with a C++ shared library, but I am

  • 0

I have seen a GCC link with a C++ shared library, but I am not able to reproduce it on my own. So first I create a C++ library with a testfunction:

g++ -shared -o libtest.so test.c

Then I have a test main function which calls the library function and compile it like this

gcc -o prog.out main.c -L. -ltest

Then i receive the error

undefined reference to 'testfunc'

which i think is caused by different refernce in the library … C names the function testfunc and C++ names the function [some stuff]__testfunc[maybe again some stuff].

I have also tried to use

gcc -o prog.out main.c -l:libtest.so

but this results in the same error.

Therefore, my question is: How is it possible to link a c++ library with gcc to a c file?

Update: I know i can use extern "C", but that’s not the way it is solved. Maybe there are some parameters for the linker instead?

Update2: Just thought it could also be possible that the first part is just compiled with c++ and linked with gcc. Also tried this:

g++ -c testlib.c -o testlib.o
gcc -shared -o libtest.so testlib.o
gcc -o prog.out -l:libtest.so

still doesn’t work. Is there something wrong with the flags?

  • 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-06T10:37:12+00:00Added an answer on June 6, 2026 at 10:37 am

    Yes, the problem has nothing to do with shared libraries (I think…) and everything to do with name mangling.

    In your header, you must declare the function like this:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    void testfunc(void);
    
    #ifdef __cplusplus
    }
    #endif
    

    This will cause testfunc to have the same symbol and calling conventions for both C and C++.

    On the system I’m using right now, the C symbol name will be _testfunc and the C++ symbol name (assuming you don’t use extern "C") will be __Z8testfuncv, which encodes information about the parameter types so overloading will work correctly. For example, void testfunc(int x) becomes __Z8testfunci, which doesn’t collide with __Z8testfuncv.

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

Sidebar

Related Questions

I have to link my code to a shared library without the lib prefix.
First of all, I have seen this (<- a link), and it isn't working.
I have seen examples of printing from a windows application but I have not
I have seen the Contacts in iPhone Simulator. I want to create an application
I have seen another post similar to this one on the website but the
gcc 4.4.4 c89 I have seen this in some code I am maintaining. #define
Have seen some conversations revolving around this, but hoping for some current input as
Have seen some similar questions: What is the difference between a JavaBean and a
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have seen two commonly used techniques for adding the directory of the file

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.