静态库缺少`__imp_`符号

问题描述 投票:0回答:1

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

我在静态链接库到我的程序时遇到了一些麻烦。我使用MinGW工具链和Code :: Blocks作为IDE / Build System从Linux到Windows进行交叉编译。我自己也使用与程序相同的工具链编译了库。

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

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

The problem

我将在下面给出所有细节,但实质上问题是链接器抱怨缺少_imp__some_symbol符号。这些符号存在于静态库中,但不是_imp__some_symbol,而是_some_symbol。我通过grepping nm libSomeLibrary.a的输出验证了这一点

Details

库本身是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();
}

我的完整代码:: 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)。

现在用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

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

造成这种情况的原因是什么,以及解决这个问题的方法是什么?

Toolchain

我正在使用MinGW工具链和Code :: Blocks作为IDE / Build系统(所有安装自Fedora官方回购)的Fedora 28系统进行编译。如果相关,这里是这些工具的一些版本信息。

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

c++ windows mingw codeblocks static-linking
1个回答
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

它在构建库时或者在项目中包含头部时,在__declspec(dllexport/dllimport)前面加上所有API函数的前缀(仅在Windows上,并且仅在未定义BEARLIBTERMINAL_STATIC_BUILD时)。

直到现在我才查看库代码 - 并且假设在构建库本身时设置此定义已经足够了。

看来我需要在构建我的项目时设置它以防止_declspecs出现。

© www.soinside.com 2019 - 2024. All rights reserved.