gpt4 book ai didi

c++ - C mxe 链接失败 : ld cannot find symbols

转载 作者:行者123 更新时间:2023-11-28 06:44:11 24 4
gpt4 key购买 nike

尝试交叉编译 TranscriberAG使用 MXE,我遇到了一个我不明白的问题。

我设法在一个小文件上重现了这个问题:

#include <dlfcn.h>

int test() {
dlopen("test", RTLD_LAZY);
}

我编译它:

/path/to/mxe/usr/bin/i686-pc-mingw32.static-g++  -o test_dl.obj -c test_dl.cpp
/path/to/mxe/usr/bin/i686-pc-mingw32.static-ar cr test_dl.a test_dl.obj
/path/to/mxe/usr/bin/i686-pc-mingw32.static-g++ -shared -o test_dl.dll -Wl,--whole-archive test_dl.a -Wl,--no-whole-archive -ldl -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32

(当然,这些命令都是CMake生成的,我不能随便改)。它失败了:

test_dl.a(test_dl.obj):test_dl.cpp:(.text+0x16): undefined reference to `_dlopen(char const*, int)'
collect2: error: ld returned 1 exit status

我已经在 mxe 中安装了 dlfcn。如果我用我的本地 (Debian/Sid) 编译器尝试类似的命令:

g++  -o test_dl.obj -c test_dl.cpp -fPIC
ar cr test_dl.a test_dl.obj
g++ -shared -o test_dl.dll -Wl,--whole-archive test_dl.a -Wl,--no-whole-archive -ldl

效果很好...

这是 objdump 的输出:

$ /home/eroux/softs/mxe/usr/i686-pc-mingw32/bin/objdump -t /home/eroux/softs/mxe/usr/i686-pc-mingw32.static/lib/libdl.a 
In archive /home/eroux/softs/mxe/usr/i686-pc-mingw32.static/lib/libdl.a:
dlfcn.o: file format pe-i386

SYMBOL TABLE:
[ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x00000000 dlfcn.c
File
[ 2](sec 1)(fl 0x00)(ty 20)(scl 3) (nx 1) 0x00000000 _save_err_str
AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0
[ 4](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000040 _error_buffer
[ 5](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 _current_error
[ 6](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000190 _dlopen
[ 7](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00010040 _first_object
[ 8](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000300 _dlclose
[ 9](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x000003a0 _dlsym
[ 10](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000460 _dlerror
[ 11](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .text
AUX scnlen 0x470 nreloc 42 nlnno 0
[ 13](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .data
AUX scnlen 0x0 nreloc 0 nlnno 0
[ 15](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .bss
AUX scnlen 0x1004c nreloc 0 nlnno 0
[ 17](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .rdata
AUX scnlen 0xb nreloc 0 nlnno 0
[ 19](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .rdata$zzz
AUX scnlen 0x11 nreloc 0 nlnno 0
[ 21](sec 7)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .eh_frame
AUX scnlen 0x17c nreloc 5 nlnno 0
[ 23](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _GetLastError@0
[ 24](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _FormatMessageA@28
[ 25](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _SetErrorMode@4
[ 26](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _LoadLibraryExA@12
[ 27](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _malloc
[ 28](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _GetModuleHandleA@4
[ 29](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _sprintf
[ 30](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _FreeLibrary@4
[ 31](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _free
[ 32](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _GetProcAddress@8
[ 33](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _CloseHandle@4

我一定是遗漏了一些明显的东西......它是什么?

编辑:如果我将 -v 添加到第三个 g++ 命令,我得到:

Using built-in specs.
COLLECT_GCC=/home/eroux/softs/mxe/usr/bin/i686-pc-mingw32.static-g++
COLLECT_LTO_WRAPPER=/home/eroux/softs/mxe/usr/libexec/gcc/i686-pc-mingw32.static/4.9.1/lto-wrapper
Target: i686-pc-mingw32.static
Configured with: /home/eroux/softs/mxe/tmp-gcc-i686-pc-mingw32.static/gcc-4.9.1/configure --target=i686-pc-mingw32.static --build=x86_64-unknown-linux-gnu --prefix=/home/eroux/softs/mxe/usr --libdir=/home/eroux/softs/mxe/usr/lib --enable-languages=c,c++,objc,fortran --enable-version-specific-runtime-libs --with-gcc --with-gnu-ld --with-gnu-as --disable-nls --disable-shared --disable-multilib --without-x [...]
Thread model: win32
gcc version 4.9.1 (GCC)
COMPILER_PATH=/home/eroux/softs/mxe/usr/libexec/gcc/i686-pc-mingw32.static/4.9.1/:/home/eroux/softs/mxe/usr/libexec/gcc/i686-pc-mingw32.static/4.9.1/:/home/eroux/softs/mxe/usr/libexec/gcc/i686-pc-mingw32.static/:/home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/4.9.1/:/home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/:/home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/4.9.1/../../../../i686-pc-mingw32.static/bin/
LIBRARY_PATH=/home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/4.9.1/:/home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/4.9.1/../../../../i686-pc-mingw32.static/lib/
COLLECT_GCC_OPTIONS='-v' '-shared' '-o' 'test_dl.dll' '-mtune=generic' '-march=pentiumpro'
/home/eroux/softs/mxe/usr/libexec/gcc/i686-pc-mingw32.static/4.9.1/collect2 -plugin /home/eroux/softs/mxe/usr/libexec/gcc/i686-pc-mingw32.static/4.9.1/liblto_plugin.so -plugin-opt=/home/eroux/softs/mxe/usr/libexec/gcc/i686-pc-mingw32.static/4.9.1/lto-wrapper -plugin-opt=-fresolution=/tmp/cc3LyEtG.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt --shared -Bdynamic -e _DllMainCRTStartup@12 --enable-auto-image-base -o test_dl.dll /home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/4.9.1/../../../../i686-pc-mingw32.static/lib/dllcrt2.o /home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/4.9.1/crtbegin.o -L/home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/4.9.1 -L/home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/4.9.1/../../../../i686-pc-mingw32.static/lib --whole-archive test_dl.a --no-whole-archive -ldl -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -lstdc++ -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt /home/eroux/softs/mxe/usr/lib/gcc/i686-pc-mingw32.static/4.9.1/crtend.o
test_dl.a(test_dl.obj):test_dl.cpp:(.text+0x16): undefined reference to `dlopen(char const*, int)'
collect2: error: ld returned 1 exit status

这似乎表明好的libdl被占用了,对吧?

编辑: 在命令前添加 LD_LIBRARY_PATH='' 不会改变任何东西...

最佳答案

看起来 dlfcn.h header 没有包含在 extern "C" block 中的函数原型(prototype),因此无法使用 C++ 正确编译。

确保您使用的 dlfcn-win32 库是最新的(extern "C" 包装器已添加到 dlfcn.h 一月份)。

关于c++ - C mxe 链接失败 : ld cannot find symbols,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25333527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com