loading shared libraries (linux)
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?
Hm, MinGW means "Minimal GNU for Windows", so do you run it on Linux or what?
( Do you mean Dev-C++ 5 ??? )
( Do you mean Dev-C++ 5 ??? )
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.
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
ldconfig is only necessary if you did the former (maybe not even then)
Mark
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
take this
then this
compile like this
where dynamic.cpp is the library loader (first code listing) and hellocc.cpp is the module (second).
#include <dlfcn.h>#include <iostream>using namespace std;// calling Foo demonstrates calling internal functionsextern "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-dynamicgenjix@linux:~/media/tmp/obj/dl> g++ -o hello.so -shared hellocc.cppgenjix@linux:~/media/tmp/obj/dl> ./a.outhello 110Foo()Bar()genjix@linux:~/media/tmp/obj/dl>
where dynamic.cpp is the library loader (first code listing) and hellocc.cpp is the module (second).
I still don't get it :-(
I'm using classes... is it the same thing I should do? just put extern C before my methods implementations?
I'm using classes... is it the same thing I should do? just put extern C before my methods implementations?
ok you make an extern c function that returns a pointer to your class
as for implementation, well you implement this in the module (above).
#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).
Quote:Original post by Silent AngelYes, but why use a Windows port of something originally made for UNIX variants on a UNIX variant?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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement