Linux C dynamic shared library compilation link

Source: Internet
Author: User

1.1.1 LinuxWrite SoHow files are

1 first compile to add -fpic -fpic is to tell GCC generate a location-independent code

2GCC link to add -shared option, meaning to generate a shared library.

for linux or unix , one so file, file extension must be

1.1.2 LinuxUse So

GCCwhen linking, you need to add-L.representative from the current directory to find the relevant Sofiles,- Lfile name (but not including the file name at the beginning ofLiband extension So)

For example, compiling a MAIN.O file to be used in the current directory. liba.so Library

Gcc–o Main.out–l.–la MAIN.O

1.1.3 Configuration Profilefiles can be found in the current directory Sofile

Linux not in the current directory to find executable programs, but also not early in the current directory to find So Library Files

How to modify a User configuration file

1

Cd

2

VI. bash_profile

3

Export Ld_library_path = $LD _library_path:.

4

Save exit

5

. . bash_profile





[Email protected]:~/link/c$ cat mymax.c int max (Int a,int b) {if (a>b) Return a;elsereturn b;} [email protected]:~/link/c$ cat mymax.h  #ifndef  mymax_c_#define mymax_c_int  max (int a,int b); #endif  [email protected]:~/link/c$ cat main.c # include <stdio.h> #include   "mymax.h" Int main () {printf ("max =%d \n", Max (1,4)); return 0;} [EMAIL&NBSP;PROTECTED]:~/LINK/C$&NBSP;[EMAIL&NBSP;PROTECTED]:~/LINK/C$&NBSP;GCC&NBSP;-C&NBSP;-O&NBSP;MYMAX.O   -fPIC  mymax.c   tells the compiler to generate location-independent code [email protected]:~/link/c$ gcc - shared -o libmymax.so mymax.o  mymax.o  generate so shared library, must start with LIB, the extension must be .so[email  protected]:~/link/c$ gcc -c main.c -o main.o[email protected]:~/link/c$ gcc  -o main.out main.o -L. -lmymax[email protected]:~/link/c$ ./main.out max =4echo  ' export ld_library_path= $LD _library_path:. '  >> /home/chunli/.bashrc[email protected]:~/link/c$ . /home/chunli/.bashrc




Copy to another directory:

Still running:

[Email protected]:~/link/c$ cp libmymax.so Main.out/tmp/[email protected]:/tmp$./main.out Max =4








This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1771934

Linux C dynamic shared library compilation link

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.