Using VS2012 to generate DLL files (1)

Source: Internet
Author: User

One: Build DLL

1: Create DLL project

FILE-New Project->visual C++->WIN32->WIN32 Console Application (Win32 Project can also)

Fill in the project name mydll-> OK and next->dll (additional options for empty item tick), complete.

Here the DLL project is created, the following new two files MyDLL.cpp and MyDLL.h.

MyDLL.cpp content is as follows:

1 #include
2 using namespace Std;
3 #include "MyDLL.h"
4
5 int Add (int &a,int &b)
6 {
7 return a+b;

8}

MyDLL.h content is as follows:

1 #pragma once

2
3 extern "C" __declspec (dllexport) int Add (int &a,int &b);

Click Generate Bulid-->bulid mydll,dll file generated, vs2008 can not directly generate Lib file, this time we need to build a DLL project and then create a new def file, the default generation and then regenerate will be able to get Lib file, However, you can create a Lib file by modifying the General->project default->configure type in the project properties to Lib.

Note: If you create a DLL or LIB on an existing project, you do not need to modify the project, just modify the General->project default->configure type in the project properties to Lib or DLL, You can generate Lib files or DLL files.

Use the DLL file steps that you just generated in your C + + program:

Create a new Win32 console project, named Testmydll, new two files: TestMyDLL.cpp and TestMyDLL.h,

The first need to implicitly link The static loading method, relatively simple, need. h,. lib,. dll three-piece set. Create a new console application or empty project. The configuration is as follows: (this is very important)

Configuration Properties---Properties---->vc++ Directory Add header file MyDLL.h directory in "Include directory"

Configuration Properties---Properties--->vc++ directory, add header file MyDLL.lib directory in the library directory
Add "MyDLL.lib" in "Additional Dependencies" (if more than one Lib is separated by a space), type------------configuration Properties------

TestMyDLL.cpp content is as follows:

#include "TestMyDLL.h"
#pragma comment (lib, "... \\debug\\MyDLL.lib ")//can be written as absolute path, but the path must be specified with \ \, that is: j:\\pr//ograms\\c++\\practice\\dlltest\\dlltest\\debug\\ MyDLL.lib "#include
using namespace Std;

int main ()
{
int a = 3;
int b = 2;
Cout<<add (b) <<endl;

GetChar ();
}

TestMyDLL.h content is as follows

#pragma once


extern "C" _declspec (dllexport) int Add (int &a,int &b);

It is now possible to compile, but the program runs with an error, and you need to copy the MyDLL.dll to the same directory as the executable file that the current project generates. (This is very important)

It is important to note that the TestMyDLL.cpp file calls Lib in this phrase:

#pragma comment (lib, "... \\debug\\MyDLL.lib ")

It is necessary to indicate the folder where Lib is located, and of course we can specify the output path of the Lib and DLL files in the Mydll project that generated the DLL, directly to the Testmydll project.

Note: If only DLL files, then you must call the LoadLibrary () function in the program to use, if there is a Lib file, then there are two ways to call immediately

(1). Dependencies (recommended, Lib source code is required)

A project is divided into several projects to do, one main project EXE, the other for the Static library Lib

Project-->dependencies, setting the main project dependencies for other Static library Lib

In this case, the Lib is automatically added to the resource files of the main project.

Header files in the main project that need to be added to the location of other libraries

(2). Add Lib directly to the required project (not recommended, Lib not unified management)

Lib and its header files are provided

Select Project--Right-click-->add Files to Project

In this case, the Lib is automatically added to the resource files of the main project.

Header files in the main project that need to be added to the location of other libraries

(3). Link Setup through the project (recommended, Lib can be managed uniformly)

Lib and its header files are provided

Project-->settings-->link, select input from Categery

The. lib file name for the dynamic link library entered in the Object/library modules

Enter the path of the. lib for the dynamic link library in the additional library path

Header files in the main project that need to be added to the location of other libraries

(4). #pragma (lib, "Filename.lilb") (not recommended, Lib cannot be managed uniformly)

Lib and its header files are provided

Add #pragma (lib, "Filename.lilb") to the location of the other libraries in the main project

In the main project, you need to use the location of the other libraries to add the storage header files:

=============================================================================================

Use of extern C

C + + is an object-oriented programming language, in order to support the overloading of functions, C + + to the global function of the processing mode and C are obviously different,

Question: Why do standard header files have a structure similar to the following?

#ifndef __incvxworksh

#define __incvxworksh
#ifdef __cplusplus
extern "C" {
#endif/*...*/
#ifdef __cplusplus
}
#endif
#endif/* __incvxworksh */

Analysis

Obviously, the compiler macros in the header file #ifndef __incvxworksh, #define __incvxworksh, #endif的作用是防止该头文件被重复引用. So


#ifdef __cplusplus

extern "C" {
#endif
#ifdef __cplusplus
}
#endif

What is the role of it? We will be in the following one by one lanes.

1,extern "C" contains a double meaning, literally can be obtained: first, it is modified by its target is "extern";

Second, the target that it modifies is "C". Let's take a detailed reading of this twofold meaning.

