9

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 Files\toATS_DVS\anysee\anyseee30\anyseee30\anyseeUSBTVControllerDlg.cpp 122 anyseee30”

4

6 回答 6

6

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

于 2010-09-09T13:54:59.130 回答
2

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.

于 2010-09-09T13:55:16.773 回答
2

这可能是因为

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

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

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

于 2015-03-17T18:11:35.417 回答
1

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.

于 2010-09-09T14:06:50.520 回答
0
//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;
    };
}

关键的事情,而不是函数名称,或者我对尾随返回类型的使用,是您将#defined __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 放在前面。

于 2017-04-14T21:28:46.743 回答
0

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

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:
...
};

边注:

这将允许将您的项目构建为 adll或 alib并且两者都不是(即通过包含它们来使用它们)并且您还可以在 linux 下编译此代码,因此这里没有特定于平台的内容。

如果您在 Visual Studio 中并想构建一个 dll,只需转到 Properties>C/C++>CommandLine 并输入:

/D_CORE_BUILD_DLL 

并替换CORE为您自己指定的名称。

于 2020-04-05T09:59:36.577 回答