Three methods for compiling links for simple programs (compiling multiple source files, static link libraries, and dynamic link libraries)

Source: Internet
Author: User

A simple program is as follows:

1 hello. h

# Ifndef hello_h
# Define hello_h

Void Hello (const char * Name );

# Endif

 

2 Hello. c

# Include <stdio. h>
# Include <stdlib. h>

Void Hello (const char * name)
{
Printf ("Hello % s \ n", name );
}

 

3 main. c

# Include <stdio. h>
# Include <stdlib. h>

Void Hello (const char * name)
{
Printf ("Hello % s \ n", name );
}

 

Method 1: compile and generate the target file for each source file, and then generate executable files for the target file:

Step 1: gcc-C hello. c

Gcc-C main. c

At this time, the. O target file is generated. ls can see hello. O main. o

Step 2: gcc-O Jia hello. O main. o

The executable file Jia is generated.

 

Run:./Jia

Display: Hello jiayudong

 

Method 2: Use the static Link Library

Mr Cheng static Link Library: (both static Link Library and Dynamic Link Library contain target files, so the target file must be generated first)

Step 1: generate the hello function as a static Link Library:

Gcc-C hello. C // generate the target file of Hello. o

Ar RCS libmyhello. A hello. O // generate the static Link Library of libmyhello.

Step 2: generate executable files through the static Link Library

Gcc-O Jia main. C-static-L.-lmyhello // generate the executable file Jia

 

Note: 1) the command for creating a static Link Library is Ar, the parameter is RC, and the static library name is lib + Library name +.

2) When a static library is connected, the required command parameter is-static-L. -l library name. When GCC is linked, it will append lib to the static library and append lib to the library name. a, and then find the corresponding static

In the GCC command, only the database name myhello is written. To link libmyhello. A, you only need to write-lmyhello.

3) after the static Link Library completes the compilation link, if it is deleted, the files with compiled links will not be affected. Therefore, when linking, the function of the library file has been linked to the execution file.

For example, to delete a static library: libmyhello. a rm libmyhello.

Execute again./Jia display: Hello jiayudong!

 

Method 3: dynamically link the database

Step 1: generate a Dynamic Link Library:

Gcc-C hello. C // generate the target file hello. o

Gcc-shared-FPIC-O libmyhello. So hello. O // generate dynamic link library libmyhello. So

 

Step 2: link the dynamic library to generate the execution file:

Gcc-O Jia main. C-L.-lmyhello // generate Jia

 

When Jia:./Jia is executed, the following error occurs:

./Jia: Error while loading shared libraries: libmyhello. So: cannot open shared object file: no such file or directory

The error message is: the dynamic link library cannot be found. The reason is that the dynamic link library is connected only when the program is running (the static Link Library is called when the program is generated), and the dynamic link library

If it is not explicitly specified in GCC, it will be searched in/usr/lib or/lib by default.

Solution 1: Copy libmyhello. So to/usr/lib or/lib.

Solution 2: Add libmyhello. So to the LD_LIBRARY_PATH path. If it is in the current directory of libmyhello. So, export LD_LIBRARY_PATH = $ (PWD );

If it is not in the current directory: Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/home/jiayudong/programme/Pratice

Solution 3: run the ldcongfig directory name under the root permission.

This command means that the dynamic link library in this directory is shared by the system. The essence is: append the cached file/etc/lD. So. cache to the shared library under the specified directory, and recreate

/Etc/lD. So. cache file.

 

Note: When deleting a dynamic link library, the program cannot run.

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.