Functions or variables that are qualified by extern "C" are extern types, and extern is a keyword that indicates the scope (visibility) of functions and global variables in the C/D + + language, which tells the compiler that its declared functions and variables can be used in this module or in other modules. Remember, the following statement:

extern int A;

is simply a declaration of a variable that does not define variable A and does not allocate memory space for a. Variable A can only be defined once in all modules as a global variable, or a connection error occurs. Typically, the functions and global variables that this module provides to other modules in the header file of the module are declared with the keyword extern, for example:

If module B is to refer to the global variables and functions defined in module A, only the header file of module a can be included. In this way, when a function in module A is called in Module B, in the compile phase, Module B cannot find the function, but it does not error; it will find this function in the target code generated from module a during the connection phase .

The keyword that corresponds to extern is static, and the global variables and functions it modifies can only be used in this module. Therefore, a function or variable may not be modified by extern "C" only if it is used by this module.

The variables and functions modified by extern "C" are compiled and concatenated in C language;

2, not add the extern "C" declaration when the compilation method

Let's start with a look at how C + + is compiled for a function that is similar to.

For example, suppose a function is prototyped as:

1 void foo (int x, int y);

The function is compiled by the C compiler in the symbol library with the name _foo, and the C + + compiler will produce names like _foo_int_int (different compilers may generate different names, but all use the same mechanism, the resulting new name is called "Mangled Name"), _foo _int_int such a name includes the function name, function parameter number and type information, C + + is this mechanism to implement function overloading.

In C + +, the function void foo (int x, int y) is not the same as the symbol generated by the compilation of void foo (int x, float y), which is _foo_int_float.

Similarly, variables in C + + support class member variables and global variables in addition to local variables. The class member variable of the program that the user writes may have the same name as the global variable, and we use the "." to differentiate. In essence, the compiler, when compiling, is similar to the processing of the function and takes a unique name for the variable in the class, which differs from the name of the global variable named in the user program.

3. Connection method without extern "C" declaration

Assume that in C + +, the header file for module A is as follows:

Module a header file moduleA.h

#ifndef module_a_h
#define Module_a_h
int foo (int x, int y);
#endif

Reference the function in module B:

#include "ModuleA.h"

Foo (2,3);

In fact, during the connection phase, the connector looks for symbols like _foo_int_int from the target file Modulea.obj generated by module A!

4, add extern "C" after the declaration of the compilation and connection mode

After adding the extern "C" declaration, the header file of module a becomes:

module A header file ModuleA.h

#ifndef module_a_h
#define Module_a_h
extern "C" int foo (int x, int y);
#endif

Foo (2,3) is still called in the implementation file of Module B, and the result is:

(1) When module a compiles the target code of Foo, it does not have special handling of its name and adopts the C language method;

(2) When the connector looks for foo (2,3) call for the target code of Module B, it looks for the unmodified symbol name _foo.

If the function in module a declares Foo to be an extern "C" type, and Module B contains an extern int foo (int x, int y), module B cannot find the function in module A;

Therefore, it is possible to summarize the true purpose of the extern "C" statement in one sentence (the birth of any grammatical feature in any language is not arbitrary and is driven by demand from the real world.) When we're thinking about a problem, we can't just stay in the language, ask why it's doing it, what the motivation is, so we can understand a lot more in depth: Implementing a mixed programming of C + + and C and other languages.

5 . The customary method of extern "C"

(1) in C + +, references to functions and variables in the C language, in the case of a C language header file (assuming cExample.h), the following processing is required:

extern "C"

{
#include "cExample.h"
}

In the header file of the C language, only the extern type is specified for its external function, and the extern "C" declaration is not supported in the C language, and a compile syntax error occurs when the. c file contains the extern "C".

The author of C + + reference C function Example project contains the source code of the three files as follows:

/* C Language header file: CExample.h */

#ifndef C_example_h
#define C_example_h
extern int Add (int x,int y);
#endif
/* C language Implementation file: CEXAMPLE.C */
#include "cExample.h"
int add (int x, int y)
{
return x + y;
}

C + + implementation file, call Add:cppFile.cpp

extern "C"
{
#include "cExample.h"
}
int main (int argc, char* argv[])
{
Add (2,3);
return 0;
}

If C + + calls a. dll written in C, the header file that includes the. dll or the declaration of the interface function should be added

extern "C" {}

(2) When referencing functions and variables in the C + + language , the header file of C + + must be added extern "C", but the header file that declares extern "C" cannot be directly referenced in C, only the extern "C" defined in C + + should be The function is declared as an extern type.

The author of the C-referenced function example of the three files in the project contains the following source code:

C + + header file CppExample.h

#ifndef Cpp_example_h
#define Cpp_example_h
extern "C" int Add (int x, int y);
#endif


C + + implementation file CppExample.cpp
#include "cppExample.h"
int add (int x, int y) return x + y;
}

/* C Implementation file cfile.c

/* This compiles an error: #include "cExample.h" */
extern int Add (int x, int y);
int main (int argc, char* argv[])
{
Add (2, 3);
return 0;
}

Using VS2012 to generate DLL files (1)

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.