1. Dynamic Link library Items
1.1linux Eclipse Create a dynamic link library project
Create a Project new->project->c++ project selects Shared Library->empty project. Enter the project name "Your Project", click Finish to complete the project creation.
1.2 Hello Project Project source code:
* libhello.so
* HelloWorld.h * * Created on:2013 Year December 26
* Author:ron Tang
* * * ifndef helloworld_h_
#define Helloworld_h_
extern "C" void Hello ();
#endif/* Helloworld_h_ * *
* * * libhello.so * HelloWorld.cpp * * * * Created on:2013 Year December 26
* Author:ron Tang
* *
#include <iostream>
using Std::cout;
Using Std::endl;
extern "C" void Hello () {
cout<< "Hello World" <<endl;
}
1.3 Errors that you may encounter when compiling a dynamic link library
Building target:libHello.so
INVOKING:GCC C + + Linker
g++-shared-o "libhello.so"./SRC/HELLOWORLD.O
/USR/BIN/LD:./src/helloworld.o:relocation r_x86_64_32 against '. Rodata ' can is used when making a shared object; Recompile with-fpic
./src/helloworld.o:could not read Symbols:bad value
COLLECT2:LD return 1
Make: * * * [libhello.so] Error 1 1.4 solution
Project->properties->setting->tool setting->complier add-fpic as shown in the following illustration
2. Executable Project 2.1Linux Eclipse Create an executable project
.......
2.2 hao123 Project code (non-advertising, name chaos, do not tangle)
* * * * * LoadSo.cpp * * Created on:2013 December 26 * Author:ron Tang/#include <iostream> #include <dlf
cn.h> int Main () {using Std::cout;
Using Std::cerr;
cout << "C + + Dlopen demo\n\n";
Open the library cout << "Opening hello.so...\n";
void* handle = Dlopen ("yourpath/libhello.so", Rtld_lazy);
if (!handle) {cerr << "Cannot open library:" << dlerror () << ' \ n ';
return 1;
}//Load the symbol cout << "Loading symbol hello...\n";
typedef void (*hello_t) ();
hello_t Hello = (hello_t) dlsym (handle, "Hello");
if (!hello) {cerr << "Cannot load symbol ' Hello ':" << dlerror () << ' \ n ';
Dlclose (handle);
return 1;
}//Use it to do the calculation cout << "calling hello...\n";
Hello ();
Close the library cout << "Closing library...\n";
Dlclose (handle); }
2.3 Errors that you may encounter when compiling a dynamic library project
Undefined reference to ' Dlopen '
Undefined reference to ' Dlsym '
Undefined reference to ' Dlsym ' 2.4 solutions
Project->properties->setting->tool Setting->linker add a DL as shown in the following figure