Jump to content
  • Advertisement
Sign in to follow this  
methinks

Problem with dynamic objects using dlsym

This topic is 1201 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

I'm trying to learn how to write plugins (OSX Yosemite).

Everything compiles and runs properly, the shared object is loaded, but function in the shared object doesn't execute.

 

All the files remain in the same directory.

 

The host application, compiled as follows:

g++ -ldl -o Host Host.cpp

// Host.cpp
 
#include <iostream>

#include <dlfcn.h>



using namespace std;



int main(int argc, char* argv[])

{

    void * plugin = dlopen("Plugin.mod", RTLD_NOW);



    if(plugin == nullptr)

    {

        cout << "Error opening file" << endl;

    }

    else

    {

        cout << "File open, attempting to access function" << endl;

        dlerror();

        dlsym(plugin, "run"); // This is where the line "Plugin Running" should be printed to the console

        char * errMsg = dlerror();

        if(errMsg)

        {

            cout << "Unable to access function" << endl;

        }

        else

        {

            cout << "Success: " << endl;

        }

        dlclose(plugin);

    }

}

The plugin that gets called

g++ -fPIC -c Plugin.cpp

g++ -shared -o Plugin.mod Plugin.o

// Plugin.cpp
#include <iostream>



using namespace std;



extern "C" void run()

{

    cout << "Plugin running" << endl;

}

and the output:

File open, attempting to access function
Success:

 

Is this a problem with the code, or the compilation?

Share this post


Link to post
Share on other sites
Advertisement

I think it's because you need to export the function.

On Windows :

__declspec( dllexport )

On other platform :

__attribute__ ( ( __visibility__ ( "default" ) ) )

EDIT:  On this case you normally have a compile error.

Edited by Alundra

Share this post


Link to post
Share on other sites

You can also use this way (more c++, T is the type of the function of course) :

typedef T* (*PtrFunc)();
PtrFunc RunFunction = reinterpret_cast<PtrFunc>(dlsym(plugin, "run"));
Edited by Alundra

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!