VS BUG(5) error C2491: “****“: 不允许 dllimport 函数 的定义

VS BUG(5) error C2491: "****": 不允许 dllimport 函数 的定义

前言

程序员难免要与BUG打交道,本系列将记录自己在VS平台下代码调试过程中遇到的一些BUG,希望对大家遇到相关问题时,可以提供帮助。

一. BUG提示

1>------ 已启动生成: 项目: Project5, 配置: Debug x64 ------
1>  SoundTouchDLL.cpp
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(86): error C2491: “soundtouch_createInstance”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(104): error C2491: “soundtouch_destroyInstance”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(117): error C2491: “soundtouch_getVersionString”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(125): error C2491: “soundtouch_getVersionString2”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(133): error C2491: “soundtouch_getVersionId”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(140): error C2491: “soundtouch_setRate”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(151): error C2491: “soundtouch_setTempo”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(161): error C2491: “soundtouch_setRateChange”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(171): error C2491: “soundtouch_setTempoChange”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(181): error C2491: “soundtouch_setPitch”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(191): error C2491: “soundtouch_setPitchOctaves”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(201): error C2491: “soundtouch_setPitchSemiTones”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(211): error C2491: “soundtouch_setChannels”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(220): error C2491: “soundtouch_setSampleRate”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(235): error C2491: “soundtouch_flush”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(251): error C2491: “soundtouch_putSamples”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(266): error C2491: “soundtouch_putSamples_i16”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(297): error C2491: “soundtouch_clear”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(312): error C2491: “soundtouch_setSetting”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(326): error C2491: “soundtouch_getSetting”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(336): error C2491: “soundtouch_numUnprocessedSamples”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(353): error C2491: “soundtouch_receiveSamples”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(374): error C2491: “soundtouch_receiveSamples_i16”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(422): error C2491: “soundtouch_numSamples”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(432): error C2491: “soundtouch_isEmpty”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(441): error C2491: “bpm_createInstance”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(459): error C2491: “bpm_destroyInstance”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(474): error C2491: “bpm_putSamples”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(487): error C2491: “bpm_putSamples_i16”: 不允许 dllimport 函数 的定义
1>e:\project\vs\project5\project5\soundtouch\soundtouchdll.cpp(522): error C2491: “bpm_getBpm”: 不允许 dllimport 函数 的定义
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

二. 分析解决

在进行语音变速不变调的工程集成应用中,调试时出现上述报错。查询资料可知原工程调用了生成的dll动态链接库,为了减少使用dll时候的设置或者代码编写,因此需要修改dll.h文件中的相关代码,在dll.h文件开头添加如下代码

#ifndef DLL_EXPORT
   #define DECLDIR __declspec(dllimport)
#else
   #define DECLDIR __declspec(dllexport)
#endif

本工程是基于SoundTouchDLL.h进行应用,首先添加DLL_EXPORTS到项目-》属性-》C/C+±》预处理器-》预处理器定义中如下图所示
在这里插入图片描述
在SoundTouchDLL.h文件中的存在此部分代码如下:

 #ifdef DLL_EXPORTS
        #define SOUNDTOUCHDLL_API extern "C" __declspec(dllexport)
    #else
        #define SOUNDTOUCHDLL_API extern "C" __declspec(dllimport)
    #endif

因此无需再进行修改(添加完上述预处理器定义后,工程内的报错提示即已消失),首先清理解决方案后,直接重新生成解决方案,如下图所示生成成功。
在这里插入图片描述
再进行调试运行如下图所示
在这里插入图片描述
运行成功,问题解决。

三. 小结

这个问题是在项目移植过程中出现的,主要原因是宏定义未被识别,故报错,根据相关提示直接在预处理器定义中添加缺失的宏定义一般问题都可以得到解决。希望本文可以帮助到大家解决这个BUG,再给大家两个参考链接。

不允许dllimport函数的定义

c ++:不允许使用dllimport函数的定义,使用visual studio 2010构建(c++: definition of dllimport function not allowed, building with visual studio 2010)

猜你喜欢

转载自blog.csdn.net/sinat_34897952/article/details/125174622