我正在尝试创建一个使用动态库的程序。我的程序:
#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
但是如何解决这个问题呢?