VS2017 complete the construction of the static library

Source: Internet
Author: User
This article is mainly for you to introduce the visual Studio2017 write static library of relevant data, with a certain reference value, interested in small partners can refer to

Making wheels is an interesting thing, VS is a powerful tool for super-large projects, but honestly, it's not as friendly for less-large projects (other tutorials on the web are not friendly at all Orz). Here is the correct posture for building a simple static library. (By the way, a bit of truth

Create a solution--many people don't understand the difference between project/project and solution solution, in VS, Project is the basic execution unit, and a solution can contain multiple project-- Our static library will contain several (. lib) project and one (. exe) project as a test. Solution Select the Win32 Console Application console.

Type select static Library to remove the precompiled Header Precompiled headers option.

Add header files and source files.

The source file. cpp is the same, then it should look like this:

(Note that only the. h file is added without. CPP is not possible, such a project can not be compiled. Lib Library, after you test the time will appear "" Xxx.lib ": Cannot open file specified" such as error don't ask me how to know Orz)

(Another reason to turn back cannot open file specified is that. Lib is not generated in the right place, see Solution explorer->properties property page here.)

->output Direction Output Directory

The default one is right. In the \debug directory, who knows how to be wrong)

Then you can write the code:

  mathlib.h#pragma onceint Add (int x, int y);//  Mathlib.cpp#include "MathLib.h" int Add (int x, int y) {return x + Y }

Then you need a project to test (recall from the beginning that a solution can have multiple project, they are execution units (a later talk about how to call the execution unit This is the concept of my own creation)), in the solution The explorer adds a new project for solution:

Select the console and remove the precompiled header as before, and now we have a solution with two projects:

Write a test code

  test.cpp#include "stdafx.h" #include ". \mathlib\mathlib.h "#include <iostream>int main () {  std::cout << Add (1, 2) << Std::endl;  return 0;}

Now you need to add a mathlib reference to test, and this step is actually to include the correct link option when booting the IDE with the CL compiler command line (Csapp has a very inspiring explanation, the source file # The Include header file only introduces a bunch of non-relocated symbols for the linker, which needs to find these symbols in the library file (the static library, Linux is. A file windows is. lib), and it is important to find these libraries. Inside the Solution Explorer add Reference

vs very thoughtful to put the projects in solution into the alternative, select.

At this time test reference in the Mathlib project.

Next, add the include path for the CL compiler, that is, how to find MathLib.h during the precompilation period to complete the introduction of the symbol. In the Test Properties property page->c++->general->additional include directaries additional include path, which is additional to the standard library header file directory of other (custom) header file contains the directory, CL wants to go to these places to find MathLib.h. Select Add mystaticlibrary\mathlib this directory OK.

This time everything is ready, F5 the formation of the results of a report such a mistake:

Says that Lib is not a valid Win32 app and cannot be run. Yes, well, I didn't want to run it, but how does it know how much projects should run in solution? (think of the previous execution unit to understand what that means, the results of each project compilation are considered executable) the first add project is mathlib so the VS defaults to run its output so wrong. Try to boot vs to run Test output (Test.exe), Solution explorer->mystaticlibrary Properties page:

Set up a single start-up project for Test,ok.

"Recommended"

1. asp free Video Tutorial

2. asp Tutorials

3. Eon the ASP Basic video tutorial

Related Article

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.