OpenCV's Cv_export

Source: Internet
Author: User

The usage of cv_exports appears:

Class Cv_exports matexpr;

Template<typename _tp> class Cv_exports size_;

Definition of Cv_export:

#if (defined WIN32 | | defined WIN64) && defined Cvapi_exports
#define Cv_exports __declspec (dllexport)
#else
#define Cv_exports
#endif

The functions that are important to the operation of the DLL are the following two:
__declspec (dllimport), __declspec (dllexport),

__declspec (dllimport) keyword to import a function or variable. Import of Functions     when you need to use a function in a DLL, you often do not need to display the import function, the compiler can automatically complete. But if you display the Import function, the compiler will produce better quality code. Because the compiler knows exactly whether a function is in a DLL, it can generate better code and no longer requires an indirect call transfer.     Win32 's PE format (portable Executable format) places all import addresses in an import address table. The following is a concrete example that uses the __declspec (dllimport) Import function and does not use the difference:     Suppose Func is a function in a DLL, The Func function is now called in the main function of the. exe to be generated, and the Func function (that is, no: __declspec (dllimport)) is imported without displaying the following code example:     int main ()      {        func ();    } The compiler will produce an invocation code like this:   & nbsp Call Func then, the linker translates the invocation into code like this:     called 0x40000001      ; OX40000001 is the address of "Func" and the linker will produce a thunk, shaped like:     0x40000001:jmp DWORD PTR __imp_func imp_ here Func is the address of the function slot in the Import address table of the Func function in. exe. The loader then only needs to update the import address table of the. exe at load time.     if the __declspec (dllimport) display is used to import functions, then the linker does not produce thunk (if not required) and directly generates an indirect call. Therefore, the following code:     __declspec (dllimport) void func1 (void);
void Main (void)
{
Func1 ();
The following invocation instruction will be invoked: Call DWORD PTR __IMP_FUNC1 Therefore, the display of the import function can effectively reduce the target code (because it does not produce thunk). In addition, functions that are used outside of DLLs in DLLs can also be done to improve space and time efficiency. The import of a variable differs from a function in that it is necessary to import variables when using a variable in a DLL. Use the __declspec (dllimport) keyword to import variables. If you export a variable using. def in a DLL, you should use the data modifier instead of the constant that has been abandoned. Because constant may need to use pointers to access variables indirectly, it is not certain when there will be a problem.

First look at the code: here is the code in the dll.h inside the dev-c++ DLL, which has one: _declspec (dllexport)

#ifndef _dll_h_
#define _DLL_H_//anti-duplication definition

#if Building_dll
# define DllImport __declspec (dllexport)
#else/* Not Building_dll * *
# define DllImport __declspec (dllimport)
#endif/* Not Building_dll * *
dllimport void HelloWorld (void);
#endif/* _dll_h_ * *

The _DELCSPCE (dllexport) in the above code is defined as a macro, which can improve the readability of the program! This is to define the function as an export function, which means that the function is called by a program other than the one that contains the function! This statement is: void Helloword (void):

Excerpt from MSDN: In the 32-bit compiler version, you can use the __declspec (dllexport) keyword to export data, functions, classes, or class member functions from a DLL. __declspec (dllexport) adds an export directive to an object file

To export a function, the __declspec (dllexport) keyword must appear to the left of the calling convention keyword, if a keyword is specified. For example:

__declspec (dllexport) void __cdecl Function1 (void);

To export all public data members and member functions in a class, the keyword must appear to the left of the class name, as follows:

Class __declspec (dllexport) cexampleexport:public CObject
{. class definition ...};

When you build a DLL, you typically create a header file that contains the function prototypes and/or classes you are exporting, and add __declspec (dllexport) to the declaration in the header file. To improve the readability of your code, define a macro for __declspec (dllexport) and use the macro for each symbol that you are exporting:

#define Dllexport   

__declspec (dllexport) stores the function name in the exported table of the DLL.

To determine the method (. def file or __declspec (dllexport) keyword) to use to export a function, answer the following questions:

Whether you want to add additional exported functions all the time.

Who wants to use the DLL. For example, are Third-party DLLs that are used by many executables that cannot be rebuilt or DLLs that are used only by applications that can be easily regenerated. use. The pros and cons of DEF files

Exporting a function in a. def file allows you to control the export ordinal. When additional exported functions are added to the DLL, they can be assigned a higher ordinal value (higher than any other exported function). When you do this, an application that uses implicit linking does not have to be relink with the new import library that contains the new function. This is important, for example, when designing a third-party DLL that will be used by many applications. You can continuously enhance DLLs by adding additional functionality while ensuring that existing applications continue to use the new DLLs normally. MFC DLLs are generated by using a. def file.

Another advantage of using a. def file is that you can use the NONAME property to export a function that simply places an ordinal in the DLL's export table. For DLLs with a large number of exported functions, the NONAME property can be used to reduce the size of the DLL file. For information about writing module definition statements, see the Rules for module definition statements. For more information about ordinal exports, see exporting functions from a DLL by ordinal rather than by name.

The main disadvantage of using a. def file is that when you export a function in a C + + file, you must place the decorated name in the. def file, or you can use the external "C" to define the exported function with the standard C link to avoid the compiler's name modification.

If you need to place the decorated name in a. def file, you can get the decorated name by using the DUMPBIN tool or the/MAP linker option. Note that the compiler-generated decorated name is compiler-specific. If you place the decorated name produced by the Visual C + + compiler in a. def file, the application that is linked to the DLL must also be generated in the same version of Visual C + +, so that the decorated name in the calling application can match the exported name in the DLL's. def file. advantages and disadvantages of using __declspec (dllexport)

Using __declspec (dllexport) is convenient because you do not have to consider maintaining the. def file and getting the decorated name of the exported function. This method is useful, for example, if you design a DLL for use by your own controlled application. If you rebuild the DLL with the new export function, you must also rebuild the application, because if you recompile with a different version of the compiler, the decorated name of the exported C + + function may change.

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

The difference between Lib and DLL:

1. What is the difference in the way of use?

2. What is the reason.

In fact, Lib no matter how, will use.

One is the use of Lib alone, the other is the way of lib+dll (usually referred to as the DLL).

Lib is used separately:

1. lib contains the function code itself, including not only the header file, but also the source code, in the compile-time directly into the program, called the Static link library. A link-generated program can run independently.

That is, the Lib file is statically compiled, indexed and implemented in it.

2. The same machine, many of the same program to run, each program will load a code base.

3. Any changes, all need to recompile, link, this large program is very inconvenient, upgrade also trouble. Sometimes, when you change a data structure, you need to recompile the entire project, rather than just compiling and linking to a single lib.


Lib+dll Way:

1. Lib contains information about the DLL file where the function is located and the location of the function in the file (the entry), which is provided by the DLL loaded in the process space by the runtime, known as the dynamic link library. The generated program requires a DLL to work with.

That is, Lib is generally some index information, record the entry and location of functions in the DLL, the specific contents of the function in the DLL. The application uses the Lib file to link to the DLL file. In an application's executable file, you save memory resources by storing not the called function code, but the address of the corresponding function code in the DLL.

2. Multiple programs running, sharing a code. Save memory.

3. When a function in a DLL changes, it is not necessary to recompile or relink the application that uses them as long as the function's arguments and return values have not changed.

Differences in usage: 1. Static lib requires. H and Lib. The collection of obj, which is equivalent to CPP compilation, when generating lib. When linking, specify the location of Lib.
2. Dynamic Lib is essentially how a DLL is used. When you build the DLL, a lib,lib is generated that contains the index information. When compiling links, you need. H,lib. At run time, programs and DLLs are required.

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.