Linux 错误 C2375:重新定义;不同的联系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:20:56  来源:igfitidea点击:

error C2375: redefinition; different linkage

c++redefinition

提问by CarolusPl

Error place in api:

api 中的错误位置:

#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int CAnyseeUSBTVControllerDlg::InitCaptureDevice()
{

In my .h library class and function definition:

在我的 .h 库类和函数定义中:

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"

“错误 1 ​​错误 C2375:'CANyseeUSBTVControllerDlg::InitCaptureDevice':重新定义;不同的链接 c:\Program Files\toATS_DVS\anysee\anyseee30\anyseee30\anyseeUSBTVControllerDlg.cpp 122 anyseee30”

回答by Rod

You have to make sure you use the same declaration in your header file. Otherwise it is seen as different methods.

您必须确保在头文件中使用相同的声明。否则,它被视为不同的方法。

class CAnyseeUSBTVControllerDlg : public CDialog
{
// Construction
public:
    int InitCaptureDevice(void);
    DLLEXPORT int CaptureDevice(void);

See Using dllimport and dllexport in C++ Classes

请参见在 C++ 类中使用 dllimport 和 dllexport

回答by Yippie-Ki-Yay

You can have DLLEXPORTstated in .cppfile, but not in a header file (because otherwise compiler treats these functions as different ones).

您可以DLLEXPORT.cpp文件中声明,但不能在头文件中声明(否则编译器会将这些函数视为不同的函数)。

Make your definition also DLLEXPORT.

也做出你的定义DLLEXPORT

回答by Mark B

From http://tldp.org/HOWTO/C++-dlopen/thesolution.html

来自http://tldp.org/HOWTO/C++-dlopen/thesolution.html

C++ has a special keyword to declare a function with C bindings: extern "C". A function declared as extern "C" uses the function name as symbol name, just as a C function. For that reason, only non-member functions can be declared as extern "C", and they cannot be overloaded.

C++ 有一个特殊的关键字来声明一个带有 C 绑定的函数:extern "C"。声明为 extern "C" 的函数使用函数名作为符号名,就像 C 函数一样。因此,只能将非成员函数声明为 extern "C",并且它们不能被重载。

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 then extern "C"the wrappers and expose that outside your DLL.

我相信静态成员也可以extern "C",但你不能直接做你想做的事。您需要创建一个仅 C 语言的包装器接口来调用您的类成员函数。然后extern "C",您可以使用包装器并将其暴露在您的 DLL 之外。

回答by Fernando Gonzalez Sanchez

This may happen because

这可能是因为

  1. You defined the prototype of a function in different places with different visibility (externvs static)
  2. Same as above but different name mangling (extern "C"vs extern "C++")
  3. Same as above but different dll export (__declspec(dllimport)vs __declspec(dllexport)).
  1. 您在不同可见性(externvs static)的不同地方定义了一个函数的原型
  2. 与上面相同但名称修改不同(extern "C"vs extern "C++"
  3. 与上面相同,但不同的 dll 导出(__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.

要解决此问题,请为文件启用 /p 以查看它们是如何预处理的(这必须以文件为基础,并且将停止为该文件生成 .obj),查找带有结果的 .i 文件。

Or using /displayincludes, or simply greping thru the code.

或者使用/displayincludes,或者简单地通过代码greping。

回答by Gary Stewart

//foo.h

#pragma once
#ifdef FOO_EXPORTS
#define FOO_API __declspec(dllexport)
#else
#define FOO_API __declspec(dllimport)
#endif

namespace foo
{
    class Baz
    {
    public:
        FOO_API static auto say_hello() -> void;
    };
}

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.

关键不是函数名称,也不是我对尾随返回类型的使用,而是将#defined __declspec 的名称放在要导出的函数前面,就像放置类型一样。

You would also do the same in the function definition:

你也可以在函数定义中做同样的事情:

//foo.cpp

#include "foo.h"

namespace foo 
{
    FOO_API auto Baz::say_hello() -> void
    {
        do 
        { 
            MessageBox(nullptr, L"Seems to be working okay!", L"OK", MB_OK); 
            exit(1); 
        } 
        while (0);
     }
}

The function implementation isn't important, just that you put FOO_API in front.

函数实现并不重要,只是你把 FOO_API 放在前面。

回答by Rika

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 :

今天我遇到了同样的问题,对我来说,我没有在课前包含这个类型。那是我必须改变:

class Core
{
private:

    py::object cls;
    py::object obj;
    py::object startFunc;
    py::object startFuncAsync;
    py::object stopFunc;
...
public:
...
};

to

class COREAPI Core
{
private:

    py::object cls;
    py::object obj;
    py::object startFunc;
    py::object startFuncAsync;
    py::object stopFunc;
...
public:
...
};