Chapter 4 how to generate and use dynamic and static link libraries, Chapter 4 static

Source: Internet
Author: User

Chapter 4 how to generate and use dynamic and static link libraries, Chapter 4 static

Chapter 4 how to generate and use dynamic and static Link Libraries

I. How to generate and use Dynamic Link Libraries

1. Generate a dynamic link library

In the project 1 project created in Chapter 3, change the main function name of main. cpp to maina.

Open project1.pro and change TEMPLATE = app to TEMPLATE = lib.

Execute "qmake" to generate a new Makefile, and then execute "make" to compile.

The ln command is used in the compilation process to produce soft-link files of the dynamic link library. When you compile the project, Run "ln-s" to create three soft link files (libproject1.so, libproject1.so. 1 and libproject1.so. 1.0) pointing to libproject1.so ). When the program links to the dynamic library, the default search ". so, in order to use these libraries, you need to establish a soft link to them (the Link name is. so is the suffix), which is the significance of establishing soft links during project compilation.

2. Use Dynamic Link Library

When a third-party program connects to the library libproject1.so, you can call maina to draw a software interface. All the details are implemented in the database. You only need to call the interface. Run the following command:

Enter the following code in mylib. h to save

The interface definition files of the library and library are complete. Next, write the test code test. cpp using this library in the testlib directory. The content is as follows:

Finally, compile and Test

"-I" is used to specify the path for the library header file search; "-L" is used to specify the path of the dynamic library; "-lproject1" tells the Compiler which library to find. Note: we wrote "-lproject1" instead of "-llibproject1". This is also because of Linux's preference for dynamic link library naming (it must begin with "lib). When "-l" is used to specify the file name of the library to be linked, it can be abbreviated as "-lproject1" instead of being fully written.

When you run. test, the above error message is displayed, indicating that the dynamic link library cannot be found. Run "ldd./test" to check what dynamic link libraries are used by test.

You can see "libproject1.so. 1 => not found ", and other libraries used by the program, such as libc. so.6 In the "/lib" directory, libstdc ++. so.6 can be found under "/usr/lib. These two paths (/lib and/usr/lib) are the default search paths for Linux dynamic libraries. Dynamic Link Library (libproject1.so. 1) You need to copy it to the default search path. When an application needs to use the dynamic link library interface (maina) for execution, the system will automatically go to the default search path to find the corresponding dynamic library, load the Dynamic Linked Library to the memory supply application.

You can specify the dynamic library search path by loading the environment variable (LD_LIBRARY_PATH.

The previous software interface came out.

Ii. How to generate and use a static Link Library

1. Generate a static Link Library

Open project1.pro in the Project 1 project created in Chapter 3, and add a statement "CONFIG + = staticlib" in it ".

Execute "qmake" to generate a new Makefile, and then execute "make" to compile and generate the static Link Library libproject1.a.

2. Use static Link Library

Create the teststaticlib directory under the created testlib directory, and the generated static link library (libproject1.a) and the previously created mylib. h header file and test code test. copy cpp to the teststaticlib directory and run "qmake-project" to generate the project file teststaticlib. pro, compile the file and add "LIBS + =-L. /-lproject1 ", specify the libproject1.a search path, and then generate Makefile again to compile and run the project. You can see that the interface is coming out.

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.