1, new console project Xdltest, change Properties-Configuration Properties-general-the configuration type is dynamic library. dll or static library. lib
2, open the project after adding Dltest.h, Dltest.cpp, where. h does not have the same name as. cpp.
3, writing code
----------------------------------------------
--dltest.h
#pragma once
_declspec (dllexport) void Print (const char* str);
----------------------------------------------
--dltest.cpp
#include <iostream>
#include "Dltest.h"
void Print (const char* STR)
{
Std::cout << "dltest>>" << str << Std::endl;
}
4, compile, build < The name of the library is generated according to the project name >
In the case of a static library, Xdltest.lib is generated.
If the dynamic link library generates Xdltest.lib and Xdltest.dll
5, using
(1) Add Xdltest.lib to < properties-Configuration Properties-linker-Input-Additional dependencies >
(2) in < properties-Configuration Properties-linker-General-Additional Library directory > specified xdltest.lib directory
(3) in < properties-Configuration Properties-c/c++-general-Append directory with dltest.h specified in Directory >
(4) After #include "Dltest.h" in the program, you can use print ("Hello") to output
Here are three ways to use library functions
-----------------------------------------
--How to use one
#include "stdafx.h"
#include "abc.h"//header file containing the library
int main ()
{
Print ("Hello,world");
return 0;
}
-----------------------------------------
--How to use two
#include "stdafx.h"
void Print (const char* str); Declare a function in the library so that the compilation passes
int main ()
{
Print ("Hello,world");
return 0;
}
-----------------------------------------
--How to use three
#include "stdafx.h"
_declspec (dllimport) void Print (const char* str); Importing from a library into a project
int main ()
{
Print ("Hello,world");
return 0;
}
6, run
DLL files must be copied to the EXE directory for the dynamic link library to run
Principle:
The. h file is used for compiling, which allows the program to be detected syntactically through, using this principle,
You can declare the library functions you need to use directly before using them without using the. h file
The. lib file is used for linking, if the static link library, directly writes the function implementation to the EXE,
If a dynamic link is a function signature in the EXE and it is marked as dynamically loaded at run time.
------------------------------------------------------------------------------------------------------
It is important to note that:
1,c# can only invoke C + + DLLs, not Lib,
2,c++ generated DLLs, the function names are changed (each compiler differs), and C # calls C + + DLLs requires the name of the functions, so there is an error, the function cannot be found
The corresponding method is: Generate C-style DLL, its function is not renamed Word.
C-style DLL in the function declaration plus exterun "C", such as:
Exterun "C" _declspec (dllexport) void Print (const char* str);
The generation and use of C + + dynamic link library and static link library