Using your own package so encountered a problem, in the light of the principle of the decision to write a demo to see, by the way record the whole process.
1) The files required to generate so are as follows:
Print.h
#ifndef __print_h__#define __print_h__void print (void); #endif
Print.cpp
#include <stdio.h> #include "print.h" void print (void) {printf ("Hello world.\n");}
Bye.h
#ifndef __bye_h__#define __bye_h__void bye (void); #endif
Bye.cpp
#include <stdio.h> #include "bye.h" void Bye (void) {printf ("Bye bye.\n");}
Midd.h
#ifndef __midd_h__#define __midd_h__class middleware{public:middleware (); ~middleware (); BOOL Data (const char *p);}; #endif
Midd.cpp
#include "print.h" #include "bye.h" #include "midd.h" #include <stdio.h>middleware::middleware () {printf (" Middleware::middleware () \ n ");} Middleware::~middleware () {printf ("middleware::~middleware () \ n");} BOOL Middleware::d ATA (const char *p) {print (); bye (); return true;}
2) Compilation process
g++-c-fpic-shared Bye.cpp
g++-c-fpic-shared Print.cpp
g++-c-fpic-shared Midd.cpp
3) Linking process
g++-o test.so midd.o print.o bye.o-fpic-shared
4) Use of dynamic link libraries
Now write a main.cpp to call so and see the effect
Main.cpp
#include "midd.h" int main () {middleware middleware;middleware.data ("xxx"); return 0;}
To generate an executable file:
g++-O test main.cpp/test.so
Test:
./test
Middleware::middleware () Hello World.bye bye. Middleware::~middleware ()
The generation and use of dynamic link library in Linux environment