MinGW动态链接未定义引用

时间:2013-01-13 21:19:24

标签: 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 || 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中):

g++ -c -std=c++11 -o Main.o Main.cpp
g++ -o Test.exe -llauncher Main.o

第一个调用很有效,但在第二个命令中,打印出错误:

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

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

1 个答案:

答案 0 :(得分:2)

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