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

发布于 2024-09-18 09:34:17 字数 553 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

枫林﹌晚霞¤ 2024-09-25 09:34:17

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

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

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

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

各空 2024-09-25 09:34:17

这可能会发生,因为

  1. 您在不同的地方定义了函数的原型
    不同的可见性(externstatic
  2. 与上面相同,但是
    不同的名称修改 (extern "C"extern "C++")
  3. 与上面相同,但不同的 dll 导出 (__declspec(dllimport) vs
    __declspec(dllexport))。

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

或者使用 /displayincludes,或者简单地通过代码进行 grep 操作。

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.

萤火眠眠 2024-09-25 09:34:17

您可以在 .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.

眼睛会笑 2024-09-25 09:34:17

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

C++ 有一个特殊的关键字来声明
具有 C 绑定的函数:extern "C"。
声明为 extern "C" 的函数使用
函数名称作为符号名称,只需
作为 C 函数。为此,仅
可以声明非成员函数
作为外部“C”,并且它们不能是
超载。

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

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.

故笙诉离歌 2024-09-25 09:34:17
//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;
    };
}

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

您也可以在函数定义中执行相同的操作:

//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);
     }
}

函数实现并不重要,只需将 FOO_API 放在前面即可。

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

沉睡月亮 2024-09-25 09:34:17

今天我遇到了同样的问题,对我来说,我未能在课前包含该类型。
也就是说,我必须将 : 更改

class Core
{
private:

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

#ifndef CORE_H
#define CORE_H
/* If we are we on Windows, we want a single define for it.*/
#if !defined(_WIN32) && (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
#define _WIN32
#endif /* _WIN32 */

#if defined(_WIN32) && defined(_CORE_BUILD_DLL)
/* We are building FV as a Win32 DLL */
#define CORE_API __declspec(dllexport)
#elif defined(_WIN32) && defined(CORE_DLL)
/* We are calling FV as a Win32 DLL */
#define CORE_API __declspec(dllimport)
#elif defined(__GNUC__) && defined(_CORE_BUILD_DLL)
/* We are building FV as a shared / dynamic library */
#define CORE_API __attribute__((visibility("default")))
#else
/* We are building or calling CORE as a static library */
#define CORE_API
#endif
class CORE_API Core
{
private:

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

旁注:

这将允许将您的项目构建为 dlllib 而两者都不是(即通过包含它们来使用它们)你也可以在Linux下编译这段代码,所以这里没有特定于平台的内容。

如果您在 Visual Studio 中并想要构建 dll,只需转到“属性”>“C/C++”>“命令行”并输入 :

/D_CORE_BUILD_DLL 

并将 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 :

class Core
{
private:

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

to

#ifndef CORE_H
#define CORE_H
/* If we are we on Windows, we want a single define for it.*/
#if !defined(_WIN32) && (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
#define _WIN32
#endif /* _WIN32 */

#if defined(_WIN32) && defined(_CORE_BUILD_DLL)
/* We are building FV as a Win32 DLL */
#define CORE_API __declspec(dllexport)
#elif defined(_WIN32) && defined(CORE_DLL)
/* We are calling FV as a Win32 DLL */
#define CORE_API __declspec(dllimport)
#elif defined(__GNUC__) && defined(_CORE_BUILD_DLL)
/* We are building FV as a shared / dynamic library */
#define CORE_API __attribute__((visibility("default")))
#else
/* We are building or calling CORE as a static library */
#define CORE_API
#endif
class CORE_API Core
{
private:

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

Side note:

This will allow for building your project as a dll or a lib 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 :

/D_CORE_BUILD_DLL 

and replace CORE with your own designated name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文