Windows MinGW动态连杆未定义参考

Windows MinGW动态连杆未定义参考,windows,dll,mingw,Windows,Dll,Mingw,我正在尝试创建一个使用动态库的程序。我的计划: #include "launcher.h" using namespace std; int main(void) { helloWorld(); return 0; } launcher.h代码: #ifndef LAUNCHER_H_ #define LAUNCHER_H_ //Define the private and public preprocessor commands #if defined _WIN32

我正在尝试创建一个使用动态库的程序。我的计划:

#include "launcher.h"

using namespace std;

int main(void)
{
    helloWorld();

    return 0;
}
launcher.h代码:

#ifndef LAUNCHER_H_
#define LAUNCHER_H_

//Define the private and public preprocessor commands
#if defined _WIN32 || defined __CYGWIN__
  #ifdef BUILDING_DLL
    #ifdef __GNUC__
      #define DLL_PUBLIC __attribute__ ((dllexport))
    #else
      #define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
    #endif
  #else
    #ifdef __GNUC__
      #define DLL_PUBLIC __attribute__ ((dllimport))
    #else
      #define DLL_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
    #endif
  #endif
  #define DLL_LOCAL
#else
  #if __GNUC__ >= 4
    #define DLL_PUBLIC __attribute__ ((visibility ("default")))
    #define DLL_LOCAL  __attribute__ ((visibility ("hidden")))
  #else
    #define DLL_PUBLIC
    #define DLL_LOCAL
  #endif
#endif


void DLL_PUBLIC helloWorld(void);

void DLL_LOCAL hiddenHelloWorld(void);

void internalHelloWorld(void);


#endif /* LAUNCHER_H_ */
我使用以下两个命令编译它(通常包装在makefile中):

第一个调用非常有效,但在第二个命令中,此错误正在打印:

Main.o:Main.cpp:(.text+0x3c): undefined reference to `__imp___Z10helloWorldv'
collect2.exe: error: ld returned 1 exit status
但在liblauncher.a文件中,nm可以找到该方法:

d000006.o:
00000000 i .idata$4
00000000 i .idata$5
00000000 i .idata$7
00000000 I _launcher_dll_iname

d000004.o:
00000000 i .idata$2
00000000 i .idata$4
00000000 i .idata$5
00000000 I __head_launcher_dll
         U _launcher_dll_iname

d000005.o:
00000000 i .idata$4
00000000 i .idata$5
00000000 i .idata$6
00000000 i .idata$7
00000000 t .text
00000001 a @feat.00
00000000 T __Z10helloWorldv
         U __head_launcher_dll
00000000 I __imp___Z10helloWorldv

但是如何解决这个问题呢?

试试
g++-o Test.exe Main.o-llauncher
(注意参数的顺序)

d000006.o:
00000000 i .idata$4
00000000 i .idata$5
00000000 i .idata$7
00000000 I _launcher_dll_iname

d000004.o:
00000000 i .idata$2
00000000 i .idata$4
00000000 i .idata$5
00000000 I __head_launcher_dll
         U _launcher_dll_iname

d000005.o:
00000000 i .idata$4
00000000 i .idata$5
00000000 i .idata$6
00000000 i .idata$7
00000000 t .text
00000001 a @feat.00
00000000 T __Z10helloWorldv
         U __head_launcher_dll
00000000 I __imp___Z10helloWorldv