QT Dynamic library static library creation, use, multi-level library dependencies, dynamic library changes to static library and other detailed instructions

Source: Internet
Author: User
Tags mixed

This article describes a way to add a dynamic library and a static library to a pro file by using Qtcreator under Windows systems:

1, add dynamic library (directly add dynamic library file. dll, non-subproject)

The method of creating a dynamic library through Qtcreator is not covered here.

The only thing worth emphasizing is that the creation of the dynamic library and the static library is configured in the pro file in the same way that the static library is more

CONFIG + = Staticlib, as shown (don't be impatient, the project code of the following example will eventually be uniformly packaged and uploaded)

Note: The dynamic library file generated by the Mingw32 compiler consists of a ***.dll and a ***.a file (the. A file here is an import library, similar to the Lib file generated by the VC dynamic Library . The DLL.A type file is the imp-lib of the DLL file under MinGW, that is, the DLL file with MSVs is shipped with an introduction library. Lib is similar. Programming under MSVs, when you want to use a DLL file, you must have a. lib file at development time to link through. In MinGW without the help of Imp-lib library files, you can directly link to the. dll file. In most cases, the. dll.a file does not work. See www.cnblogs.com/liushui-sky/articles/9488020.html), VC compiler generated dynamic library file contains a ***.dll and a ***.lib file

Next, how to add a dynamic library, right-click on the project name, select "Add Library", because it is to add external libraries, select "External Library", if it is a sub-project generated dynamic library, then select "Internal Library", the system library is not described here.

You need to spit it out here. The steps to add an external library under Windows can only Add. lib files, that is, to add VC compiler generated dynamic library files, this comparison pit, Linux down to is no this problem.

So qtcreator mingw32 How the compiler-generated libraries are added, looking at the first of the two images above, the ultimate goal of adding a library is "please add the library and its include path to the. Pro file", So we directly write the library file and the include path directly into the pro file. There are several ways to add methods, do not repeat, look directly at the picture

This allows the dynamic library to be added.

2. Add static libraries (add static library files directly. A, not sub-project)

Similar to adding a dynamic library, not detailed, directly look at the picture, I believe that you can see the smart

Pro file: Compile build file (static library ***.a file):

Next, how to add a static library, right-click the project name, select "Add Library", because is to add external library, select "External Library", if it is a child project generated dynamic library, then select "Internal Library", the system library is not introduced here.

Still have to say sorry, Looks like qtcreator. To add an external library under Windows, both the dynamic library and the static library can only Add. lib files, meaning that it is primarily used to add VC compiler-generated dynamic libraries (library file. dll and its bootstrap file. lib) and Static library files (. lib files), this comparison pit, Linux down to is no The problem.

So the above qtcreator mingw32 compiler generated static library how to add, in fact, with the addition of dynamic library is the same, specify the library file and include path, see the first section of the figure.

3. Add a dynamic library or a static library sub-project

When a project contains multiple sub-projects, often a project with main main file as the main project (that is, the last running program), and other sub-projects to build the library (dynamic library or static library) in the way of the main project to include.

The future debugging is convenient, we establish the dependency relationship by adding "internal library" to achieve the purpose of dynamic Association. Add the following method:

The above code is eventually added to the Test_lib.pro file, but since the static library generated by Mingw32 does not have a Lib file, the last 3 lines above are deleted, that is, the comments section in does not need

Adding an internal dynamic library is easier, without deleting it, see:

4. Multi-level library dependency

Take the project as an example, Test_lib relies on test_lib.2, and test relies on test_lib, so also rely on TEST_LIB2, remember whether the dynamic library or static library sub-project to the multi-level dependency of the library are added, but also pay attention to the smooth (the specific red line shown), Otherwise, even if the compilation is not error, the function can not be implemented properly. Maybe someone first added the test_lib2 dependency in the Test Project Pro, and then compiled the pass, and then commented out the recompile or the normal operation, but I want to tell you is----this is false, do not believe that you will compile the files are deleted and then compile, It's just that the Qtcreator rebuilt feature doesn't clean up some compiled files, so it's still a requirement to add dependencies. This dependency study can be specifically viewed in this article www.cnblogs.com/liushui-sky/articles/9487600.html

Note: The order that is contained here is exactly the opposite of the library dependency order, with the bottommost sub-item above, and the topmost item below

5, dynamic library, static library mixed use

Whether this can be mixed, of course, the answer is yes, why so, a very simple truth--the dynamic compilation of the QT library itself is a DLL, and statically compiled sub-projects generated by the library is a static library, since this can be

Use the instructions to mix them together. Specifically, take the above project as an example:

Here the TEST_LIB2 is configured to generate a static library, test_lib unchanged, still a dynamic library, test is included, run the result is OK, the explanation is feasible.

Project Sample code Location: Files.cnblogs.com/files/liushui-sky/test123.rar

6. Dynamic library changed to static library

In the dynamic Library project created through Qtcreator, there is a special "defines +=*******" definition in the Pro file, and a ******_global.h header file is generated. Add sub-project (Dynamic Library) Test_share_lib as above TEST12 project.

If the project later needs to be changed to a static library, in addition to the "CONFIG + = Staticlib" In the Pro, you also need to remove the header file and defines, and in the Test_share_ Lib.h header file in the red underlined part of the deletion, otherwise when he also relies on a few other static libraries will not find the library (note: _imp__*** related issues are generally related to dynamic library issues) and DllImport related alarms, specifically, this error is very covert, remember, If you do not delete these, and sometimes compile can be too, it should be some compiled files (or dynamic library when the compilation of files) did not remove the clean reason, this has already explained the similar situation, remember .

Project code where the error is not commented: Files.cnblogs.com/files/liushui-sky/test123_2.rar

You just need to press on the Delete pro content and test_share_lib_global.h header files, and press on the modified Test_share_lib.h, will not error, you can try it yourself.

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.