静态库缺少`__imp_`符号

7

编辑:已找到解决方法,但还无法接受自己的答案。

我在将库静态链接到程序时遇到了一些问题。我正在使用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/找到源代码)。如前所述,我使用与我的主程序相同的工具链从源代码编译了它。

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

#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();
}

完整的Code::Blocks构建日志可以在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)。
现在使用nm libBearLibTerminal.a | grep -E ' _imp__terminal_(open|print_ext8|refresh|read|close)'检查libBearLibTerminal.a符号会得到空输出。
但是,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

所以,这些符号是存在的,只是前缀不同。
这是什么原因,如何解决?
工具链
我在 Fedora 28 系统上使用 MinGW 工具链和 Code::Blocks 作为 IDE/Build 系统进行编译(所有工具都是从 Fedora 官方仓库安装的)。如果相关,请参考以下这些工具的版本信息。 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)

编辑:此外,我已经按照这里稍作修改后的说明设置了Code::Blocks以使用交叉编译器 http://wiki.codeblocks.org/index.php/Code::Blocks_and_Cross_Compilers

1个回答

4
原来库头文件包含了这样一个块:
#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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接