【问题标题】:Error while trying to compile a DLL with a static class function in VC++尝试在 VC++ 中使用静态类函数编译 DLL 时出错
【发布时间】:2011-08-31 18:17:28
【问题描述】:

我正在尝试使用我在独立应用程序中制作和测试过的一些功能。现在我正在尝试创建一个 DLL,我收到了一些错误,指出该函数已被重新定义并且返回参数不匹配。这似乎是我唯一一次遇到这些错误,我已经通过删除这个类对其进行了测试,它编译得很好,以及带有直接引用这些错误的 main.cpp 的独立应用程序。以下是错误以及 h 和 cpp 文件:

错误 7 错误 C2371: 'Parser::parse' : 重新定义;不同的基础 类型 c:\users\seb\documents\visual studio 2005\projects\TestDLL\TestDLL\parser.cpp 17

错误 4 错误 C2526: 'Parser::parse' : C 链接函数不能 返回 C++ 类 'std::vector<_ty>' c:\users\seb\documents\visual 工作室 2005\projects\TestDLL\TestDLL\parser.h 28

错误 6 错误 C2556: 'IDVec Parser::parse(const char *)' : 重载 函数仅通过返回类型与 'void Parser::parse(const char *)' c:\users\seb\documents\visual studio 2005\projects\TestDLL\TestDLL\parser.cpp 17

还可以从 .cpp 中找到 .h 文件和函数:

Parser.h

#ifndef PARSER_H
#define PARSER_H

#if defined DLL_EXPORT
#define TESTAPI __declspec(dllexport)
#else
#define TESTAPI __declspec(dllimport)
#endif

#include <iostream>
#include <vector>

typedef struct _ListEntry {
    std::string id, path;
} ListEntry;

typedef std::vector<ListEntry> IDVec;

extern "C"
{
    class TESTAPI Parser
    {
    public:
        Parser(void);
        ~Parser(void);
        static IDVec parse(const char* Buffer);
    private:
        static size_t nextLine(std::string& rstrText, size_t pos);
        static std::string nextWord(std::string& rstrText, size_t pos);
        static void fixOSSpecificPath(std::string& rstrPath);
    };
}

#endif

Parser.cpp

IDVec Parser::parse(const char* Buffer) 
{

    std::string s = Buffer;
    IDVec v;

    // Doing stuff here

    return v;
}

感谢您的建议

【问题讨论】:

  • 您希望通过将 C++ 类包装在 extern "C" 块中来实现什么?此外,如果所有成员都是静态的,那么为什么它首先是一个类。
  • 去掉`extern "C" 链接。 C 中没有类和成员函数。
  • 这是我第一次制作 DLL,我只是按照他们所说的基本教程进行操作。

标签: c++ visual-studio dll


【解决方案1】:

删除你的类定义周围的extern "C"

【讨论】:

  • 这似乎确实有效。不过,我确实有一个后续问题。大多数 DLL 创建教程都在外部“C”块中显示类。这有什么用还是没有必要。
  • 那是一些奇怪的教程。用 extern "C" 盲目地包装 C++ 类是没有意义的。这里有很好的解释 extern "C" 的作用:stackoverflow.com/questions/1041866/extern-c
猜你喜欢
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多