Gcc shared object, gccshared

Source: Internet
Author: User

Gcc shared object, gccshared

An example of generating a dynamic link library *. so is introduced: first create a new 1 Head file test. h:

#include <stdio.h>void first();void second();void third();

Create three source files first. c/second. c/third. c:

First. c:

#include "test.h"void first() {    printf("this is first.\n");}

 

Second. c:

#include "test.h"void second() {    printf("this is second.\n");}

 

Third. c:

#include "test.h"void third() {    printf("this is third.\n");}

 

Then, generate the dynamic link library libtest. so: gcc first. c second. c third. c-fPIC-shared-o libtest. so
Note: 1.-fPIC indicates that the code location is independent for program sharing. 2.-shared means to generate a dynamic link library

Next, create a source code test. c that calls the Dynamic Link Library:

#include "test.h"void main(){    first();    second();    third();}

Generate the executable file: gcc test. c-L.-ltest-o test
Note:-L. indicates that the link library is in the current directory;-ltest indicates that the link library libtest. so is found based on implicit rules.

Execute./test to obtain the following results:

this is first.this is second.this is third.

If you don't get this result, you will be prompted ". /test: error while loading shared libraries: libtest. so: cannot open shared object file: No such file or directory ", then in/etc/ld. so. conf. create a test. conf configuration file, and write the access path of your dynamic link library in this file, such as/usr/local/demo/lib, depending on your own situation. After the configuration is completed, type sudo ldconfig to make the configuration take effect.

Over.

Ps:
You can also create a soft link, for example, ln-s/your_folder/* so/usr/lib.

Related Article

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.