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

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

问题描述

我正在编写一个关于g ++的项目的MSVC链接问题。这里的问题:



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



add_library(ssh_static STATIC $ libssh_SRCS)



Libssh在C中,所以我有'extern'C{...}来源。然后我将ssh_static目标链接到我的可执行文件sshconnectiontest,用

target_link_libraries(sshconnectiontest ... ssh_static ...)



这一切都可以在linux与gcc,但现在在MSVC我得到

 错误LNK2019:unresolved外部符号__imp__对于我使用的每个libssh函数,[filename]中引用的[函数名] 

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

解决方案

从我记得的Windows天,在MinGW构建的DLL中, __ imp __ 符号前缀用于调用DLL适当的trampoline函数。这个符号由一个小的静态库提供,扩展名为 .dll.a



标头,您需要设置 #define ,表示您希望静态链接。如果不这样做,则头文件中的libssh函数将被声明为 __ declspec(dllimport),因此 __ imp __



我看了一下libssh源码,发现这在 libssh.h

  #ifdef LIBSSH_STRATIC 
#define LIBSSH_API
#else
#if定义_WIN32 ||定义__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

您需要在之前通过 #define 定义 LIBSSH_STATIC libssh.h> 行或作为 / D 选项。因为你使用的是CMake,你可以通过 CMakeLists.txt 中的 add_definitions

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

解决方案

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:

#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

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.

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

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