Linking error LNK2019 in MSVC, unresolved symbols with __imp__ prefix, but should be from static lib

前端 未结 4 1469
南笙
南笙 2020-12-01 11:53

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, add

4条回答
  •  遥遥无期
    2020-12-01 12:34

    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 line, or as a /D option. Since you're using CMake, you'll probably do this through add_definitions in CMakeLists.txt.

提交回复
热议问题