das_j
das_j

Reputation: 4794

MinGW dynamic linkage undefined reference

I'm trying to create a program that uses a dynamic library. My for the program:

#include "launcher.h"

using namespace std;

int main(void)
{
    helloWorld();

    return 0;
}

The launcher.h code:

#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_ */

I compile it with these two commands (normally wrapped in makefile):

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

The first call works great but in the second command, this error is beeing printed:

Main.o:Main.cpp:(.text+0x3c): undefined reference to `__imp___Z10helloWorldv'
collect2.exe: error: ld returned 1 exit status

But in the liblauncher.a file, the method is found by 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

But how to solve this problem?

Upvotes: 0

Views: 323

Answers (1)

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

Try g++ -o Test.exe Main.o -llauncher (note order of arguments).

Upvotes: 2

Related Questions