错误C2375:重新定义;不同的联动
api 中的错误位置:
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int CAnyseeUSBTVControllerDlg::InitCaptureDevice()
{
在我的 .h 库类和函数定义中:
class CAnyseeUSBTVControllerDlg : public CDialog
{
// Construction
public:
int InitCaptureDevice(void);
知道如何解决它吗?
“错误 1 错误 C2375: 'CAnyseeUSBTVControllerDlg::InitCaptureDevice' :重新定义;不同的 链接 c:\Program 文件\toATS_DVS\anysee\anyseee30\anyseee30\anyseeUSBTVControllerDlg.cpp 122 anyseee30"
Error place in api:
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int CAnyseeUSBTVControllerDlg::InitCaptureDevice()
{
In my .h library class and function definition:
class CAnyseeUSBTVControllerDlg : public CDialog
{
// Construction
public:
int InitCaptureDevice(void);
Any idea how to resolve it?
"Error 1 error C2375:
'CAnyseeUSBTVControllerDlg::InitCaptureDevice'
: redefinition; different
linkage c:\Program
Files\toATS_DVS\anysee\anyseee30\anyseee30\anyseeUSBTVControllerDlg.cpp 122 anyseee30"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须确保在头文件中使用相同的声明。否则就会被视为不同的方法。
请参阅在 C++ 类中使用 dllimport 和 dllexport
You have to make sure you use the same declaration in your header file. Otherwise it is seen as different methods.
See Using dllimport and dllexport in C++ Classes
这可能会发生,因为
不同的可见性(
extern
与static
)不同的名称修改 (
extern "C"
与extern "C++"
)__declspec(dllimport)
vs__declspec(dllexport)
)。要解决此问题,请为文件启用 /p 以查看它们是如何预处理的(这必须逐个文件地进行,并且将停止为该文件生成 .obj),查找包含结果的 .i 文件。
或者使用 /displayincludes,或者简单地通过代码进行 grep 操作。
This may happen because
different visibility (
extern
vsstatic
)different name mangling (
extern "C"
vsextern "C++"
)__declspec(dllimport)
vs__declspec(dllexport)
).To solve, enable /p for files to see how they are preprocessed (this has to be in a file by file basis, and will stop generating .obj for that file), look for a .i file with the result.
Or using /displayincludes, or simply greping thru the code.
您可以在
.cpp
文件中声明DLLEXPORT
,但不能在头文件中声明(因为否则编译器会将这些函数视为不同的函数)。将您的定义也设为
DLLEXPORT
。You can have
DLLEXPORT
stated in.cpp
file, but not in a header file (because otherwise compiler treats these functions as different ones).Make your definition also
DLLEXPORT
.来自 http://tldp.org/HOWTO/C++-dlopen/thesolution.html
我相信静态成员也可以
extern "C"
,但你不能直接做你想做的事情。您需要创建一个仅 C 语言的包装器接口来调用您的类成员函数。然后,您可以extern "C"
包装器并将其公开到 DLL 外部。From http://tldp.org/HOWTO/C++-dlopen/thesolution.html
I believe static members may also be possible to
extern "C"
, but you can't do what you're trying to do directly. You'll need to make a C-only wrapper interface that calls your class member functions. You can thenextern "C"
the wrappers and expose that outside your DLL.关键的事情,不是函数名称,也不是我对尾随返回类型的使用,而是将 #define __declspec 的名称放在要导出的函数前面,就像类型一样。
您也可以在函数定义中执行相同的操作:
函数实现并不重要,只需将 FOO_API 放在前面即可。
The key things, not so much the function names, or my use of the trailing return type, is that you put the name of the #defined __declspec in front of the function you want to export, much like you would a type.
You would also do the same in the function definition:
The function implementation isn't important, just that you put FOO_API in front.
今天我遇到了同样的问题,对我来说,我未能在课前包含该类型。
也就是说,我必须将 : 更改
为
旁注:
这将允许将您的项目构建为
dll
或lib
而两者都不是(即通过包含它们来使用它们)你也可以在Linux下编译这段代码,所以这里没有特定于平台的内容。如果您在 Visual Studio 中并想要构建 dll,只需转到“属性”>“C/C++”>“命令行”并输入 :
并将
CORE
替换为您自己指定的名称。Today I faced the same issue and for me, I had failed to include the type before my class.
That is I had to change :
to
Side note:
This will allow for building your project as a
dll
or alib
and neither of them (i.e. use them by including them) and you can also compile this code under linux, so nothing platform specific here.If you are in visual studio and want to build a dll, just go to Properties>C/C++>CommandLine and enter :
and replace
CORE
with your own designated name.