gpt4 book ai didi

c - 如何在 Linux 上使用 C 和 Mingw-GCC 使用正则表达式库?

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:59 24 4
gpt4 key购买 nike

我想在我的 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(这是工作)

无法构建.exei686-w64-mingw32-gcc , 这显示关于 #include <regex.h> 的错误.

但可以使用适用于 linux 的“gcc”进行编译。


如何解决 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

$制作

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...

最佳答案

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

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

关于c - 如何在 Linux 上使用 C 和 Mingw-GCC 使用正则表达式库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45242741/

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