Compile the static library and connect to the static library, compile the static library connection

Source: Internet
Author: User

Compile the static library and connect to the static library, compile the static library connection

Introduction: The following example describes how to compile a simple sum static library libplus. a in linux and then use it.
 
$ mkdir 1;cd 1$ pwd/home/nbz/1$ cat >plus.cint plus(int i0, int i1){return i0 + i1;}$ cat >plus.hint plus(int i0, int i1);$ gcc -c -fPIC -o plus.o plus.c$ cat >main.c#include "plus.h"int main(){return plus(2, 3);}$ lsmain.c  plus.c  plus.h  plus.o$ ar -r libplus.a plus.oar: creating libplus.a$ rm plus.o$ lslibplus.a  main.c  plus.c  plus.h$ gcc -L. -o main main.c -lplus$ lslibplus.a  main  main.c  plus.c  plus.h$ ./main$ echo $?5$$ rm main$ lslibplus.a  main.c  plus.c  plus.h$ gcc -o main main.c libplus.a$ lslibplus.a  main  main.c  plus.c  plus.h$ ./main$ echo $?5$ 

In Linux, how does one call multiple static library files when using g ++ to compile and connect programs?

In the compilation command line, place the used static library file behind the source file. For example:
Gcc-L/usr/lib myprop. c libtest. a libX11.a libpthread. a-o myprop
-L/usr/lib specifies the path for searching the library file. By default, the compiler searches for the specified library file in the current directory.

When I compile A static library, I rely on B static library. The program I compile depends on A static library and B static library. In this way, does B static library have duplicates?

The static library is copied to the program at the link stage of the program.
You use the word dependency.
Program compilation usually requires preprocessing, compilation, compilation, and linking.
In the link step, the static library will be copied.
Because the static library is copied to the program in the Link stage of the program, it does not matter when the program is running.
How many links are there?
Then, it will be copied in a few minutes.

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.