C++ 如何摆脱 VC++ 链接器中的 __imp__ 前缀?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5159353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:33:19  来源:igfitidea点击:

How can I get rid of the __imp__ prefix in the linker in VC++?

c++libcurl

提问by BeeBand

I'm using libcurl and am getting the following sort of linker errors in VC++ 10.

我正在使用 libcurl 并且在 VC++ 10 中遇到以下类型的链接器错误。

1>main.obj : error LNK2019: unresolved external symbol __imp__curl_easy_strerror referenced in function "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl curl_httpget(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?curl_httpget@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV12@@Z)

How can I get rid of that impprefix in front of the function name? I am linking to the right lib, right path etc.

我怎样才能去掉函数名前面的那个imp前缀?我正在链接到正确的库、正确的路径等。

回答by Suma

The __imp__prefix appears whenever you are linking to a DLL. It does not appear when linking to statically linked libraries. Most likely the code is generated to be linked against a DLL import lib, but you have linked it with a static lib instead.

__imp__当你链接到一个DLL出现前缀。链接到静态链接库时它不会出现。生成的代码很可能是链接到 DLL 导入库,但您已将其链接到静态库。

The prefix is added when you mark the imported function with __declspec(dllimport)- make sure your imports are not using this when not linking against a DLL.

当您标记导入的函数时添加前缀__declspec(dllimport)- 确保您的导入在不链接 DLL 时不使用它。

回答by Kostya Trushnikov

You have to add CURL_STATICLIBto Preprocessor Definitionsat the properties of your projects in MSVC

您必须在 MSVC 中项目的属性中将CURL_STATICLIB添加到预处理器定义

回答by Alfishe

If using wizard generated projects - check "Runtime settings" value in project properties -> C/C++ -> Code Generationsection.

如果使用向导生成的项目 - 检查部分中的“运行时设置”值project properties -> C/C++ -> Code Generation

By default it usually has "Multithreaded DLL" value. You need Multithreaded /MT and Multithreaded Debug /MTd values.

默认情况下,它通常具有“多线程 DLL”值。您需要多线程/MT 和多线程调试/MTd 值。

回答by Mahmoud Al-Qudsi

You are using a header file that defines the function prototype with the specifier evaluating to __declspec(dllimport)

您正在使用一个定义函数原型的头文件,其中的说明符评估为 __declspec(dllimport)

You need to either redefine the statement that is evaluating to this (set it to nothing), or use a different header file altogether.

您需要重新定义对此进行评估的语句(将其设置为空),或者完全使用不同的头文件。

Typically you'll see code like this:

通常你会看到这样的代码:

#ifdef FOO_EXPORTS
#define DLLSPEC __declspec(dllexport)
#else
#define DLLSPEC __declspec(dllimport)
#endif

...

DLLSPEC bool foo(int bar);

Compiling the project with FOO_EXPORTS defined will use one mode and without it will use the other.

使用定义的 FOO_EXPORTS 编译项目将使用一种模式,没有它将使用另一种模式。

回答by CKE

As Suma mentioned, <your_library>.dllis normally accompanied by <your_library>.lib. Therefore if you get such error messages like

正如 Suma 所提到的,<your_library>.dll通常伴随着<your_library>.lib. 因此,如果您收到此类错误消息,例如

Error LNK2019 unresolved external symbol __imp__<your_symbol> referenced in function ...

Error LNK2019 unresolved external symbol __imp__<your_symbol> referenced in function ...

it is highly recommended to check the Linker settings of your project.

强烈建议检查项目的链接器设置。

In Visual Studio, just go for your project to

在 Visual Studio 中,只需将您的项目

Properties -> Linker -> General

and check under entry Additional Library Directories, if the path to <your_library>.libis defined there. If not, just add it.

并在 entry 下检查Additional Library Directories,是否在<your_library>.lib那里定义了路径。如果没有,只需添加它。

Then check and define on

然后检查并定义

Properties -> Linker -> Input

under entry Additional Dependenciesthe probable missing <your_library>.lib.

在条目Additional Dependencies下可能失踪<your_library>.lib

The error message should disappear now.

错误消息现在应该消失了。

Hope it helps?

希望能帮助到你?