简体   繁体   English

在Linux上使用MinGW编译适用于Windows的Gnu11

[英]Compiling Gnu11 for Windows with MinGW on Linux

I have a C program; 我有一个C程序; it compiles and links with gcc -std=gnu11 iter.c -o iter because I am using some GNU string.h extensions like strndup , strnlen and strsep . 它编译并链接gcc -std=gnu11 iter.c -o iter因为我正在使用一些GNU string.h扩展,如strndupstrnlenstrsep

I want to compile this program for Windows using the package i686-w64-mingw32-gcc on Ubuntu Linux. 我想在Ubuntu Linux上使用i686-w64-mingw32-gcc软件包为Windows编译这个程序。

$ i686-w64-mingw32-gcc -std=gnu11 iter.c -o iter32.exe 
In file included from iter.c:1:0:
iter.h: In function ‘str_chomp’:
iter.h:166:15: warning: implicit declaration of function ‘strndup’ [-Wimplicit-function-declaration]
   char* new = strndup(str, MAX_STR_LEN);
               ^
iter.h:166:15: warning: incompatible implicit declaration of built-in function ‘strndup’
iter.h: In function ‘str_split’:
iter.h:189:19: warning: incompatible implicit declaration of built-in function ‘strndup’
       *str_copy = strndup(str, MAX_STR_LEN);
                   ^
iter.h:211:26: warning: implicit declaration of function ‘strsep’ [-Wimplicit-function-declaration]
     for (i = 0; (token = strsep(&str_copy, delim_str)) != NULL; i++) {
                          ^
iter.h:211:24: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     for (i = 0; (token = strsep(&str_copy, delim_str)) != NULL; i++) {
                        ^
/tmp/ccPH0nG5.o:iter.c:(.text+0x216): undefined reference to `strndup'
/tmp/ccPH0nG5.o:iter.c:(.text+0x2bf): undefined reference to `strndup'
/tmp/ccPH0nG5.o:iter.c:(.text+0x3a7): undefined reference to `strsep'
collect2: error: ld returned 1 exit status

I have installed all the required MinGW runtime and libraries, but MinGW seems to ignore the -std=gnu11 directive. 我已经安装了所有必需的MinGW运行时和库,但是MinGW似乎忽略了-std=gnu11指令。 It's not ignoring the option entirely, because -std=asdf is "unrecognised", but it refuses to cross-compile GNU C. 它并没有完全忽略该选项,因为-std=asdf是“无法识别的”,但它拒绝交叉编译GNU C.

Is there a way I can cross-compile for Windows a program using GNU extensions? 有没有办法可以使用GNU扩展为Windows编译程序?

As discussed in comments, although MinGW provides many GNU programs, including GCC, it does not provide the GNU C library. 正如评论中所讨论的,尽管MinGW提供了许多GNU程序,包括GCC,但它并没有提供GNU C库。 C programs built by GCC for the MinGW environment rely on Microsoft's C runtime library. GCC为MinGW环境构建的C程序依赖于Microsoft的C运行时库。 They can use GNU language extensions, but not, generally, GNU library extensions that are not also implemented by Microsoft. 他们可以使用GNU 语言扩展,但通常不使用Microsoft也不实现的GNU 扩展。 On the other hand, they can rely on Microsoft library extensions that are not implemented by GNU, though they may need to provide their own headers. 另一方面,他们可以依赖GNU未实现的Microsoft库扩展,尽管他们可能需要提供自己的头文件。

If your program relies on functions that may or may not be available in the target environment (eg Windows / MinGW) then the usual approach is to use conditional compilation directives and maybe macros to substitute alternatives to those functions in builds for targets that do not provide them. 如果你的程序依赖于目标环境中可能有或没有的函数(例如Windows / MinGW),那么通常的方法是使用条件编译指令和宏来代替那些不提供的目标的构建中的函数替代他们。 Depending on the specific function, that may mean you conditionally provide your own implementation. 根据具体功能,这可能意味着您有条件地提供自己的实现。

How you integrate conditional compilation into your build system is a much broader question. 如何将条件编译集成到构建系统中是一个更广泛的问题。 If you are using the Autotools then facilities for this sort of thing are among its core features. 如果您使用的是Autotools,那么这类设备就属于其核心功能。 You can do similar with CMake, and probably with most other popular systems. 您可以使用CMake,也可以与大多数其他流行系统类似。 If you're using a hand-rolled build system then of course you have both the freedom and the obligation to come up with your own approach. 如果您使用的是手工制作的构建系统,那么您当然有自由和义务来提出自己的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM