【问题标题】:Cross-Compiling with mingw for Win on Linux a pthread在 Linux 上使用 mingw for Win 交叉编译一个 pthread
【发布时间】:2013-09-23 10:17:08
【问题描述】:

我尝试用mingw为Linux上的Win编译一个简单的线程程序。

为此,我加载了source 并将源代码放到我的目录中。 比我写了一个makefile。对于 Linux,它可以完美运行。但是对于 Win,我遇到了很多错误——其中一些我已经无法修复:

$ make
x86_64-w64-mingw32-gcc -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN   -c -o main.o main.c
x86_64-w64-mingw32-gcc -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN   -c -o usleep.o usleep.c
------------------------------------------
clean up
make clean 
make[1]: Betrete Verzeichnis '/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Verlasse Verzeichnis '/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
    make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Betrete Verzeichnis '/mnt/prog/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS="     " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Betrete Verzeichnis '/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3  -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall  pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: Erzeugen von libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Verlasse Verzeichnis '/mnt/prog/pthreads'
make[1]: Verlasse Verzeichnis '/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c  -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN 
------------------------------------------
create object files
usleep.o  main.o
------------------------------------------
linking
x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o  -o possix.exe
./usleep.o:usleep.c:(.text+0x3c): undefined reference to `__imp_select'
./usleep.o:usleep.c:(.text+0x8c): undefined reference to `__imp_select'
/usr/bin/x86_64-w64-mingw32-ld: ./usleep.o: Falsche Verschiebungsadresse 0x0 in Abschnitt `.pdata'
/usr/bin/x86_64-w64-mingw32-ld: final link failed: Ungültiger Vorgang
collect2: ld returned 1 exit status
make: *** [prog] Fehler 1

如何解决链接问题?


到达亚尔莱

我的标题包含(按顺序,我认为它们被称为):

main.c

/*#include <errno.h>    */  
/*#include <fcntl.h>    */  
#include <inttypes.h>
#include "./pthreads/pthread.h"
#include <stdio.h>       
#include <stdlib.h> 
#include <string.h>
/*#include <termios.h>  */
/*#include <unistd.h> */

#include "usleep.h"      /* um usleep nutzen zu können */

usleep.h

...
#include "unistd.h"
#include <inttypes.h>
#include <stdio.h>
...

难道在 windows 目标中包含 unistd.h 是错误的吗?


我另外减少了标题的数量并尽可能更改它们的顺序,但错误仍然存​​在。我不知道它来自哪里......

main.c

#include <inttypes.h>  /* Spezielle int-Datentypen */
#include <stdio.h>

#if defined(FORCEWIN) || defined(_WIN64) || defined(_WIN32)
    #include <time.h>
    #include <winsock2.h>
#elif __unix    /* all unices not caught above */
    #include <sys/time.h>
    #include <sys/socket.h>
#endif

usleep.h


有英文反馈

LC_ALL=C make
------------------------------------------
clean up
make clean 
make[1]: Entering directory `/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
    make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Entering directory `/mnt/prog/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Entering directory `/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3  -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall  pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: creating libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Leaving directory `/mnt/prog/pthreads'
make[1]: Leaving directory `/mnt/prog/pthreads'
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c  -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN 
usleep.o  main.o
------------------------------------------
linking
x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o  -o possix.exe
./usleep.o:usleep.c:(.text+0x3c): undefined reference to `__imp_select'
./usleep.o:usleep.c:(.text+0x8c): undefined reference to `__imp_select'
/usr/bin/x86_64-w64-mingw32-ld: ./usleep.o: bad reloc address 0x0 in section `.pdata'
/usr/bin/x86_64-w64-mingw32-ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
make: *** [prog] Error 1

我已经从 CFLAGS 中删除了 -DPTW32_STATIC_LIB,现在我得到了这个

LC_ALL=C make
------------------------------------------
clean up
make clean 
make[1]: Entering directory `/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
    make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Entering directory `/mnt/prog/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Entering directory `/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3  -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall  pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: creating libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Leaving directory `/mnt/prog/pthreads'
make[1]: Leaving directory `/mnt/prog/pthreads'
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c  -Wall -O3 -FORCEWIN 
usleep.o  main.o
------------------------------------------
linking
x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o  -o possix.exe -v
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/4.6/lto-wrapper
Target: x86_64-w64-mingw32
Configured with: ../../src/configure --build=x86_64-linux-gnu --prefix=/usr --includedir='/usr/include' --mandir='/usr/share/man' --infodir='/usr/share/info' --sysconfdir=/etc --localstatedir=/var --libexecdir='/usr/lib/gcc-mingw-w64' --disable-maintainer-mode --disable-dependency-tracking --prefix=/usr --enable-shared --enable-static --disable-multilib --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --libdir=/usr/lib --enable-libstdcxx-time=yes --with-tune=generic --enable-version-specific-runtime-libs --enable-threads=win32 --enable-fully-dynamic-string --enable-sjlj-exceptions --enable-languages=c,c++,fortran,objc,obj-c++,ada --enable-lto --with-plugin-ld --target=x86_64-w64-mingw32 --with-gxx-include-dir=/usr/include/c++/4.6 --with-as=/usr/bin/x86_64-w64-mingw32-as --with-ld=/usr/bin/x86_64-w64-mingw32-ld
Thread model: win32
gcc version 4.6.3 (GCC) 
COMPILER_PATH=/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/
LIBRARY_PATH=/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/
COLLECT_GCC_OPTIONS='-o' 'possix.exe' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-w64-mingw32/4.6/collect2 -m i386pep -Bdynamic -o possix.exe /usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/crtbegin.o -L/usr/lib/gcc/x86_64-w64-mingw32/4.6 -L/usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib ./main.o ./usleep.o ./pthreads/pthread.o -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt /usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/crtend.o
./main.o:main.c:(.text+0x1c): undefined reference to `__imp_pthread_mutex_lock'
./main.o:main.c:(.text+0x23): undefined reference to `__imp_pthread_mutex_unlock'
./main.o:main.c:(.text+0x6b): undefined reference to `__imp_pthread_exit'
./main.o:main.c:(.text+0x8b): undefined reference to `__imp_pthread_mutex_lock'
./main.o:main.c:(.text+0x9e): undefined reference to `__imp_pthread_mutex_unlock'
./main.o:main.c:(.text+0x19c): undefined reference to `__imp_pthread_exit'
/usr/bin/x86_64-w64-mingw32-ld: ./main.o: bad reloc address 0x0 in section `.pdata'
/usr/bin/x86_64-w64-mingw32-ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
make: *** [prog] Error 1

在我看来,它像是来自 pthread 的错误 confused


参考下面 Gavin Smith 的 cmets,我在 makefile 中做了一些更改:

$ LC_ALL=C make
------------------------------------------
clean up
make clean 
make[1]: Entering directory `/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
    make CROSS=x86_64-w64-mingw32- clean GC-static -v
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-pc-linux-gnu
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c  -Wall -g -O3 -FORCEWIN 
usleep.o  main.o
------------------------------------------
linking
x86_64-w64-mingw32-ld ./main.o ./usleep.o ./pthreads/libpthreadGC2.a  -o possix.exe -v
GNU ld (GNU Binutils) 2.22.90.20120919
./main.o: In function `thr_ausgabe':
/mnt/prog/./main.c:66: undefined reference to `__imp_pthread_mutex_lock'
/mnt/prog/./main.c:66: undefined reference to `__imp_pthread_mutex_unlock'
/mnt/prog/./main.c:72: undefined reference to `printf'
/mnt/prog/./main.c:83: undefined reference to `__imp_pthread_exit'
./main.o: In function `thr_arbeite':
/mnt/prog/./main.c:44: undefined reference to `__imp_pthread_mutex_lock'
/mnt/prog/./main.c:46: undefined reference to `__imp_pthread_mutex_unlock'
/mnt/prog/./main.c:56: undefined reference to `__imp_pthread_exit'
./main.o: In function `main':
/mnt/prog/./main.c:89: undefined reference to `__main'
/mnt/prog/./main.c:100: undefined reference to `__imp_pthread_mutex_init'
/mnt/prog/./main.c:105: undefined reference to `__imp_pthread_create'
/mnt/prog/./main.c:116: undefined reference to `__imp_pthread_join'
/mnt/prog/./main.c:111: undefined reference to `__imp___iob_func'
/mnt/prog/./main.c:111: undefined reference to `fprintf'
/mnt/prog/./main.c:106: undefined reference to `__imp___iob_func'
/mnt/prog/./main.c:106: undefined reference to `fprintf'
./usleep.o: In function `msleep':
/mnt/prog/./usleep.c:16: undefined reference to `__imp_select'
./usleep.o: In function `usleep':
/mnt/prog/./usleep.c:33: undefined reference to `__imp_select'
make: *** [prog] Error 1

