Use eclipse + CDT + mingw for Windows Programming (part 4)

Source: Internet
Author: User

 

Part 4 makes reusable parts a static library

4.1 import a sample project
Click here to download the two C ++ projects used in this article (eclipse + CDT project, Environment configuration according to the instructions in the first part), download and decompress.
Then, use the file-> Import-> Genaral: existing project into workspace wizard in eclipse to import the downloaded project to the development environment. The following project is displayed:

If build is successfulAfter running, you can see the following running results:

This example is actually the same as the previous example. The difference is that the static library method is used in this example.ProgramPractice in the sense of extension.So how to create a static library )? How can I reference this library in another project?The following two sections answer these two questions respectively.

4.2 create a static library project

The establishment of static library projects in CDT is very convenient. You can use new-> C ++ project-> static library-> Empty Project in the new Wizard to create a project. Then you can performCode. If the compilation is successful, the project structure is shown as follows:

Note thatA ***. A file generated in archives, which is the static library generated by code compilation..

4.3 static library project call

Next we will create a common C ++ project to reference and call the code in the previous section library. The process of creating a project is also very simple. You just need to use the new-> C ++ project-> executable-> Empty Project in the new wizard.

After the creation, add a reference to the static library created in 4.2.

1: header file

Because we need to call and expand the classes in the static library, we generally need to reference an externally published header file (such as inshionlib. h In this example) in the static library project ).

As shown in, add the include paths of the static library to be referenced in/C ++ build-> Settings's gcc C ++ compiler directories. The purpose isTo make the # include "inshionlib. H" in the inshion_exa002_caller Project (hereinafter referred to as the caller project) Pass.

2: Reference Library and parameter configuration
Of course, introducing only header files is far from enough. Taking the caller project as an example, if only the header file directory is added and then the source code is compiled, the following error occurs:

Undefined reference to 'winmain @ 16'
Undefined reference to 'textouta @ 20'
Undefined reference to 'setbkcolor @ 8'
Wait ......

So we alsoIntroduce the library generated by the Lib project, and set the configuration parameters to be added for windowsapi call.. The specific procedure is as follows:

SpecificallyUnder other objects in miscellaneous of mingw C ++ linker, add the ***. A library file generated by static library compilation mentioned above..

Then set-Lmingw32 and-mwindowsParameters are shown in the following two figures:

 

By the way, in fact, we are no stranger to the mwindows parameter. We have used it in the third part. The usage here is exactly the same. For details, see this seriesArticleThe third part.

After the setting is complete, you can build the caller project again. You should be able to use: D.

4.3 Other possible problems

InSource codeOr after the compilation parameters are modified, it is best to use the project-> clean function of the eclipse menu to clear the project, so that all our modifications take effect when we generate it again.
Sometimes, after both projects are cleaned and built at the same time, the following prompt appears:
.. \: No such file or directory
In fact, it means that the ***. A file is not found. In this case, check whether the archives mentioned above has what we want. file a, if our reference settings and Lib project. the a file is normal, indicating that this is a problem with eclipse resource synchronization. At this time, it does not matter. Just perform a separate build on the caller project.

This problem occurs not only now, but also often when the. o file is generated using the resource file (. Res. Normally. the o file has been generated, but cannot be seen in the Eclipse project. In this case, you only need to use F5 to refresh (or right-click the project and choose refresh) in this project, we can see the files we need and then compile the connection.

 

Appendix: Download the source code project (inshion_exa002.rar) in this document)

 

 

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.