在MSVC中链接错误LNK2019,带有__imp__前缀的未解析符号,但应该来自静态lib

dlo*_*nie 25 cmake dllimport lnk2019 visual-c++ imp

我正在尝试将MSVC中的问题与我为g ++编写的项目联系起来.这是问题所在:

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

add_library(ssh_static STATIC $ libssh_SRCS)

Libssh在C中,所以我有'extern'C"{...}'将包含在我的c ++源代码中.然后我将ssh_static目标链接到我的可执行文件sshconnectiontest

target_link_libraries(sshconnectiontest ... ssh_static ...)

这一切都适用于linux和gcc,但现在在MSVC中我得到了

error LNK2019: unresolved external symbol __imp__[function names here] referenced in [filename]
Run Code Online (Sandbox Code Playgroud)

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

任何想法都出错了?我已经读过某个地方,imp前缀意味着链接器期望链接.dll,但这不应该是这种情况,因为ssh_static在add_library调用中被声明为静态库...

Jac*_*lly 28

根据我记得的Windows日期,在MinGW构建的DLL中,__imp__符号前缀用于调用DLL本身的trampoline函数.然后,此符号由带有扩展名的小型静态库提供.dll.a.

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

我看了一下libssh源代码,发现它位于顶部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
Run Code Online (Sandbox Code Playgroud)

您需要定义LIBSSH_STATIC,无论是通过#define的前#include <libssh.h>行,或作为一个/D选项.由于您使用CMake的,你可能会做到这一点,通过add_definitionsCMakeLists.txt.


小智 13

不知道是不是你的情况,但imp前缀可能意味着你正在Win32项目中编译x64库.

  • 这实际上是我在Windows上的问题.我无意中将64位dll与32位dll连接起来. (6认同)