现在主要的符号是:

$ x86_64-w64-mingw32-nm main.o
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 N .debug_abbrev
0000000000000000 N .debug_aranges
0000000000000000 N .debug_frame
0000000000000000 N .debug_info
0000000000000000 N .debug_line
0000000000000000 N .debug_loc
0000000000000000 N .debug_ranges
                 U fprintf
                 U __imp___iob_func
                 U __imp_pthread_create
                 U __imp_pthread_exit
                 U __imp_pthread_join
                 U __imp_pthread_mutex_init
                 U __imp_pthread_mutex_lock
                 U __imp_pthread_mutex_unlock
0000000000000000 T main
                 U __main
                 U msleep
0000000000000000 p .pdata
0000000000000000 p .pdata.startup
                 U printf
0000000000000000 r .rdata
0000000000000000 t .text
0000000000000000 t .text.startup
0000000000000080 T thr_arbeite
0000000000000000 T thr_ausgabe
0000000000000000 r .xdata
0000000000000000 r .xdata.startup

我也查看了 lib 的目标文件,发现符号与主文件有点不同:

000000000000a9c0 T pthread_create
000000000000e6a0 T pthread_exit
000000000000b510 T pthread_join
00000000000024b0 T pthread_mutex_init
0000000000006c70 T pthread_mutex_lock
0000000000005800 T pthread_mutex_unlock

