c++ - 解析外部符号时出错

标签 c++

我正在尝试在 Visual Studio 2015 中创建一个简单的 C++ 项目

Peakdetector.h

 #ifndef PEAKDETECTOR_H
 #define PEAKDETECTOR_H
 //-------------------------------------------------------
 #ifdef DLL_BUILD_SETUP
    #ifdef Q_OS_LINUX
        #define DLLSPEC __attribute__((visibility("default")))
  #else
      #define DLLSPEC __declspec(dllexport)
  #endif
 #else
   #ifdef Q_OS_LINUX
      #define DLLSPEC
  #else
      #define DLLSPEC __declspec(dllimport)
   #endif
 #endif
  namespace vpg {
   #ifndef VPG_BUILD_FROM_SOURCE
   class DLLSPEC PeakDetector
  #else
   class PeakDetector
  #endif
       private:
          int __seek(int d) const;
          double __getDuration(int start, int stop);
   }

   inline int PeakDetector::__seek(int d) const
   {
     return ((m_intervalslength + (d % m_intervalslength)) % m_intervalslength);
   }

#endif

PeakDetector.cpp

#include "stdafx.h"
#include "peakdetector.h"

   namespace vpg {
     void PeakDetector::__updateInterval(double _duration)
     {
         //other stuff

     }
}

当我尝试运行这个应用程序时出现错误

LNK2019 unresolved external symbol "__declspec(dllimport) private: int __cdecl vpg::PeakDetector::__seek(int)const " (__imp_?__seek@PeakDetector@vpg@@AEBAHH@Z) referenced in function "private: void __cdecl vpg::PeakDetector::__updateInterval(double)" (?__updateInterval@PeakDetector@vpg@@AEAAXN@Z) MyCustomProject

我是新手,无法弄清楚为什么会出现此错误。我刚刚从示例中复制粘贴了这段代码。如果我遗漏了任何代码,请告诉我。我也没有任何 .lib 文件。

最佳答案

您必须在 Visual Studio 中添加定义的 DLL_BUILD_SETUP

为了做到这一点,你必须去

Project Settings -> C/C++ -> Preprocessor -> Preprocessor definitions

并将定义添加到列表中。

在编译导出符号(在本例中为类)的库时,必须使用规范 __declspec(dllexport),并且在 __declspec(dllimport)使用该库的项目。

我从您提供的源代码中看到,有一个附加定义 VPG_BUILD_FROM_SOURCE 禁用导出以使用静态/内联链接,您可以尝试添加该定义。

关于c++ - 解析外部符号时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45493544/

相关文章:

c++ - Eigen 和 MKL 11.2

javascript - 如何在 Cheerp(C++ 到 JavaScript 转换器)中保留变量名称

c++ - 为什么不 boost 元组运算符 == 和 << 编译?

c++ - 将包含目录添加到 AUTOMOC

C++,实例化一个通用节点,使用模板存储一个对象的实例

c++ - 直接定义转换函数以引用数组的 ISO C++ 方法是什么?

c++ - C++/Qt - 内存分配如何工作?

c++ - SFML 2.0 崩溃(window-d-2 错误

c++ - 通过引用将结构数组传递给函数

c++ - 按位交换()何时不是一个好主意的例子?