Dlsym () 'global variable in C ++

I want to create a program that can dlopen()create a series of libraries (written by me) and run all the functions stored in a global variable test_suiteinside this .so file, which is a NULL-complete array of function pointers (function signatures are predefined by itself, no need to worry about it )

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

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

and the "function index" is declared and distributed statically as:

const testunit_testcase test_suite = { ... }

till

objdump -t foo.so  | grep test_suite

shows:

0000000000200940 l     O .data.rel.ro   0000000000000020              _ZL10test_suite

I need

0000000000200940 l     O .data.rel.ro   0000000000000020              test_suite

Therefore I can dlsym(dlh, "test_suite")in the program dlopen()'ingfoo.so

thank


Adding

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

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

I use:

g++ -v . COLLECT_GCC = g++ COLLECT_LTO_WRAPPER =/USR/Library/GCC/x86_64--Linux-/4.5.2/LTO- : x86_64-unknown-linux-gnu : /build/src/ gcc-4.5-20110127/configure --prefix =/usr --enable-languages ​​= c, ++, fortran, objc, obj-++, ada --enable-shared --enable-threads = posix --enable -__ cxa_atexit --enable-clocale = gnu --enable-gnu-unique-object --enable-lto --enable-plugin --enable-gold - -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 : posix gcc 4.5.220110127 (preerelease) (GCC)


2

-

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

, :

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

. , extern "C" { ... } . - , , , test_suite , , 4.x( ) g++?

+5
4

. (, , : .) , "const" , . , extern. " , -seq , (3.1); , , extern (7.1.1) ". , , ( , ), : , ; , extern. :

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

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

extern, , "" "const".

+8

x.cxx:

 extern "C"
 {
    extern const int test_suite[] = { 0 };
 }

:

~/ec% g++ -Wall -rdynamic -shared x.cxx -o x.so
~/ec% objdump -t x.so | grep test_suite
00000444 g     O .rodata        00000004              test_suite

extern test_suite, . , const static. , , extern "C" "" . , gcc . - ?

+1

++, . gcc ( )

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

extern "C" .

0

static.

staticmeans that the variable is local to the current compilation unit, therefore g ++ changes its name. If you want a variable to be accessible "outside", you should not do it static. So, in the .cpp file, define the variable as follows:

const testunit_testcase test_suite = { ... }

If you also declare a variable in the corresponding .h file, make this declaration extern:

extern const testunit_testcase test_suite;
-1
source

All Articles