GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Apr 18, 2014 3:50 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to use Glibmm's Module class properly?
PostPosted: Tue Jan 01, 2013 4:54 pm 
Offline

Joined: Tue Jan 01, 2013 4:35 pm
Posts: 2
Hi folks!

I'm using Glibmm on Linux for my project, which needs to load plugins and execute functions from within those. However, I was having problems with it never finding the symbol I was after. Given that the project is somewhat complicated, I made a simple test-case, but even then it failed to find the symbol.

Here's the test-case program:

Code:
#include <glibmm.h>
#include <iostream>
#include <string>

int main(int argc, char **argv)
{
   //Load a module from the argument given to the program.
   Glib::Module *test = new Glib::Module(std::string(argv[1]));

   //Log an error if the module fails to load.
   if(!test) std::cout << Glib::Module::get_last_error() << std::endl;

   //Here's out function pointer.
   void (*testptr)() = NULL;

   //Here's our problem. From what I can tell, this should load the symbol into testptr. What actually happens is that the symbol is 'undefined' according to Glib.
   if(!test->get_symbol(std::string("test"), (void *&)testptr)) std::cout << Glib::Module::get_last_error() << std::endl;

   return 0;
}


..and here's the test-case module:

Code:
//NOTE: I know there should be a G_MODULE_EXPORT here, but a) I couldn't find the C++ equivalent, and b) the manual says it's a no-op on *nix, so it shouldn't matter anyway.
void test()
{
}


Perhaps somebody can tell me what I'm doing wrong?

Feedback on code style/Glibmm idioms is also appreciated.


Top
 Profile  
 
 Post subject: Re: How to use Glibmm's Module class properly?
PostPosted: Tue Jan 01, 2013 5:18 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK
Hello,

What has happened is that you have come across one of the many differences between C and C++. Under C++ all symbols are name mangled to allow overloading of operators and functions, unless otherwise specified not to.

So in you example the function void test() could be name mangled to "v_test_v" or even "v4testv" and maybe "v4test". The method get_symbol() is looking for an exact match which is not possible to portably do under C++.

As stated before it is possible to specify not to name mangle a function by using extern "C", so your module becomes
Code:
//NOTE: I know there should be a G_MODULE_EXPORT here, but a) I couldn't find the C++ equivalent, and b) the manual says it's a no-op on *nix, so it shouldn't matter anyway.
extern "C" void test()
{
}
This does mean that your code acts more as C code and less like C++.

_________________
E.


Top
 Profile  
 
 Post subject: Re: How to use Glibmm's Module class properly?
PostPosted: Tue Jan 01, 2013 5:26 pm 
Offline

Joined: Tue Jan 01, 2013 4:35 pm
Posts: 2
errol wrote:
What has happened is that you have come across one of the many differences between C and C++. Under C++ all symbols are name mangled to allow overloading of operators and functions, unless otherwise specified not to.


Wonderful!

Thanks, I'll give this a go and post back if I have any problems, but I don't forsee any. Thanks for your help!

EDIT: Nope, this works perfectly. Thanks again for your time!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group