Jump to content
  • Advertisement
Sign in to follow this  

exposing singleton in dynamically loaded library

This topic is 4747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I expose internal functions/globals (i.e my singleton)?

void Foo();
void Bar();

void Foo()
	cout << "Foo()\n";

void Bar()
	cout << "Bar()\n";

int main()
	// open module
	void *module = dlopen("./hello.so", RTLD_LAZY);
		cerr << "error in open: " << dlerror() << "\n";

	// can this next 4 lines be done in a simpler way?
	typedef void (*Function)();
	Function function;

	void *pointer = dlsym(module, "hello");
	function = *reinterpret_cast<Function*>(&pointer);

	// check for errors
	const char *error = dlerror();
		cerr << "fatal error: " << error << "\n";

	// call the function

	// close the module

	return 0;

#include <iostream>
using namespace std;

void Foo();

extern "C"

// i think this needs to be wrapped in extern "C"
void hello()
	cout << "hello\n";


genjix@linux:~/media/tmp/obj/dl> ./a.out
error in open: ./hello.so: undefined symbol: _Z3Foov

Share this post

Link to post
Share on other sites
I might be wrong cause I haven't used dynamically loaded libraries often...

First, why are you defining Foo() and Bar() in what seems to be your main application? Aren't you trying to load these from the library?

also, in your .so, is there some missing code?

You define void Foo(), but never implement it...

Might be the reason of your error.

Hope that helps


[Edit: typos]

Share this post

Link to post
Share on other sites
well, i'm trying to define code inside my dynamically loaded library that can call functions within my main base code (so hello() can access data and call methods from within main code).

I have no trouble loading the library but I can't expose any of the main.cpp functions. I know its possible because I've seen references to it all over the net.

Share this post

Link to post
Share on other sites
This sounds weird but it helped me once: try putting extern "C" in front of every function instead of using the block syntax.

Linux also has a command nm that shows a binary's symbols.

Share this post

Link to post
Share on other sites
extern "C" void hello() does not help :(

genjix@linux:~/media/tmp/obj/dl> echo -en "[hello.so]\n\n" > log && nm hello.so>> log
genjix@linux:~/media/tmp/obj/dl> echo -en "[a.out]\n\n" >> log && nm a.out >> log


00001b50 A __bss_start
00000780 t call_gmon_start
00001b50 b completed.1
00001a2c d __CTOR_END__
00001a24 d __CTOR_LIST__
U __cxa_atexit@@GLIBC_2.1.3
w __cxa_finalize@@GLIBC_2.1.3
00000920 t __do_global_ctors_aux
000007b0 t __do_global_dtors_aux
00001b44 d __dso_handle
00001a34 d __DTOR_END__
00001a30 d __DTOR_LIST__
00001a3c A _DYNAMIC
00001b50 A _edata
00001b58 A _end
00000954 T _fini
00000820 t frame_dummy
00000a20 r __FRAME_END__
00000904 t _GLOBAL__I_x
w __gmon_start__
U __gxx_personality_v0@@CXXABI_1.2
00000868 T hello
0000072c T _init
00001a38 d __JCR_END__
00001a38 d __JCR_LIST__
w _Jv_RegisterClasses
00001b48 d p.0
000008ec t __tcf_0
00001b4c D x
U _Z3Foov
000008ae t _Z41__static_initialization_and_destruction_0ii
U _ZNSolsEi@@GLIBCPP_3.2
0000097c V _ZNSt15basic_streambufIcSt11char_traitsIcEE13_S_pback_sizeE
00000980 V _ZNSt15basic_streambufIwSt11char_traitsIwEE13_S_pback_sizeE
U _ZNSt8ios_base4InitC1Ev@@GLIBCPP_3.2
U _ZNSt8ios_base4InitD1Ev@@GLIBCPP_3.2
U _ZSt4cout@@GLIBCPP_3.2
00001b54 b _ZSt8__ioinit
U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCPP_3.2

08049d94 A __bss_start
08048784 t call_gmon_start
08049eb4 b completed.1
08049c54 d __CTOR_END__
08049c4c d __CTOR_LIST__
U __cxa_atexit@@GLIBC_2.1.3
08049d88 D __data_start
08049d88 W data_start
U dlclose@@GLIBC_2.0
U dlerror@@GLIBC_2.0
U dlopen@@GLIBC_2.1
U dlsym@@GLIBC_2.0
08048aa0 t __do_global_ctors_aux
080487b0 t __do_global_dtors_aux
08049d8c D __dso_handle
08049c5c d __DTOR_END__
08049c58 d __DTOR_LIST__
08049c64 D _DYNAMIC
08049d94 A _edata
08049ebc A _end
U exit@@GLIBC_2.0
08048ac4 T _fini
08049c4c A __fini_array_end
08049c4c A __fini_array_start
08048ae0 R _fp_hw
080487f0 t frame_dummy
08048c48 r __FRAME_END__
080489b4 t _GLOBAL__I__Z3Foov
w __gmon_start__
U __gxx_personality_v0@@CXXABI_1.2
08048a98 T __i686.get_pc_thunk.bx
0804867c T _init
08049c4c A __init_array_end
08049c4c A __init_array_start
08048ae4 R _IO_stdin_used
08049c60 d __JCR_END__
08049c60 d __JCR_LIST__
w _Jv_RegisterClasses
080489d0 T __libc_csu_fini
08048a40 T __libc_csu_init
U __libc_start_main@@GLIBC_2.0
08048858 T main
08049d90 d p.0
08048760 T _start
0804899c t __tcf_0
0804883a T _Z3Barv
0804881c T _Z3Foov
0804895e t _Z41__static_initialization_and_destruction_0ii
08048b28 V _ZNSt15basic_streambufIcSt11char_traitsIcEE13_S_pback_sizeE
08048b2c V _ZNSt15basic_streambufIwSt11char_traitsIwEE13_S_pback_sizeE
U _ZNSt8ios_base4InitC1Ev@@GLIBCPP_3.2
U _ZNSt8ios_base4InitD1Ev@@GLIBCPP_3.2
08049e28 B _ZSt4cerr@@GLIBCPP_3.2
08049d98 B _ZSt4cout@@GLIBCPP_3.2
08049eb8 b _ZSt8__ioinit
U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCPP_3.2

as far as I can tell, it cannot resolve the _Z3Foov symbol :(

Share this post

Link to post
Share on other sites
I'd try with extern "C" for foo() too, I'm no expert in this area, but it might be the name mangling that foo() has that makes it incompatible with the call in the extern "C" section?

Anyway, really I can't help much than that, wish I helped

Good luck

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!