Writing static libraries using Visual Studio 2017


Building wheels is an interesting thing. Vs is a powerful tool and can be competent for large-scale projects, but seriously, it is not so friendly to the configuration of less large projects (other tutorials on the Internet are not friendly at all orz). Here is the correct pose for building a simple static library. (by the way, teach me a little truth

Create a solution – many people don’t know the difference between project / project and solution solution. In VS, project is the basic execution unit. A solution can contain multiple projects – our static library will contain several (. LIB) projects and one (. Exe) project for testing. The solution is Win32 console application console.

Select static library as the type, and remove the precompiled header option.

Add header and source files.

The same is true for the source file. CPP, which should look like this:

(note that adding only. H files without. CPP is not enough. Such a project cannot compile the. Lib library, and then you will report errors such as “XXX. Lib”: cannot open file specified “when you test. Don’t ask me how I know orz)

(another reason for turning back to cannot open file specified is that. Lib is generated in the wrong location. Here, see the Solution Explorer – > properties property page

->Output direction output directory

The default is right (in the \ debug directory, who knows how to make a mistake)

Then you can write code:

//  MathLib.h
#pragma once
int 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 what I said at the beginning, a solution can have multiple projects, all of which are execution units (I will talk about how to call execution units later, which is my own concept)). Add a new project to the solution in the Solution Explorer:

Select the console and remove the precompiled header as before. Now we have a solution containing 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 reference to mathlib for test. The actual function of this step is to add the correct link option when guiding the IDE to compile with the command line of CL compiler (CSAPP has a very instructing explanation. After the header file is included in the source file #include, it only introduces a pile of symbols that have not been relocated for the linker, which needs to be in the library file (for static libraries, Linux is the. A file and windows is. LIB). It is important to find these libraries. Add reference in solution explorer, as shown in the figure

Vs kindly lists the projects in the solution as an alternative, and you can select them.

At this point, there is the mathlib project in the test reference.

Next, add the include path for the CL compiler, that is, how to find mathlib. H in the precompiling period to complete the introduction of symbols. On the test properties property page – > C + + > General – > additional include directories, i.e. the directories contained in other (custom) header files that are additional to the standard library header file directory. CL should go to these places to find mathlib. H. Select the directory mystaticlibrary \ mathlib and click OK.

Everything is ready at this time. The F5 set-up and operation results report such an error:

Say. Lib is not a valid Win32 Application and cannot run. Yes, I didn’t want to run it, but how does vs know which of so many projects in the solution should run? (think about what the previous execution unit means. The compiled results of each project are considered executable.) the first added project is mathlib, so vs runs its output by default, so it is wrong. Try to guide vs to run the output of test (test. Exe). Solution Explorer – > mystaticlibrary properties property page:

Set the single startup project as test, OK.

Build & Run!

I wish you all a happy day!

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

Go reflection class instance

Internal mechanism of variables Type information: static meta information, predefined Value information: it is dynamically changed during program operation Use of reflection Get type information: reflect.typeof, which is static Get value information: reflect.valueof, which is dynamic Get interface value information by reflection package main import ( “fmt” “reflect” ) //Get interface value information by reflection […]