MSVC 中的链接错误 LNK2019,带有 __imp__ 前缀的未解析符号,但应该来自静态库 [英] Linking error LNK2019 in MSVC, unresolved symbols with __imp__ prefix, but should be from static lib

查看:11
本文介绍了MSVC 中的链接错误 LNK2019,带有 __imp__ 前缀的未解析符号,但应该来自静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我为 g++ 编写的项目,我在 MSVC 中遇到了链接问题.问题来了:

I'm running into linking problems in MSVC for a project that I wrote for g++. Here's the problem:

我将 libssh 作为静态库构建为我的应用程序的一部分,在 cmake 中添加目标

I build libssh as a static library as part of my application, adding the target in cmake with

add_library(ssh_static 静态 $libssh_SRCS)

add_library(ssh_static STATIC $libssh_SRCS)

Libssh 使用 C 语言,所以我在我的 C++ 源代码中使用了 'extern "C" {...}' 来包装包含.然后我使用

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 ...)

target_link_libraries(sshconnectiontest ... ssh_static ...)

这在带有 gcc 的 linux 中一切正常,但现在在 MSVC 中我得到

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]

对于我使用的每个 libssh 函数.

for every libssh function I use.

任何想法出了什么问题?我在某处读到 imp 前缀意味着链接器期望链接 .dll,但情况不应该是这样,因为 ssh_static 在 add_library 调用中被声明为静态库...

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...

推荐答案

从我记忆中的 Windows 时代开始,在 MinGW 构建的 DLL 中,__imp__ 符号前缀用于 Trampoline 函数正确调用 DLL.然后这个符号由一个扩展名为 .dll.a 的小型静态库提供.

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.

当您包含 libssh 标头时,您需要设置一个 #define 以指示您希望静态链接.如果不这样做,标头中的 libssh 函数将被声明为 __declspec(dllimport),因此在链接时将需要 __imp__ 符号.

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.

我查看了 libssh 源代码,发现它位于 libssh.h 的顶部:

I had a look at the libssh source and found this at the top of libssh.h:

#ifdef LIBSSH_STATIC
  #define LIBSSH_API
#else
  #if defined _WIN32 || defined __CYGWIN__
    #ifdef LIBSSH_EXPORTS
      #ifdef __GNUC__
        #define LIBSSH_API __attribute__((dllexport))
      #else
        #define LIBSSH_API __declspec(dllexport)
      #endif
    #else
      #ifdef __GNUC__
        #define LIBSSH_API __attribute__((dllimport))
      #else
        #define LIBSSH_API __declspec(dllimport)
      #endif
    #endif
  #else
    #if __GNUC__ >= 4
      #define LIBSSH_API __attribute__((visibility("default")))
    #else
      #define LIBSSH_API
    #endif
  #endif
#endif

您需要定义 LIBSSH_STATIC,或者通过 #include <libssh.h> 行之前的 #define,或者作为 <代码>/D 选项.由于您使用的是 CMake,因此您可能会通过 CMakeLists.txt 中的 add_definitions 执行此操作.

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 through add_definitions in CMakeLists.txt.

这篇关于MSVC 中的链接错误 LNK2019,带有 __imp__ 前缀的未解析符号,但应该来自静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