静态库缺少“ __imp_”符号

时间:2018-09-06 13:05:07

标签: c++ windows mingw codeblocks static-linking

编辑:找到了一个解决方案,但还不能接受我自己的答案。

将库静态链接到程序时遇到麻烦。我正在使用MinGW工具链和Code :: Blocks作为IDE /构建系统从Linux到Windows进行交叉编译。我还使用与程序相同的工具链自己编译了库。

我对于交叉编译和Windows编译都是全新的。

编辑:我只是在Windows XP的虚拟安装上尝试了相同的过程(使用Code :: Block捆绑的MinGW),并遇到了相同的问题,所以我想这不是因为交叉编译器。

问题

我将在下面提供所有详细信息,但实质上,问题是链接器抱怨缺少_imp__some_symbol符号。这些符号存在于静态库中,但不以_imp__some_symbol的形式出现,而是以_some_symbol的形式出现。我已经通过grep nm libSomeLibrary.a

的输出来验证了这一点

详细信息

该库本身是BearLibTerminal(您可以在https://bitbucket.org/cfyzium/bearlibterminal/downloads/上找到源)。如前所述,我自己使用与主程序相同的工具链从源代码编译了它。

我的主要程序包含的代码是Bear http://foo.wyrd.name/en:bearlibterminal#simple_examplec_c在此处提供的“ Hello world”示例:

#include "BearLibTerminal.h"

int main()
{
    terminal_open();

    // Printing text
    terminal_print(1, 1, "Hello, world!");
    terminal_refresh();

    // Wait until user close the window
    while (terminal_read() != TK_CLOSE);

    terminal_close();
}

我的完整代码::块构建日志可以在https://pste.eu/p/VMJp.html上找到。它包含BearLibTerminal的构建日志及其依赖性以及我的程序。

(我认为)相关的位是这些:

i686-w64-mingw32-g++ -Wall -fexceptions -O2 -DWINVER=0x0400 -D__WIN95__ -D__GNUWIN32__ -DSTRICT -DHAVE_W32API_H -D__WXMSW__ -D__WINDOWS__ -IBearLibTerminal -I/usr/i686-w64-mingw32/sys-root/mingw/include -I/home/laleksic/Desktop/Roguelike -I/home/laleksic/Desktop/Roguelike/ -c /home/laleksic/Desktop/Roguelike/main.cpp -o /home/laleksic/Desktop/Roguelike/.objs/main.o
i686-w64-mingw32-g++ -L/usr/i686-w64-mingw32/sys-root/mingw/lib -o build/Roguelike /home/laleksic/Desktop/Roguelike/.objs/main.o -s -lstdc++ -lgcc -lodbc32 -lwsock32 -lwinspool -lwinmm -lshell32 -lcomctl32 -lctl3d32 -lodbc32 -ladvapi32 -lodbc32 -lwsock32 -lopengl32 -lglu32 -lole32 -loleaut32 -luuid BearLibTerminal/Dependencies/PicoPNG/libPicoPNG.a BearLibTerminal/Dependencies/FreeType/libFreeType.a BearLibTerminal/libBearLibTerminal.a
/home/laleksic/Desktop/Roguelike/.objs/main.o:main.cpp:(.text.startup+0x11): undefined reference to "_imp__terminal_open'
/home/laleksic/Desktop/Roguelike/.objs/main.o:main.cpp:(.text.startup+0x56): undefined reference to "_imp__terminal_print_ext8'
/home/laleksic/Desktop/Roguelike/.objs/main.o:main.cpp:(.text.startup+0x5c): undefined reference to "_imp__terminal_refresh'
/home/laleksic/Desktop/Roguelike/.objs/main.o:main.cpp:(.text.startup+0x62): undefined reference to "_imp__terminal_read'
/home/laleksic/Desktop/Roguelike/.objs/main.o:main.cpp:(.text.startup+0x7b): undefined reference to "_imp__terminal_close'
collect2: error: ld returned 1 exit status

在链接行的最后,您应该看到它是静态链接BearLibTerminal依赖项(libPicoPNG.alibFreeType.a)和BearLibTerminal本身(libBearLibTerminal.a)。

现在用libBearLibTerminal.a检查nm libBearLibTerminal.a | grep -E ' _imp__terminal_(open|print_ext8|refresh|read|close)'符号将得到空输出。

但是nm libBearLibTerminal.a | grep -E 'terminal_(open|print_ext8|refresh|read|close)'给出:

00000000 t __GLOBAL__sub_I_terminal_open
00000690 T _terminal_close
000032b0 T _terminal_open
000015b0 T _terminal_print_ext8
000024c0 T _terminal_read
00003240 T _terminal_read_str16
000031f0 T _terminal_read_str32
000031a0 T _terminal_read_str8
00000cd0 T _terminal_refresh
         U _terminal_close
         U _terminal_open
         U _terminal_print_ext8
         U _terminal_read
         U _terminal_read_str8
         U _terminal_refresh
00000220 T __Z16luaterminal_openP9lua_State
000010f0 T __Z16luaterminal_readP9lua_State
00000250 T __Z17luaterminal_closeP9lua_State
000002b0 T __Z19luaterminal_refreshP9lua_State
00001410 T __Z20luaterminal_read_strP9lua_State

所以,符号在那里,只是前缀不同。

这是什么原因,将如何解决?

工具链

我正在使用MinGW工具链和Code :: Blocks作为IDE /构建系统在Fedora 28系统上进行编译(均从Fedora官方软件仓库安装)。如果相关,这里是这些工具的一些版本信息。

Code::Blocks version info

i686-w64-mingw32-g++ -v的输出:

Using built-in specs.
COLLECT_GCC=i686-w64-mingw32-g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/i686-w64-mingw32/7.3.0/lto-wrapper
Target: i686-w64-mingw32
Configured with: ../configure --prefix=/usr --bindir=/usr/bin --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info --datadir=/usr/share --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --with-gnu-as --with-gnu-ld --verbose --without-newlib --disable-multilib --disable-plugin --with-system-zlib --disable-nls --without-included-gettext --disable-win32-registry --enable-languages=c,c++,objc,obj-c++,fortran --with-bugurl=http://bugzilla.redhat.com/bugzilla --with-cloog --enable-threads=posix --enable-libgomp --target=i686-w64-mingw32 --with-sysroot=/usr/i686-w64-mingw32/sys-root --with-gxx-include-dir=/usr/i686-w64-mingw32/sys-root/mingw/include/c++
Thread model: posix
gcc version 7.3.0 20180125 (Fedora MinGW 7.3.0-1.fc28) (GCC)

编辑:此外,我已经使用http://wiki.codeblocks.org/index.php/Code::Blocks_and_Cross_Compilers

中的稍作修改的指令,设置了Code :: Blocks与交叉编译器一起工作。

1 个答案:

答案 0 :(得分:0)

事实证明,库标头包含一个像这样的块

#if defined(BEARLIBTERMINAL_STATIC_BUILD)
#  define TERMINAL_API
#elif defined(_WIN32)
#  if defined(BEARLIBTERMINAL_BUILDING_LIBRARY)
#    define TERMINAL_API __declspec(dllexport)
#  else
#    define TERMINAL_API __declspec(dllimport)
#  endif
#else
#  if defined(BEARLIBTERMINAL_BUILDING_LIBRARY) && __GNUC__ >= 4
#    define TERMINAL_API __attribute__ ((visibility ("default")))
#  else
#    define TERMINAL_API
#  endif
#endif

在构建库或在项目中包括标头时,所有API函数均以__declspec(dllexport/dllimport)为前缀(仅在Windows上且仅当未定义BEARLIBTERMINAL_STATIC_BUILD时)。

直到现在我都没有看过库代码-只是假设在构建库本身时设置此定义就足够了。

似乎在构建项目时也需要进行设置,以防止_declspec出现。