从目标文件中剥离或重命名符号是否有用?


实际情况:

终端:

$ LC_ALL=C make
------------------------------------------
clean up
make clean 
make[1]: Entering directory `/mnt/prog/win'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog/win'
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c  -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN 
usleep.o  main.o
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
    make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Entering directory `/mnt/prog/win/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Entering directory `/mnt/prog/win/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3  -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall  pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: creating libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Leaving directory `/mnt/prog/win/pthreads'
make[1]: Leaving directory `/mnt/prog/win/pthreads'
------------------------------------------
linking
x86_64-w64-mingw32-ld ./main.o ./usleep.o ./pthreads/libpthreadGC2.a  -o possix.exe
./main.o:main.c:(.text+0x33): undefined reference to `printf'
./main.o:main.c:(.text.startup+0x6): undefined reference to `__main'
x86_64-w64-mingw32-ld: ./main.o: bad reloc address 0x6 in section `.text.startup'
x86_64-w64-mingw32-ld: final link failed: Invalid operation
make: *** [prog] Error 1

目标文件的符号

$ x86_64-w64-mingw32-nm main.o
0000000000000000 b .bss
0000000000000000 d .data
                 U fprintf
                 U __imp___iob_func
0000000000000000 T main
                 U __main
                 U msleep
0000000000000000 p .pdata
0000000000000000 p .pdata.startup
                 U printf
                 U pthread_create
                 U pthread_exit
                 U pthread_join
                 U pthread_mutex_init
                 U pthread_mutex_lock
                 U pthread_mutex_unlock
