error C2375: redefinition; different linkage

喜夏-厌秋 提交于 2020-02-23 11:00:09

问题


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"


回答1:


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




回答2:


From 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.

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.




回答3:


This may happen because

  1. You defined the prototype of a function in different places with different visibility (extern vs 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)).

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.




回答4:


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.




回答5:


//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.

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.



来源:https://stackoverflow.com/questions/3677157/error-c2375-redefinition-different-linkage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!