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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:41:11+00:00 2026-05-20T23:41:11+00:00

I want to make a program which is able to dlopen() a series of

  • 0

I want to make a program which is able to dlopen() a series of libraries (written by myself) and run all the functions stored in a global variable called test_suite inside that .so file, which is a NULL-terminated array of function pointers (the functions’ signatures are predefined by myself, no need to worry about that).

The problem is g++ mangles that variable. The library is compiled as:

g++ -Wall -shared -rdynamic -fPIC foo.cpp -o foo.so

and the “function index” is declared and allocated statically as:

const testunit_testcase test_suite = { ... }

yet

objdump -t foo.so  | grep test_suite

shows:

0000000000200940 l     O .data.rel.ro   0000000000000020              _ZL10test_suite

What I need is

0000000000200940 l     O .data.rel.ro   0000000000000020              test_suite

So I can dlsym(dlh, "test_suite") in the program dlopen()‘ing foo.so

Thanks


Addendum

Yes, extern "C" was the first thing I’ve tried:

extern "C" {
        const testunit_testcase test_suite[] = { 
                //TESTUNIT_DEF_TESTCASE(doTest),
                {NULL, NULL},
        };  
}

I am using:

g++ -v Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.5.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with:
/build/src/gcc-4.5-20110127/configure
–prefix=/usr –enable-languages=c,c++,fortran,objc,obj-c++,ada
–enable-shared –enable-threads=posix –enable-__cxa_atexit –enable-clocale=gnu –enable-gnu-unique-object –enable-lto –enable-plugin –enable-gold –with-plugin-ld=ld.gold –disable-multilib –disable-libstdcxx-pch –with-system-zlib –with-ppl –with-cloog –with-cloog-include=/usr/include/cloog-ppl
–libdir=/usr/lib –libexecdir=/usr/lib –mandir=/usr/share/man –infodir=/usr/share/info Thread model: posix gcc version 4.5.2
20110127 (prerelease) (GCC)


Addendum 2

For whatever reasons

extern "C" {
     const testunit_testcase test_suite = { ... }
}

does not work, BUT this one does:

extern "C" const testunit_testcase test_suite = { ... }

My question now: As I can see in some of your answers, enclosing extern "C" { ... } works for you. Are there any compiler flags I could use to make sure that test_suite will never be mangled, no matter what 4.x (at least) g++ version is used?

  • 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-20T23:41:12+00:00Added an answer on May 20, 2026 at 11:41 pm

    The problem isn’t one of name mangling. (Or probably isn’t: public
    variable names are not usually mangled.) The real problem is that the
    “const” means implicit static, rendering the variable invisible outside
    the translation unit. To avoid this, the variable must be explicitly
    declared extern. And “The form of linkage-specification that contains
    a brace-enclosed declaration-seq does not affect whether the contained
    declarations are definitions or not (3.1); the form of
    linkage-specification directly containing a single declaration is
    treated as an extern specifier (7.1.1) for the purpose of determining
    whether the contained declaration is a definition.” Which, while it
    doesn’t seem to address your issue directly (the presence of an
    initializer ensures that the declaration is a definition), it does seem
    to indicate the intent: within a brace enclosed linkage specifier, the
    usual rules apply; if the linkage specifier applies directly to the
    declaration, it’s as if the declaration were explicitly extern. So you
    can write either:

    extern "C" {
        testunit_testcase const test_suite[] // ...
    }
    

    or

    extern "C" testunit_testcase const test_suite[] // ...
    

    But there must be an extern which applies explicitly to the definition,
    in order to override the implicit “static” of “const”.

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

Sidebar

Related Questions

I want to make a C# program that can be run as a CLI
I want to make a program for Android, what take all people who use
I have a little program that I want to make open automatically when my
I want to make sure that a set of functions have the same signature
I want to make an etag that matches what Apache produces. How does apache
I want to make a table in SqlServer that will add, on insert, a
I want to make a copy of an ActiveRecord object, changing a single field
I want to make an entity that has an autogenerated primary key, but also
I want to make a .NET Form as a TopMost Form for another external
I want to make sure people can't type the name of a PHP script

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.