Sign in to follow this  
Silent Angel

loading shared libraries (linux)

Recommended Posts

Hi folks I created a shared library (.so) and put it on /usr/local/lib. On MinGW, I went in Project -> Settings and called it there... It compiles everything ok but the program simply doesn't run. I'm quite a newbie in programming on linux and I couldn't figure out what's going on... :-( Does anyone know?

Share this post


Link to post
Share on other sites
If you want to be able to use libraries in /usr/local/lib, either add that directory to ld.so.conf OR put it in the env variable LD_LIBRARY_PATH

ldconfig is only necessary if you did the former (maybe not even then)

Mark

Share this post


Link to post
Share on other sites
Quote:
Original post by AndiDog
Hm, MinGW means "Minimal GNU for Windows", so do you run it on Linux or what?

( Do you mean Dev-C++ 5 ??? )


It works under linux too

Quote:
Original post by Roboguy
Does the library file name begain with 'lib'? If not, add it to the front.


yes, it does

Quote:
Original post by hplus0603
On linux, you also want to run ldconfig (as root) after installing new shared libraries. Also make sure /usr/local/lib is available in your ldconfig, and in your library path.


Ok, it's done now. I put /usr/local/lib in the ld.so.conf file and ran ldconfig. But it can't find my .h files yet. Before that, I had created a libFile.so and a libFile.a and put both into /usr/local/lib. (are these the only files I need?)

I'm declaring my libraries like this -> #include "scene.h" (is this the correct way? I tried also this #include "File/scene.h" but it didn't work either)

Is there any step-by-step tutorial about how to create and use a library? I couldn't find one

Thanks

Share this post


Link to post
Share on other sites
take this

#include <dlfcn.h>

#include <iostream>
using namespace std;

// calling Foo demonstrates calling internal functions
extern "C" void Foo();
void Bar();

// Foo() calls Bar()
extern "C" void Foo()
{
cout << "Foo()\n";
Bar();
}

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

template<class T>
T function_cast(void *symbol)
{
assert(sizeof(void *) == sizeof(T));
union
{
void *symbol;
T function;
}cast;
cast.symbol = symbol;
return cast.function;
}

int main()
{
// open module
void *module = dlopen("./hello.so", RTLD_NOW|RTLD_GLOBAL);
if(!module)
{
cerr << "error in open: " << dlerror() << "\n";
exit(-1);
}

// 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();
if(error)
{
cerr << "fatal error: " << error << "\n";
exit(-1);
}

// call the function
function();

// close the module
dlclose(module);

return 0;
}




then this

#include <iostream>
using namespace std;

// look we can reference mangled variables! (below)
int x = 110;

extern "C"
{

void Foo();

void hello()
{
cout << "hello " << x << "\n";
Foo();
}

}




compile like this

genjix@linux:~/media/tmp/obj/dl> g++ dynamic.cpp -ldl -export-dynamic
genjix@linux:~/media/tmp/obj/dl> g++ -o hello.so -shared hellocc.cpp
genjix@linux:~/media/tmp/obj/dl> ./a.out
hello 110
Foo()
Bar()
genjix@linux:~/media/tmp/obj/dl>


where dynamic.cpp is the library loader (first code listing) and hellocc.cpp is the module (second).

Share this post


Link to post
Share on other sites
ok you make an extern c function that returns a pointer to your class

#include "objectvirtualbase.h"

class Object : public ObjectVirtualBase
{
// ...
};

extern "C" ObjectVirtualBase *CreateObject(ResourceTree params)
{
return new ObjectVirtualBase();
}



as for implementation, well you implement this in the module (above).

Share this post


Link to post
Share on other sites
Quote:
Original post by Silent Angel
Quote:
Original post by AndiDog
Hm, MinGW means "Minimal GNU for Windows", so do you run it on Linux or what?

( Do you mean Dev-C++ 5 ??? )


It works under linux too
Yes, but why use a Windows port of something originally made for UNIX variants on a UNIX variant?

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
Yes, but why use a Windows port of something originally made for UNIX variants on a UNIX variant?


MingwStudio is an IDE for win and linux, uses gcc on linux. I think he means that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this