PREFER C ++ (3)

zhaozj2021-02-17  53

Note:

1. Some precautions when using C to drive the DLL (note when compiled when connected to .CPP)

Generally speaking, you must first use externaln "c" {} in the header file of the Declaration Export Function to ensure the matching of the function name. This step is responsible for the operating system definition for the export function in the drive. ENUMPRINTPROCESSORDATATYPESW in the print processor in 2000 as an example:

The declaration of this function in Winspool.h is:

Bool

WinAPI

EnumprintProcessRDataTypesw

IN LPWSTR PNAME,

In LPWSTR PPRINTPROCESSORNAME,

In DWord Level,

Out lpbyte pdattypes,

In dword cbbuf,

Out LPDWORD PCBNEEDED,

Out LPDWORD PCRETURNED

);

And on the beginning of Winspool.h:

#ifdef __cplusplus

Extern "C" {

#ENDIF

At the end:

#ifdef __cplusplus

}

#ENDIF

That is to say: When the EnumPrintProcessRDataTypeSw is compiled and connected, according to the C specification, refer to the specified call rule to generate a file name, and ignore the extension name is .c or .cpp. For the above functions, because of the WINAPI (STDCALL) call rules, the function generated in the target file is named @ 28 for the MSVC . If there is no externaln "c", for the C file, the name analysis is performed according to the C specification. The result may be? EnumprintProcessRDataTypesw @@ yghpau_printprocessor @@ k @ z (Not necessarily this name, Example). So, if you want to use some functions in C and C , you must use Extern "C".

Due to function naming specifications, there are different compilers. In order to share a DLL between various compilers, you must use the .def file. The .def file is equivalent to adding an alias to the compiled function name, but the actual two still point to the same thing (very like C ). For example, in the .def file of the 2000 print processor:

Exports

EnumprintProcessRDataTypesw

------------

This way to printed the processor is actually exported by EnumPrintProcessRDataTypeSw. As for import, MSVC does not find relevant information from the complete _UnumprintprocessRocessRDataTypesw @ 28, from the truncated EnumPrintProcessRDataTypeSw. However, Jeffrey Richter said in the Programming Application for Windows, "The Microsoft's join program has been properly operated."

For MSVC , this rule is also true for C programs, that is, the MS connector can complete the output name to that large 砣 C name from the output name of EnumPrintProcessRDataTypesw. What does this mean? The following conclusions are available in some occasions, but there is not much thing that should be used. If you only use MS, you can

The implementation function in the DLL contains the relevant header file in the application, indicating the function name in .def. No need to add (including Extern "C", __ Declspec, Dllimport, DLlexPort ---). Just like this:

Testdll.cpp ---- àtestdll.exe application

#include

#include

Using std :: cout;

INT WINAPI Add (INT I, INT J);

Int main (int Argc, char * argv [])

{

INT i = add (6,7);

Cout << i;

Return 0;

}

Mathadd.cpp ------ àmathadddll DLL program

#include

Int WinApi Add (Int i, int J)

{

Return I J;

}

Mathadd.def

Library Mathadd

Exports

Add

This will run very well.

About __DECLSPEC (DLLEXPORT): If you don't use .def, use this keyword to specify the output function and variable.

About __DECLSPEC (DLLIMPORT): It is easy to optimize the compiler.

2, PREFER C 6 parts:

1, type system

2, resource management

3, error handling

4, thin say inline

5, template and standard library ◎

6, running time polymorphism

7. Structured, OO, GP

All of this is what I read, I am ready to use the program, and some are experienced.

Hope C to a broader application.

3. About the release version of the VC debugging release

The first thing to be clear is that there is a variety of symbolic information, the release version is adjustable. Think about Symbols with Windows.

The steps are as follows:

1.Project Setting-> C / C -> Debug Info (Multi-Box) This is selected here in Program Database

2. Project setting-> Link Select Generate Debug Info

3. Project setting-> Link on category selection Debug and later Select Generate MapFile and

Debug Info and Microsoft Format.

PS:

On the language, I think C is better than C, because many improvements are imported according to problems in reality, and import. It is also much better than other languages, as various language features are added. Therefore, there is, so he is complex.

转载请注明原文地址:https://www.9cbs.com/read-28840.html

New Post(0)