0000000000000000 r .rdata
0000000000000000 t .text
0000000000000000 t .text.startup
0000000000000080 T thr_arbeite
0000000000000000 T thr_ausgabe
0000000000000000 r .xdata
0000000000000000 r .xdata.startup

我真的不明白他为什么找不到 printf 和 main....

usleep.o

$ x86_64-w64-mingw32-nm usleep.o
0000000000000000 b .bss
0000000000000000 d .data
                 U __imp_select
0000000000000000 T msleep
0000000000000000 p .pdata
0000000000000000 t .text
0000000000000050 T usleep
0000000000000000 r .xdata

【问题讨论】:

  • 根据 gcc 规则If any [static] library A depends on symbols defined in library B, then library A should appear first in the list supplied to the linker.,请检查您的构建命令中源文件和库的顺序是否有任何不匹配。
  • 要让不懂德语的人能够阅读编译器错误,请运行LC_ALL=C make
  • 谢谢 - 我会添加这个
  • 我编辑了我的评论,并提出了为什么它仍然没有链接的建议。您不应该尝试重命名目标文件中的符号 - 即使它链接它也可能无法正常运行。
  • 您应该考虑使用 winpthreads,因为您使用的是 mingw-w64,恕我直言,这更简单(./configure --prefix=/usr --host=x86_64-w64-mingw32 --disable-sharedmakesudo make install)。不过,它只能通过版本控制获得。如果您需要 DLL,请删除 --disable-shared。我总是尽可能避免构建 DLL,因为 32 位 DLL 文件和 64 位 DLL 文件具有相同的名称。除非你小心,否则你最终会搞砸。更糟糕的是,您链接到的任何导入库都将依赖于未重命名的 DLL,例如您可能会为 32 位和 64 位共存所做的。

标签: c pthreads mingw


【解决方案1】:

错误:

./usleep.o:usleep.c:(.text+0x3c): undefined reference to `__imp_select'

表示符号 __imp_select 未定义。它可能位于您未包含在最终链接步骤中的其他 C 文件中。

【讨论】:

  • 是的,我这样理解,但是grep -e __imp_select * 没有给我任何东西。
  • 在我看来这是正确的方向。但无论如何我都无法修复它。我必须添加的内容是编译器标志,以便他找到库。在上面的链接中,它本来是要包含glibc - 但什么也没发生......
  • __imp_ 前缀是由于符号修饰,因此可能不会出现在源代码中。
【解决方案2】:

看起来你已经创建了一个静态库文件libpthreadGC2.a,但是你的链接命令x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o -o possix.exe -v只链接了三个目标文件,它不在其中。尝试将-lpthreadGC2libpthreadGC2.a 添加到链接命令中。

如果您想检查目标文件是否包含缺失符号(例如 __imp_pthread_mutex_unlock),您可以尝试运行 nm libpthreadGC2.a | grep '__imp_pthread_mutex_unlock' 以查看它是否存在。

我在自述文件中阅读了您链接到的 pthread 源,您必须在构建时定义 PTW32_STATIC_LIB - 您是否有理由删除了 -DPTW32_STATIC_LIB 选项?

【讨论】:

  • 我无法确定目标文件的格式。 nm 总是告诉File format not recognized。顺便一提。可以使用其他选择来制作 pthread-lib。有带有 C 清理代码的 dll带有 C 清理代码的内联 dll带有 C 清理代码的内联静态库。我应该使用另一个吗?
  • 在调试模式下,我看到我所有的 possix-commands 都是未定义的引用。在手册 pf pthread 中说我必须使用 CFLAG -pthread - 但如果我使用它,编译器找不到库:x86_64-w64-mingw32-gcc: error: unrecognized option ‘-pthread’
  • 如果我使用x86_64-w64-mingw32-nm main.o 而不是nm,则可以查看目标文件:) 我会将这些回复添加到问题中...
猜你喜欢
  • 2011-04-18
  • 2015-03-25
  • 2012-07-27
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多