VS2010封装DLL时报错:error LNK2019: 无法解析的外部符号 "__declspec(dllimport)

本文介绍了解决VS2010在封装DLL时遇到的errorLNK2019错误的方法。通过调整项目的字符集设置,并在预处理器定义中加入特定宏,有效解决了DLL与EXE间因字符集不匹配导致的问题。

VS2010封装DLL时报错:

 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) const CTripLineDetection::`vftable'" (__imp_??_7CTripLineDetection@@6B@),该符号在函数 "public: __thiscall CTripLineDetection::CTripLineDetection(void)" (??0CTripLineDetection@@QAE@XZ) 中被引用1>M:\TEST\SVNNew4.2\AllNeeds\TripLineDetection.dll : fatal error LNK1120: 1 个无法解析的外部命令


网上查找解决方法,有的网友建议:

采用一个工程生成DLL文件给另一个工程执行,但每次执行时都出现 error LNK2019: 无法解析的外部符号 "__declspec(dllimport)错误。检查了好几遍代码都觉得没有问题。

最后发现是字符集出现问题。生成DLL文件的字符集是Unicode而生成exe文件的字符集为默认的ASCII。

只要统一字符集即可解决问题:

VS2005的c++项目默认字符集是使用 Unicode 字符集,在项目属性->配置属性-->常规->项目默认值->字符集中设置字符集即可。

补充VC6.0是默认采用ASCII字符集的。


但我发现工程中 默认字符集本就是使用 Unicode 字符集,采用这种方法,解决不了问题,程序还是会报错

解决方法为:

假设新定义的DLL名为 NEWFUNCDLL

属性->配置属性->C/C++->预处理器->预处理器定义中加入;NEWFUNCDLL

问题即可解决
{ "FileVersion": 3, "Version": 1, "VersionName": "1.0", "FriendlyName": "BlackHttpRequest", "Description": "Http请求封装插件", "Category": "Other", "CreatedBy": "Black", "CreatedByURL": "", "DocsURL": "", "MarketplaceURL": "", "SupportURL": "", "CanContainContent": true, "IsBetaVersion": true, "IsExperimentalVersion": false, "Installed": false, "Modules": [ { "Name": "BlackHttpRequest", "Type": "Runtime", "LoadingPhase": "Default" } ] } // Copyright Epic Games, Inc. All Rights Reserved. using UnrealBuildTool; public class BlackHttpRequest : ModuleRules { public BlackHttpRequest(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; PublicIncludePaths.AddRange( new string[] { // ... add public include paths required here ... } ); PrivateIncludePaths.AddRange( new string[] { // ... add other private include paths required here ... } ); PublicDependencyModuleNames.AddRange( new string[] { "Core", "Engine", "HTTP", "Json", "JsonUtilities" // ... add other public dependencies that you statically link with here ... } ); PrivateDependencyModuleNames.AddRange( new string[] { "CoreUObject", "Engine", "Slate", "SlateCore", // ... add private dependencies that you statically link with here ... } ); DynamicallyLoadedModuleNames.AddRange( new string[] { // ... add any modules that your module loads dynamically here ... } ); } } // Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Engine/DeveloperSettings.h" #include "BlackHttpSettings.generated.h" /** * */ UCLASS(config = Game, defaultconfig, meta = (DisplayName = "Http插件配置项")) class BLACKHTTPREQUEST_API UBlackHttpSettings : public UDeveloperSettings { GENERATED_BODY() public: UBlackHttpSettings(); // UBlackHttpSettings(const FObjectInitializer& ObjectInitializer); // // // 基础URL // UPROPERTY(Config, EditAnywhere, BlueprintReadOnly, Category = "HTTP Settings", meta = (DisplayName = "基础URL")) // FString BaseUrl; // // // 默认请求头 // UPROPERTY(Config, EditAnywhere, Category = "HTTP Settings", meta = (DisplayName = "Default Headers")) // TMap<FString, FString> DefaultHeaders; // // // 超间(秒) // UPROPERTY(Config, EditAnywhere, Category = "HTTP Settings", meta = (DisplayName = "Default Timeout (seconds)")) // float DefaultTimeout; // // // 是否启用日志记录 // UPROPERTY(Config, EditAnywhere, Category = "HTTP Settings", meta = (DisplayName = "Enable Logging")) // bool bEnableLogging; // // // 获取单例实例 // static UBlackHttpSettings* Get(); }; // Fill out your copyright notice in the Description page of Project Settings. #include "BlackHttpSettings.h" // UBlackHttpSettings::UBlackHttpSettings(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) // { // // 设置默认值 // BaseUrl = TEXT("https://jsonplaceholder.typicode.com"); // DefaultTimeout = 10.0f; // bEnableLogging = true; // // // 添加默认头部 // DefaultHeaders.Add(TEXT("Content-Type"), TEXT("application/json")); // DefaultHeaders.Add(TEXT("Accept"), TEXT("application/json")); // } // // UBlackHttpSettings* UBlackHttpSettings::Get() // { // return GetMutableDefault<UBlackHttpSettings>(); // } UBlackHttpSettings::UBlackHttpSettings() { } 以上是我的代码,我关闭了所有Visual Studio和Unreal Editor实例,删除项目目录下的 Binaries、Intermediate 和 Saved 文件夹,并重新生成项目文件,结果编译后报错,以下是报错信息: 0>------ Building 2 action(s) started ------ 0>[1/2] Link [x64] UnrealEditor-BlackHttpRequest.dll 0> 正在创建库 D:\UE5Projects\ToonEngine\Plugins\BlackHttpRequest\Intermediate\Build\Win64\x64\UnrealEditor\Development\BlackHttpRequest\UnrealEditor-BlackHttpRequest.sup.lib 和对象 D:\UE5Projects\ToonEngine\Plugins\BlackHttpRequest\Intermediate\Build\Win64\x64\UnrealEditor\Development\BlackHttpRequest\UnrealEditor-BlackHttpRequest.sup.exp 0>BlackHttpSettings.cpp.obj: Error : LNK2019: 无法解析外部符号 "__declspec(dllimport) public: __cdecl UDeveloperSettings::UDeveloperSettings(class FObjectInitializer const &)" (__imp_??0UDeveloperSettings@@QEAA@AEBVFObjectInitializer@@@Z)函数 "public: __cdecl UBlackHttpSettings::UBlackHttpSettings(void)" (??0UBlackHttpSettings@@QEAA@XZ) 中引用了该符号 0>BlackHttpSettings.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class TSharedPtr<class SWidget,1> __cdecl UDeveloperSettings::GetCustomSettingsWidget(void)const " (?GetCustomSettingsWidget@UDeveloperSettings@@UEBA?AV?$TSharedPtr@VSWidget@@$00@@XZ) 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class TSharedPtr<class SWidget,1> __cdecl UDeveloperSettings::GetCustomSettingsWidget(void)const " (?GetCustomSettingsWidget@UDeveloperSettings@@UEBA?AV?$TSharedPtr@VSWidget@@$00@@XZ) 0>BlackHttpSettings.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FText __cdecl UDeveloperSettings::GetSectionDescription(void)const " (?GetSectionDescription@UDeveloperSettings@@UEBA?AVFText@@XZ) 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FText __cdecl UDeveloperSettings::GetSectionDescription(void)const " (?GetSectionDescription@UDeveloperSettings@@UEBA?AVFText@@XZ) 0>BlackHttpSettings.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FText __cdecl UDeveloperSettings::GetSectionText(void)const " (?GetSectionText@UDeveloperSettings@@UEBA?AVFText@@XZ) 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FText __cdecl UDeveloperSettings::GetSectionText(void)const " (?GetSectionText@UDeveloperSettings@@UEBA?AVFText@@XZ) 0>BlackHttpSettings.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FName __cdecl UDeveloperSettings::GetSectionName(void)const " (?GetSectionName@UDeveloperSettings@@UEBA?AVFName@@XZ) 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FName __cdecl UDeveloperSettings::GetSectionName(void)const " (?GetSectionName@UDeveloperSettings@@UEBA?AVFName@@XZ) 0>BlackHttpSettings.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FName __cdecl UDeveloperSettings::GetCategoryName(void)const " (?GetCategoryName@UDeveloperSettings@@UEBA?AVFName@@XZ) 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FName __cdecl UDeveloperSettings::GetCategoryName(void)const " (?GetCategoryName@UDeveloperSettings@@UEBA?AVFName@@XZ) 0>BlackHttpSettings.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FName __cdecl UDeveloperSettings::GetContainerName(void)const " (?GetContainerName@UDeveloperSettings@@UEBA?AVFName@@XZ) 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual class FName __cdecl UDeveloperSettings::GetContainerName(void)const " (?GetContainerName@UDeveloperSettings@@UEBA?AVFName@@XZ) 0>BlackHttpSettings.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual void __cdecl UDeveloperSettings::PostEditChangeProperty(struct FPropertyChangedEvent &)" (?PostEditChangeProperty@UDeveloperSettings@@UEAAXAEAUFPropertyChangedEvent@@@Z) 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2001: 无法解析外部符号 "public: virtual void __cdecl UDeveloperSettings::PostEditChangeProperty(struct FPropertyChangedEvent &)" (?PostEditChangeProperty@UDeveloperSettings@@UEAAXAEAUFPropertyChangedEvent@@@Z) 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2019: 无法解析外部符号 "__declspec(dllimport) private: static class UClass * __cdecl UDeveloperSettings::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UDeveloperSettings@@CAPEAVUClass@@XZ)函数 "public: static class UClass * __cdecl UDeveloperSettings::StaticClass(void)" (?StaticClass@UDeveloperSettings@@SAPEAVUClass@@XZ) 中引用了该符号 0> 已定义且可能匹配的符号上的提示: 0> "__declspec(dllimport) private: static class UClass * __cdecl UObject::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UObject@@CAPEAVUClass@@XZ) 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2019: 无法解析外部符号 "__declspec(dllimport) public: __cdecl UDeveloperSettings::UDeveloperSettings(class FVTableHelper &)" (__imp_??0UDeveloperSettings@@QEAA@AEAVFVTableHelper@@@Z)函数 "public: __cdecl UBlackHttpSettings::UBlackHttpSettings(class FVTableHelper &)" (??0UBlackHttpSettings@@QEAA@AEAVFVTableHelper@@@Z) 中引用了该符号 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2019: 无法解析外部符号 "__declspec(dllimport) public: virtual __cdecl UDeveloperSettings::~UDeveloperSettings(void)" (__imp_??1UDeveloperSettings@@UEAA@XZ)函数 "public: virtual __cdecl UBlackHttpSettings::~UBlackHttpSettings(void)" (??1UBlackHttpSettings@@UEAA@XZ) 中引用了该符号 0>BlackHttpSettings.gen.cpp.obj: Error : LNK2019: 无法解析外部符号 "__declspec(dllimport) class UClass * __cdecl Z_Construct_UClass_UDeveloperSettings(void)" (__imp_?Z_Construct_UClass_UDeveloperSettings@@YAPEAVUClass@@XZ)函数 "void __cdecl `dynamic initializer for 'public: static class UObject * (__cdecl*const * const Z_Construct_UClass_UBlackHttpSettings_Statics::DependentSingletons)(void)''(void)" (??__E?DependentSingletons@Z_Construct_UClass_UBlackHttpSettings_Statics@@2QBQ6APEAVUObject@@XZB@@YAXXZ) 中引用了该符号 0>UnrealEditor-BlackHttpRequest.dll: Error : LNK1120: 12 个无法解析外部命令 0>Total time in Parallel executor: 0.59 seconds 0>Total execution time: 1.14 seconds
最新发布
09-09
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值