Take you to the visual studio--take you to publish your own engineering library

Source: Internet
Author: User

Previous article take you to the visual studio--with your efficient management of code by explaining the VisualSVN excellent plugin, we have mastered the ability to manage code quickly and efficiently in integrated development environment vs. However, the programs we develop do not always generate executable software directly, we may just develop a component of a large system, or it may be the application call that the kernel SDK for the development of a software provides to the upper layer, and in the process of development we may also use a third-party open Source Library. What if you compile your program into a library for the caller? And how do you refer to third-party libraries in your own programs? This will be what this article is about-releasing your own engineering library.

What is a library?

A library is a well-written, existing, mature code that can be reused. In reality, every program relies on many underlying libraries, and it is not possible for everyone's code to start from scratch, so the existence of a library is extraordinary. For example, you often use the STL (Standard Template Library) is also a library, with the STL you can easily use std::string, std::cout these classes.

In essence, a library is a binary form of executable code that can be loaded into memory by the operating system and executed by another program call. There are two types of libraries in C + +: Static libraries and dynamic libraries. The process of compiling a program into an executable file is generally precompiled –> compiling the –> link, while the difference between the static and the dynamic libraries is mainly reflected in the linking process.

Static libraries:

During the link phase, the compiled target file. obj is packaged with the referenced library. Lib to the executable exe (also known as the target code), which is no longer needed by the program when it runs.

Therefore, the resulting linked executable (. exe) file is large in size . In Windows, the. Lib is generally the suffix name, in Linux generally with. a suffix.

Dynamic libraries:

In the link phase, the dynamic library. DLL is not actually connected to the target code, but the declaration of the dynamic library is linked to the target code (so the program runs to know how to use the dynamic library). The dynamic library. DLL is still independent, and the. dll is loaded into memory by the program call only if the program is running. Therefore, the program must run with this dynamic library and put it in the correct path.

As a result, the resulting linked executable (. exe) is small in size . In Windows, a. dll is typically the suffix name, and in Linux it is usually suffixed with. So.

The difference between a static library and a dynamic library:
features Static Library Dynamic Library
The time to link to a function library Completed during the link phase of the compilation Postpone to the time when the program is running
The relationship between the running process and the library The program is no longer associated with the static library at run time Program at run time with Dynamic library needs to exist and path is correct
Whether to link to an executable file Static libraries are linked to an executable file Dynamic libraries are not linked to the executable file
Target file size Large volume Less volume
Memory footprint Consumes memory. If multiple programs use the same static library, each of the programs will contain this static library Save memory. If multiple programs use the same dynamic library, you can implement resource sharing between processes (so dynamic libraries are also known as shared libraries)
Program porting Easy to transplant Porting is inconvenient and requires header files for all dynamic libraries
Program Upgrade Program upgrade trouble, need to download the entire program to upgrade Program upgrade is easier, only need to upgrade a DLL or a program, download an upgrade package to
Compiling your own engineering library

Assuming we have such a project, the function of this project is to provide some common tools and methods, and then we will compile the project into a library for others to use.

Compiling a static library

Let's say we've built the project and written the appropriate code:

Project Catalogue

Utils.h:

//===============================================================//summary://Utils class, Tool class//filename://Utils.h//remarks://          ...//date://2015/10/4//author://Administrator ([email protected])//===============================================================#ifndef __utils_h__#define __UTILS_H__#include <string>#include <strstream>//#include <cstdlib>classutils{ Public: Utils (void); ~utils (void); Public://---------------------------------------------------------------    //function:    //wstring2string wstring to string conversion    //access:    // public    //parameter:    //[in] const std::wstring & ws-wstring String    //returns:    //std::string-string string    //remarks:    //Some methods cross-platform, portable version    //author:luoweifu    //---------------------------------------------------------------    Static STD::stringWstring2string (Const STD::wstring& ws);//---------------------------------------------------------------    //function:    //string2wstring string to wstring conversion    //access:    // public    //parameter:    //[in] const std::string & s-string String    //returns:    //std::wstring-wstring string    //remarks:    //Some methods cross-platform, portable version    //author:luoweifu    //---------------------------------------------------------------    Static STD:: Wstring string2wstring (Const STD::string& s);};//---------------------------------------------------------------//function://converttostring convert int to string//parameter://[in] int val-variable to convert//returns://std::string-converted string//remarks://          ...//author:luoweifu//---------------------------------------------------------------STD::stringConvertToString (intVal);#endif //__utils_h__ 

The implementation of the above declaration is referenced in appendix Utils.cpp later. The annotations here are generated by VASSISTX, and the use of VASSISTX can refer to an article written earlier that takes you to the visual studio--to develop efficiently.

To compile into a static library, we can set up our project like this:
Right-click Project->properties


Compiling into a static library

Then right-click Build, you can be in the solution of the debug (the actual situation is generally compiled into release version, the same way as set, here the content of the next chapter) can see Utils.lib, this is the compiled library. To use this library for others, just provide this Utils.lib and the project's header file. Copy the Utils.h to D:\ReleaseLibs\StaticLib\Includes, copy the Utils.lib to D:\ReleaseLibs\StaticLib\Libs, and D:\ReleaseLibs\ Staticlib This file can be provided out of the way. Use of static libraries see the following section using a static library

Compiling a dynamic library

Compiling a dynamic library is a bit cumbersome compared to a static library, and you typically add the _declspec (dllexport) keyword prefix to the declaration of the exported function.
1. *utils.h 's statement is as follows

