【问题标题】:MinGW dynamic linkage undefined referenceMinGW动态联动未定义参考
【发布时间】:2013-01-13 21:19:24
【问题描述】:

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

#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

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

【问题讨论】:

    标签: windows dll mingw


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 2011-06-16
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多