Create and use your own dynamic-link library under Linux

Source: Internet
Author: User

The. So file under Linux generally refers to the dynamic link library file, which saves resources by dynamically linking the program itself to a smaller size

If multiple programs use the same module, you can use the program segment more fully, the difference is the first time the dynamic library load

Program Ken will start the slow point

Here's how to build and use your own dynamic-link library files with g++

Example: There are now files Replace.h and Replace.cpp compiled into a. So file by g++ and then called in the Main.cpp file

Functions defined in the Replace.h file

List of programs:

Replace.h File Contents:

1 #include <stdio.h>
2 int Length (const char* string);
3 char* Replace (const char* string);

Replace.cpp content:

#include <stdio.h>
2 #include "replace.h"
3 int Length (const char* string)
4 {
5 if (string = = NULL) return 0;
6 int length = 0;
7 for (int i = 0; * (string + i)! = ' + '; ++i) {
8 If (* (string + i) = = ") {
9 length + = 3;
Ten} else
One by one ++length;
12}
Length of return;
14}
15
char* Replace (const char* string)
17 {
int len = length (string);
if (len = = 0) return NULL;
char* result = new Char[len + 1];
int j = 0;
(int i = 0; i < len; ++i, ++j) {
if (string[i] = = ") {
RESULT[J] = '% ';
++j;
RESULT[J] = ' 2 ';

++j;
RESULT[J] = ' 0 ';
} else {
RESULT[J] = String[i];
31}
32}
Result[len] = ' + ';
return result;
35}
36

Main.cpp content:

1 #include <stdio.h>
2 #include "replace.h"
3 int Main ()
4 {
5 const char* p = "Hjash Sdfds fdsfds FDS F DSF DS F S";
6 printf ("%s\n", p);
7 printf ("%s\n", replace (p));
8 return 0;
3 ·
10

First step: Generate. So files

g++ Replace.cpp-fpic-shared-o libreplace.so

This time LS view the current directory can find that there is a libreplace.so

Step two: Compile the connection dynamic library

g++ main.cpp-l-lreplace-o Test

At this time, I found a test executable file under the current directory.

./test

Results:

./test:error while loading shared libraries:libreplace.so:cannot open Shared object file:no such file or directory

The reason is that the libreplace.so file does not join the System link library path

Execute Export ld_library_path= Current path

Now run the program to get the right results.

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.