Create shared library so on Linux

Source: Internet
Author: User
Create a shared library on Linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. Development Environment: Ubuntu 7.04, gcc 4.1.2

Create shared library so on Linux with C
1. Create math1.c and enter nano math1. c in the command line.
Int add (int x, int y)
{
Return x + y;
}

2. compile and generate the shared library libmath1.so
Gcc-fPIC-shared-olibmath1.so math1.c

3. Create the test program testmath. c and enter nano testmath. c In the command line.
# Include
Int main ()
{
Int x = 10, y = 18;
Int sum = add (x, y );
Printf ("% d + % d = % d \ n", x, y, sum );
Return 0;
}

4. compile and generate the test program testmath.
Gcc-otestmath-lmath1 testmath. c-L = ./

5. Run the test program and enter./testmath in the command line.
The following error occurs:
./Testmath: error while loading shared libraries: libmath1.so: cannot open shared object file: No such file or directory

6. Copy libmath1.so to the default shared library loading path (/lib or/usr/lib) in Linux)
Enter sudo cp libmath1.so/usr/lib/in the command line/

7. Run the test program testmath again.
Result: 10 + 18 = 28
Gcc compilation parameters:
-FPIC generates location-independent code so that the Library can be connected and loaded at any location.
-Shared represents the shared library.
-O links to generate a library or program with a specified name
-L specify the shared library to be linked
-L specify the path of the Shared Library

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.