如何在Linux上使用带有Mingw-GCC的C使用正则表达式库?

时间:2017-07-21 16:41:58

标签: c linux gcc mingw

我想在我的Linux Arch系统上构建一个Windows应用程序(.exe)。

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include <stdlib.h>
#include <regex.h>

int main()
{
//regcomp()...
//regexec()...
//regfree()...
}

$ i686-w64-mingw32-gcc nc.c -o nc.exe(这不行)

nc.c:7:10: fatal error: regex.h: No such file or directory
 #include <regex.h>
          ^~~~~~~~~
compilation terminated.

$ gcc nc.c -o nc(这是工作)

无法使用.exe构建i686-w64-mingw32-gcc,这显示有关#include <regex.h>的错误。

但可以使用&#39; gcc` for linux进行编译。

如何解决i686-w64-mingw32-gcc的问题?

我也下载mingw-libgnurx-2.5.1-src.tar.gz

$ ./configure --host = x86_64-w64-mingw32

configure: WARNING: If you wanted to set the --build type, don't use --host.
    If a cross compiler is detected then cross compile mode will be used.
checking for x86_64-w64-mingw32-gcc... x86_64-w64-mingw32-gcc
checking for C compiler default output file name... a.exe
checking whether the C compiler works... yes
checking whether we are cross compiling... yes
checking for suffix of executables... .exe
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether x86_64-w64-mingw32-gcc accepts -g... yes
checking for x86_64-w64-mingw32-gcc option to accept ANSI C... none needed
checking whether x86_64-w64-mingw32-gcc accepts the -mthreads option... yes
checking whether x86_64-w64-mingw32-gcc accepts the -mtune=pentium3 option... no
checking for x86_64-w64-mingw32-lib... no
checking for lib... no
configure: creating ./config.status
config.status: creating Makefile

$ make

x86_64-w64-mingw32-gcc -mthreads -g -O2 -I .   -c -o regex.o regex.c
In file included from regex.c:61:0:
regex_internal.h:424:0: warning: "alloca" redefined
 # define alloca(size)   __builtin_alloca (size)

In file included from /usr/x86_64-w64-mingw32/include/stdlib.h:695:0,
                 from regex_internal.h:27,
                 from regex.c:61:
/usr/x86_64-w64-mingw32/include/malloc.h:183:0: note: this is the location of the previous definition
 #define alloca(x) __builtin_alloca((x))

In file included from regex.c:64:0:
regcomp.c: In function ‘parse_dup_op’:
regcomp.c:2513:39: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
     postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
                                       ^
regcomp.c: In function ‘mark_opt_subexp’:
regcomp.c:3725:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
   int idx = (int) (long) extra;
                   ^
x86_64-w64-mingw32-gcc -mthreads -g -O2 -I . -shared -o libgnurx-0.dll -Wl,--enable-auto-image-base -Wl,--out-implib,libgnurx.dll.a regex.o
cp -p libgnurx.dll.a libregex.a

安装PCRE:

$ sudo pacman -S pcre

warning: pcre-8.41-1 is up to date -- reinstalling
resolving dependencies...
looking for conflicting packages...

Packages (1) pcre-8.41-1

Total Installed Size:  3.38 MiB
Net Upgrade Size:      0.00 MiB

:: Proceed with installation? [Y/n] y
(1/1) checking keys in keyring                     [######################] 100%
(1/1) checking package integrity                   [######################] 100%
(1/1) loading package files                        [######################] 100%
(1/1) checking for file conflicts                  [######################] 100%
(1/1) checking available disk space                [######################] 100%
:: Processing package changes...
(1/1) reinstalling pcre                            [######################] 100%
:: Running post-transaction hooks...
(1/1) Arming ConditionNeedsUpdate...

$ sudo pacman -S pcre2

warning: pcre2-10.23-1 is up to date -- reinstalling
resolving dependencies...
looking for conflicting packages...

Packages (1) pcre2-10.23-1

Total Installed Size:  3.22 MiB
Net Upgrade Size:      0.00 MiB

:: Proceed with installation? [Y/n] y
(1/1) checking keys in keyring                     [######################] 100%
(1/1) checking package integrity                   [######################] 100%
(1/1) loading package files                        [######################] 100%
(1/1) checking for file conflicts                  [######################] 100%
(1/1) checking available disk space                [######################] 100%
:: Processing package changes...
(1/1) reinstalling pcre2                           [######################] 100%
:: Running post-transaction hooks...
(1/1) Arming ConditionNeedsUpdate...

1 个答案:

答案 0 :(得分:0)

您可以检查gnulib并查看它是否提供与Windows兼容的<regex.h>PCRE肯定已移植到Windows,它提供了<regex.h> replacement。 PCRE也已经在mingw-w64-pcre2包中用于针对mingw-w64交叉编译的Arch Linux打包(因为您似乎使用的是Arch Linux)。

远离mingw-libgnurx-2.5.1-src.tar.gz。警告显示它与x86-64不兼容,因为64位Windows上的long仅为32位,并且如警告所示,库作者显然没有预料到这一点。