我正在尝试在 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/