The generation and use of dynamic link library in Linux environment

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.