The link problem of dynamic library in C++builder

Source: Internet
Author: User
Tags join

There are two main ways in which a dynamic library is linked to an application: implicit linking and explicit linking. Implicit linking is a common method.

If the application and dynamic libraries are developed separately on different development platforms, the import library (lib file) of the dynamic library may be incompatible with the import library format required by the application's development platform, resulting in a program link error when an application is implicitly linked to a dynamic library: contains invalid OMF Record For example, this error occurs when you link a dynamic library made by Visual C + + on the C++builder development platform. To solve this problem, there are two methods: explicit Join method and using the Import library generation tool provided in C++builder.

Explicit connections: Explicit connections do not need to include the import library and the corresponding header file in the project, just put the dynamic library in the specified directory. Explicitly load and unload DLLs through function calls in an application, invoking the DLL's exported functions through function pointers.

Steps:

1. Call the LoadLibrary function to load the DLL and get the module handle;

2. Call the GetProcAddress function to get a pointer to the specified exported function;

3. Call the function with a pointer to the function;

4. After use, release the DLL with FreeLibrary.

Example:

Use VC + + to make a dynamic library:

Select New→project→win32-dynamic-link Library and join Addit.cpp

  extern ″C″
   {
   void __declspec( dllexport ) addit(int a, int b, int *c)
   {
    *c = a + b;
   }
   }
   addit.h
   extern ″C″
   {void addit(int a, int b, int *c);}

The compilation chain is delivered into Addit.dll and addit.lib. Call the Addit function in the C++builder program.

In the C++builder program:

  {
   HINSTANCE handle; //DLLa模块的句柄
  FARPROC lpFarProc;
   void (*lpaddit)(int,int,int *);//指向addit函数的指针
   int ntemp;
   handle = LoadLibrary(″addit.dll″); //装载addit.dll, 得到该库句柄 addit.dll位于当前目录下
   lpFarProc = GetProcAddress(handle,″addit″);   //得到指向函数addit的指针
   lpaddit = (void(__cdecl *)(int, int, int *))lpFarProc; //指针类型转换
   lpaddit(2,3,&ntemp); //使用addit函数
   FreeLibrary(handle); //将addit.dll从程序中卸掉
   }

This program is passed under VC + + 5.0 and C++builder 3.0.

Use the Import Library build tool provided in C++builder: preprocessing first, then implicitly linking.

Steps:

1. Regenerate the import library (xxx.lib) of the Dynamic Library (Xxx.dll) with the Implib.exe tool provided by C++builder. The order is as follows:

Implib addit.lib Addit.dll.

Addit.dll is an existing dynamic library, Addit.lib is the import library to be built. The resulting import library Addit.lib format is compatible with the C++builder development platform;

2. In the header file addit.h of the dynamic library, the output function is redefined and the statement is as follows:

extern __stdcall void Addit (int a, int b, int *c);

3. Then the implicit link method is used to add the regenerated import library (addit.lib) and the Addit.h header file (the re) to the project of the C++builder application for compilation and connection.

Addit.dll according to the above steps, in VC + + 5.0 and C++builder 3.0 pass.

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.