The method of generating a static library procedures C (the LIB) and dynamic link library (DLL) at a VS

I. Introduction

Work sometimes because of the division of labor needs to let others write their own code to call a function to accomplish, but do not want others to see the specific implementation process, but provides an API interface in the form of calls for others; or is it the other Some of the reasons, it is necessary to learn the knowledge static library and dynamic library, most of the contents of this article are for reference after finishing the online information as a summary of learning.

 

Two and distinguishing, DLL and LIB files

lib is a binary file, similar to the dll, call for other programs. lib and dll difference is: dll is needed runtime, lib is needed when compiling.

1, there are two libraries:
  (1) One is LIB contains information about DLL files and functions are located in the function of the position (entry), code is loaded by the runtime to provide a DLL in the process space, known as dynamic link libraries dynamic link library.
  (2) A LIB contains the function code itself is directly added to the program code at compile time which, called the static link library static link library.

2, there are two linked ways:
  (1) using the dynamic link library dynamically linked, allowing executable module (.dll files or .exe file) contains only the information required to locate DLL function at run-time executable code.
  (2) using a static link library statically linked, the linker LIB get all referenced functions from the static link library, put the executable file and the library together with the code.

3, on the difference between lib and dll as follows:
  (1) lib is used at compile time, runtime dll is used. To complete source code to compile, just lib; if you want dynamically linked programs up and running, only dll.
  (2) If there dll file, lib are generally some of the index information, record the entry and location of the dll function, the dll is the specific content of the function; if only lib file, then the lib file is a static compiler of the index and implementations in them. Use statically compiled lib file, do not need to run the program and then hang a dynamic library, the disadvantage is leading to large applications, but also lose the flexibility of dynamic libraries, to publish a new application for the job when the new version is released.
  Case (3) dynamic link, there are two files: a LIB file is a DLL file. LIB contains the function name and location of the DLL to be exported, DLL contains the actual functions and data, applications use LIB file is linked to the DLL file. In the application's executable file, stored in function code it is not being called, but the address of the corresponding DLL function code, thus saving memory resources. LIB and DLL files must be issued together with the application, otherwise the application will generate an error. If you do not want to use the lib lib file or not file, you can use WIN32 API functions LoadLibrary, GetProcAddress load.

4, using two lib files to be noted:
  (. 1) .h header file containing the class data structures or symbol prototype or lib described output. Application calls lib, need the file that contains the application's source files.
  (2) .LIB file slightly.

5, using three dll file should be noted:
  (. 1) .h header file that contains the class or symbol prototype .h file or data structures and the description of the output dll. Application calls dll, need the file that contains the application's source files.
  (2) .LIB files are dll after compiling, linking success generated files action when other application calls dll, need the file into your application, otherwise an error. If you do not want to use the lib lib file or not file, you can use WIN32 API functions LoadLibrary, GetProcAddress load.
  (3) .dll files, real executable file, the application developed at the time of publication, only to have the .exe file and .dll files, the file does not need .lib and .h header files.

 

Third, generate static library (LIB) at VS

New VS2010 project, pay attention to select the Win32 project , rather than MFC DLL project. MFC DLL differences and win32 DLL is a MFC use, if you do not use a code to use the MFC, you must create the MFC DLL, because win32 DLL does not take MFC class.

Next, it is best not to choose pre-compiler option! Election precompiled headers, then, add #include "StdAfx.h" in the .cpp file

 Then add in .h and .cpp file, you can point directly to generate a solution. Do not point debugging, because there is no entry function. Here we must pay attention to your project configuration properties generated lib and you want to use this lib project configuration properties to be consistent. The .h file and generate a lib file copying your project and add, ready to use.

 If an error occurs error LNK2005: ___xi_a already defined in msvcrt.lib (cinitexe.obj), solution: Attribute :: C ++ :: runtime library, to ensure that all projects and libraries referenced are the same options, such as choose "multi-threaded dll" when the release version.

 

Fourth, a dynamic library (DLL) under VS

1, generated

(1) File - New - item, as selected in FIG.

 

(2) a new header files and source files test.c test.h

//test.h
__declspec(dllexport) int sum(int a, int b);
//test.c
#include "test.h"
#include <stdio.h>
 
int sum(int a, int b) {
	return a + b;
}

 

(3) Right-click the project selection generation in debug will produce multiple files, we just need to dllgen.dll and dllgen.lib 

2, using

(1) File - New - Project - Empty Project

(2) engineering the dllgen test.h, dllgen.dll, dllgen.lib copied to the current project

(3) Add test.h in the project, while New main.c

//main.c
#include "test.h"
#include<stdio.h>
#include<stdlib.h>
 
#pragma comment(lib,"dllgen.lib")
 
void main() {
	int n;
	n = sum(10, 6);
	printf("10 + 6 = %d\n",n);
	system("pause");
}

 (4) run the project, view test results

 

 Fifth, the load lib / header files and DLL methods

1, load lib / header file

(1) applies to the current project

Step 1: Project -> Properties -> C / C ++ -> General -> Additional Include Directories (browse path .h file) add include files.

Step 2: Project -> Properties -> C / C ++ -> Linker -> Input -> Additional Dependencies (the name written lib) add a library file.

3 Project -> Properties -> C / C ++ -> Linker -> Input -> Additional Dependencies, add lib used.

Step 4: Add #include <gtest / gtest.h> (note that the file path) prior to use cpp files.

(This step can also be displayed in the code call #pragma the Comment (lib, "*** lib.") ) (If not add, connection error: unresolved external symbol).

(2) applies to all projects, even after the set up of new reconstruction projects without re-setting.

1 Tools->Options->Projects and Solutions->VC++ Directories ->Show directories for:(选择include files)->添加.h路径
2 Tools->Options->Projects and Solutions->VC++ Directories ->Show directories for:(选择Library files)->添加lib路径

(这些路径只告诉编译器怎么找文件,没有说把那里面的文件加入工程.)
(若不设置,编译报错:无法打开***文件)。

2、加载DLL

将dll文件拷贝到工程debug文件下(如果不拷贝,编译链接不报错,运行报错:无法找到***.dll)。

 

六、参考文章地址:

https://www.cnblogs.com/crazyht/p/4131104.html

https://blog.csdn.net/knightk123/article/details/80880956

https://blog.csdn.net/baijian1989/article/details/73294128

https://blog.csdn.net/jhgameboy/article/details/23710429

发布了8 篇原创文章 · 获赞 15 · 访问量 2326

Guess you like

Origin blog.csdn.net/qq_34254642/article/details/104091651