MSVC 中的链接错误 LNK2019,带有 __imp__ 前缀的未解析符号,但应该来自静态库
我在 MSVC 中为我为 g++ 编写的项目遇到了链接问题。问题是:
在 cmake 中添加目标
我将 libssh 构建为静态库,作为我的应用程序的一部分,使用add_library(ssh_static STATIC $libssh_SRCS)
Libssh 是 C 语言,所以我有 'extern "C" {...}' 包装包含在我的 C++ 源代码中。 将 ssh_static 目标链接到我的可执行文件 sshconnectiontest ,
然后,我使用target_link_libraries(sshconnectiontest ... ssh_static ...)
这一切都在带有 gcc 的 Linux 中正常工作,但现在在 MSVC 中我得到了
error LNK2019: unresolved external symbol __imp__[function names here] referenced in [filename]
我使用的每个 libssh 函数。
有什么想法出了什么问题吗?我在某处读到 imp 前缀意味着链接器期望链接 .dll,但情况不应如此,因为 ssh_static 在 add_library 调用中被声明为静态库...
I'm running into linking problems in MSVC for a project that I wrote for g++. Here's the problem:
I build libssh as a static library as part of my application, adding the target in cmake with
add_library(ssh_static STATIC $libssh_SRCS)
Libssh is in C, so I have 'extern "C" {...}' wrapping the includes in my c++ sources. I then link the ssh_static target to my executable, sshconnectiontest, with
target_link_libraries(sshconnectiontest ... ssh_static ...)
This all works fine in linux with gcc, but now in MSVC I get
error LNK2019: unresolved external symbol __imp__[function names here] referenced in [filename]
for every libssh function I use.
Any ideas whats going wrong? I've read somewhere that the imp prefix means that the linker is expecting to link a .dll, but this should not be the case since ssh_static is declared a static library in the add_library call...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有点晚了,但在将库与 CRT 的静态和动态链接混合时,我遇到了同样的错误
Somewhat late to the party, but I got the same error when mixing libraries with static and dynamic linkage to the CRT
使用 .DEF 文件
如果您选择将 __declspec(dllimport) 与 .DEF 文件一起使用,则应将 .DEF 文件更改为使用 DATA 或 CONSTANT,以减少错误编码导致错误的可能性。问题:
下表显示了原因:
http:// msdn.microsoft.com/en-us/library/aa271769(v=vs.60).aspx
BUT CONSTANT 现已弃用,
我找到了另一种方法,在导出的 .DEF 文件中.lib use :
使用LIB.exe重新生成lib ...
并在dll代码的导入头文件中
Using a .DEF File
If you choose to use __declspec(dllimport) along with a .DEF file, you should change the .DEF file to use DATA or CONSTANT to reduce the likelihood that incorrect coding will cause a problem:
The following table shows why:
http://msdn.microsoft.com/en-us/library/aa271769(v=vs.60).aspx
BUT CONSTANT is now deprecated
i found another way, in the .DEF file of exported .lib use :
and regenerate the lib with LIB.exe
in the import header file of the dll code...
从我对 Windows 时代的记忆来看,在 MinGW 构建的 DLL 中,
__imp__
符号前缀用于调用 DLL 的 Trampoline 函数。然后,该符号由扩展名为.dll.a
的小型静态库提供。当您包含 libssh 标头时,您需要设置
#define
以指示您希望静态链接。如果不这样做,标头中的 libssh 函数将被声明为__declspec(dllimport)
,因此在链接时将出现__imp__
符号。我查看了 libssh 源代码,并在
libssh.h
的顶部发现了这一点:您需要通过
#define
定义LIBSSH_STATIC
在#include
行之前,或作为/D
选项。由于您使用的是 CMake,因此您可能会通过CMakeLists.txt
中的add_definitions
来执行此操作。From what I remember of my Windows days, in MinGW-built DLLs, the
__imp__
symbol prefix is used for the trampoline function that calls into the DLL proper. This symbol is then provided by a small static library with the extension.dll.a
.When you include libssh headers, you need to set a
#define
to indicate that you're expecting to link statically. If you don't, the libssh functions in the header will be declared__declspec(dllimport)
and so the__imp__
symbols will be expected at link time.I had a look at the libssh source and found this at the top of
libssh.h
:You need to define
LIBSSH_STATIC
, either through#define
before the#include <libssh.h>
line, or as a/D
option. Since you're using CMake, you'll probably do this throughadd_definitions
inCMakeLists.txt
.不知道这是否是你的情况,但 imp 前缀可能意味着你正在 Win32 项目中编译 x64 库。
Don't know if it is your case, but the imp prefix may mean that you are compiling a x64 library in a Win32 project.