Use Visual Studio 2017 to write a static library, Visual
Making wheels is an interesting thing. VS is a powerful tool that is competent for large-scale engineering, but to be honest, it is not so friendly to configure less large projects (other online tutorials are not friendly at all Orz ). Here we will show the correct posture for building a simple static library. (By The Way
Create a solution-many people cannot figure out the Project/Issue) Project for testing. Solution: select the win32 Console Application Console.
Select Static Library as the type, and remove the Precompiled Header option.
Add header files and source files.
The same applies to source file. cpp, which should look like this later:
(Note: add only. h file instead. cpp cannot be compiled. lib library. "" xxx. lib ": cannot open file specified" and other errors. Do not ask me how to know Orz)
(Another reason for turning back to cannot open file specified is that the location generated by. lib is incorrect. Here, we will look at the Solution Explorer-> Properties property page.
-> Output Direction Output directory
By default, the correct one is under the \ Debug directory. Who knows how to make a mistake)
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 for testing (in retrospect, a Solution can have multiple projects, they are all execution units (I will talk about how to call execution units later), and add a new project for Solution in Solution Explorer:
Select the console and remove the pre-compilation header. Now we get 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 we need to add a reference to MathLib for Test. The actual purpose of this step is to guide IDE To compile with cl compiler command line, add the correct link options (CSAPP has a very inspiring explanation. After the source file # includes the header file, it only introduces a bunch of unrelocated symbols to the linker, it needs to be in the library file (for static libraries, Linux is. file a Windows is. find these symbols to complete the relocation), it is very important to find these libraries. Add Reference in Solution Explorer
VS has carefully listed the Projects in Solution into the standby option and selected it.
Now there is a MathLib project in the Reference of Test.
Next, add the include path for the cl compiler. That is to say, how to find MathLib. h during the pre-compilation period to complete the introduction of symbols. On the "Test Properties" property page, choose "C ++"> "General"> "Additional Include" Directaries "to append the Include path, which is the directory contained by other (custom) header files in the standard library header file directory, cl should go to these places to find MathLib. h. Select "add MyStaticLibrary \ MathLib.
At this time, everything is ready. The following error is reported when F5 is set up:
It is said that. lib is not a valid win32 application and cannot be run. Yes, I didn't have to run it either, but how does VS know which project should be run in Solution? (Think about the meaning of the previous Execution Unit. The compilation result of each Project is considered executable) the first Project to be added is MathLib, so VS runs its output by default, so it is wrong. Tutorial guide vsto run test's output (test.exe), Solution Explorer-> MyStaticLibrary Properties property page:
Set the project to Test and OK.
Build & Run!
Wish everyone a pleasant journey!
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.