问题描述
我想为我的工作中使用C ++编写的组件之一创建一个动态库(.dll).几个函数在.h文件中声明为extern,并在相应的.cpp文件中引用.链接它们时,我收到未解决的符号错误.
I want to create a dynamic library(.dll) for one of the components written in C++, used in my work.Couple of functions are declared as extern in a .h file and being refernced in the corresponding .cpp file.While linking them i'm getting Unresolved symbol error.
xxxx.obj:错误LNK2019:未解析的外部符号"int __cdecl run_xxx(int,char * * const,struct st_xx *)"(?run_xxx @@ YAHHQEAPEADPEAUst_xx @@@ Z)在函数"int __cdecl start_xx(int,int,int,char * * const)(?start_xx @@ YAHHHHQEAPEAD @ Z)
xxxx.obj : error LNK2019: unresolved external symbol "int __cdecl run_xxx(int,char * * const,struct st_xx *)" (?run_xxx@@YAHHQEAPEADPEAUst_xx@@@Z) referenced in function "int __cdecl start_xx(int,int,int,char * * const)" (?start_xx@@YAHHHHQEAPEAD@Z)
这些函数的用法就像,它们需要由使用此库的任何应用程序定义.因此,这些功能的定义将仅在使用此库的应用程序中进行.因此,在链接时,我无法添加任何其他具有这些API定义的库.
The usage of these functions is like, they need to be defined by any application using this library. So the definition for these functions will only be in the application using this library.Thus i could not add any other library having the definition of these APIs while linking.
创建dll时如何处理extern API?
How the extern APIs need to be handled while creating dll?
推荐答案
DLL无法链接到应用程序中的功能,就像DLL和应用程序是一个大型C ++程序一样.
The DLL can't link to functions in the application as if the DLL and application was one big C++ program.
Windows DLL本身更像是一个可执行文件:它不像C或C ++静态库.
A Windows DLL is more like an executable in its own right: it's not like C or C++ static library.
最简单的方法是让应用程序在某些初始化调用中将指针传递给回调/自定义函数和DLL.更高级别的您可以将接口传递给DLL.第三种选择是,这些功能由应用程序必须提供的它们自己的DLL提供.
The simplest approach is to let the application pass pointers to the callback/customization functions, to the DLL, in some initialization call. More high level you can pass an interface to the DLL. A third alternative is that the functions are provided by their own DLL which the application must supply.
基于可执行文件和DLL都使用可移植可执行文件格式的事实,还有一种可能的肮脏低级方法,因此,原则上,可执行文件可以导出功能以供DLL使用,从而实际上充当了可执行文件和DLL.但是,很久以前,我在那个级别上做了一些事情,所以我不能保证它是可行的,无论如何我还是建议不要这样做.
There is also a possible dirty low level approach based on the fact that both executables and DLLs use the Portable Executable format, so in principle an executable can export functions for consumption by a DLL, in effect acting as both executable and DLL. However it's so long ago I did things at that level that I can't guarantee that it's doable, and anyway I recommend against it.
这篇关于如何链接在.h文件中声明为extern的函数以创建.dll文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!