Linux build (load) dynamic library static libraries and load sample methods _linux Shell

Source: Internet
Author: User

Generation of dynamic libraries:

1,/*mysum.c*/

Copy Code code as follows:

#include <stdio.h>
#include "src.h"


int sum (int a,int b)
{
return (A+B);
}


2,/*mysum.h*/

Copy Code code as follows:

#ifndef __src_h__
#define __src_h__

int sum (int a,int b);

#endif


3,/*main.c*/

Copy Code code as follows:

#include <stdio.h>

extern int sum (int a,int b);

/*

#include <stdio.h>

#include "mysum.h"

*/

int main ()
{
int a,b,c=0;
printf ("Input A and b:\n");
scanf ("%d%d", &a,&b);
c = SUM (A,B);
printf ("c =%d\n", c);
return 0;
}


4. Generate Dynamic Library

Copy Code code as follows:

Gcc-c-fpic mysum.c mysum.h//Generate MYSUM.O file

Gcc-shared-fpic-o libmysum.so MYSUM.O//Generate dynamic library file, the default naming method is lib+ name. So


5, dynamic library static loading

Copy Code code as follows:

Gcc-o main main.c-l.-lmysum-wall

Or

Gcc-o Main Main.c-lmysum-wall

6. When executing./main, an error is reported, prompting that the Libmysum.so shared library cannot be loaded.

7, the processing method has three:

Method One:

Copy a dynamic library to/lib or/usr/lib

such as: sudo cp libmysum.so/lib

Method Two:

Changing environment variables:

Ld_library_path= $PWD Export Ld_library_path

Method Three:

Specify the path to the shared library at compile time:

Gcc-o main1-wl,-rpath,.-L.-louyang MAIN.O

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.