//===============================================================//summary://Utils class, Tool class//filename://Utils.h//remarks://          ...//date://2015/10/4//author://Administrator ([email protected])//===============================================================#ifndef __utils_h__#define __UTILS_H__#include <string>#include <strstream>//#include <cstdlib>//===============================================================//=============================================================== class Utils{ Public: Utils (void); ~utils (void); Public://---------------------------------------------------------------    //function:    //MAX Gets the maximum value in two numbers    //access:    // public    //parameter:    //[in] int nValue1-first number    //[in] int nValue2-every two numbers    //returns:    //INT-Maximum value    //remarks:    //          ...    //author:luoweifu    //---------------------------------------------------------------    Static intMax (intNValue1,intNValue2);//---------------------------------------------------------------    //function:    //min to get the minimum value in two numbers    //access:    // public    //parameter:    //[in] int nValue1-First value    //[in] int nValue2-second value    //returns:    //INT-Minimum value    //remarks:    //          ...    //author:luoweifu    //---------------------------------------------------------------    Static intMin (intNValue1,intNValue2);//---------------------------------------------------------------    //function:    //Range limits a value to one range    //access:    // public    //parameter:    //[in] int nMin-Minimum value    //[in] int nMax-Maximum value    //returns:    //INT-Returns a value within the range that is restricted    //remarks:    //          ...    //author:luoweifu    //---------------------------------------------------------------    Static intRange (intNMin,intNMax,intNvalue);};//---------------------------------------------------------------//function://Converttoint converts a constant string to int type data//access:// public//parameter://[in] const char * pStr-constant string//returns://INT-converted to an int value//remarks://          ...//author:luoweifu//---------------------------------------------------------------intConverttoint (Const Char* pStr);#endif//__utils_h__
    1. To compile into a dynamic library, we can set up our project as follows:
      Right-click Project->properties

      Set the target type of the compilation

      Set up precompiled macros

Then right-click Build, you can be in the solution of the debug (the actual situation is generally compiled into release version, the same way as set, here the content of the next chapter) can see Utils.dll and Utils.lib, this is the compiled library. To use this library for others, just provide this Utils.dll, Utils.lib, and the project's header file. Copy the Utils.h to D:\ReleaseLibs\DynamicLib\Includes, copy Utils.dll and Utils.lib to D:\ReleaseLibs\DynamicLib\Libs, and D:\ Releaselibs\dynamiclib This file can be provided out of the way. Use of static libraries see the next section using dynamic libraries

Perhaps you want to ask why the compiled static library is Utils.lib, the compiled dynamic library also has Utils.lib, these two. lib files are the same?
You compare two. lib file size will be found to be very large (Static library lib has 235KB, dynamic library of the Lib only 2.7KB), so certainly not the same! The lib file corresponding to the dynamic library is called "Import Library", the import library contains only the address symbol table, etc., ensure that the caller's program can find some basic address information of the corresponding function, and the actual execution code is in the DLL file. the lib file of the static library contains the actual execution code, symbol table and so on.

Using the import (third-party) library

In real-world development, you often use libraries provided by third parties, such as open source libraries, or components provided by partners in large systems. What if I use it? Let's take a sample of our own library. Let's say we have a project TestProject to use the Utils library we made above.

Using a static library
    1. Right-click the project->properties, and make the following settings.


      Set the path where the header file resides

      Set the path where the Lib library is located

      Set which Lib library to import

    2. The test code is as follows:

#include <iostream>#include <tchar.h>#include "Utils.h"int_tmain (intARGC, _tchar* argv[]) {intNMax = Utils::max ( -,Panax Notoginseng);STD::cout<< NMax <<STD:: Endl;intNMin = Utils::min (Ten, -);STD::cout<< NMin <<STD:: Endl;intNvalue = Utils::range (0, -, the);STD::cout<< Nvalue <<STD:: Endl;Char* PStr ="1234";intNValue2 = Converttoint (PSTR);STD::cout<< nValue2 <<STD:: Endl;return 0;}
Working with Dynamic libraries
    1. Right-click the TestProject project->properties and make the following settings.


      Set the path where the header file resides

      Set the path where the Lib library is located

      Set which import library to import

    2. Put the Utils.dll in the same path as the output file TestProject.exe the TestProject. This is the heaviest, or the successful compilation will fail because the corresponding. dll file cannot be found.

    3. The test code is the same as the static library.

Appendix Utils.cpp
#include "Utils.h"Utils::utils (void) {}utils::~utils (void){}intUtils::max (intNValue1,intNValue2) {returnNValue1 > NValue2? Nvalue1:nvalue2;}intUtils::min (intNValue1,intNValue2) {returnNValue1 < nValue2? Nvalue1:nvalue2;}intUtils::range (intNMin,intNMax,intNvalue) {if(NMax < NMin) {inttemp = nMin;        NMin = NMax;    NMax = temp; }if(Nvalue < NMin) {returnNMin; }Else if(Nvalue > NMax) {returnNMax; }Else{returnNvalue; }}intConverttoint (Const Char* pStr) {intValSTD:: Strstream SS;      SS << PStr; SS >> Val;returnVal }

Reference article: C + + static library and dynamic library

Previous review:
Take you to the visual studio--to efficiently manage your code

What to tell NEXT:
Take you to the visual studio--take you to manage a variety of release publications

Copyright NOTICE: This article for Bo Master original article, without Bo Master permitted not for any commercial use, reproduced please indicate the source.

Take you to the visual studio--take you to publish your own engineering library

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.