Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用外部库函数时,用C编写的简单Hello World不会输出任何内容_C_Windows_Pcre - Fatal编程技术网

使用外部库函数时,用C编写的简单Hello World不会输出任何内容

使用外部库函数时,用C编写的简单Hello World不会输出任何内容,c,windows,pcre,C,Windows,Pcre,实际上,我必须调试一个旧的C程序。我是C编程的新手,在运行这个程序时,我面临着一种奇怪的行为。这个程序使用PCRE,一个移植到Windows的正则表达式库(我从中获得了“开发人员文件”),但是当程序尝试使用这个库中的函数时,甚至没有调用main方法。 我已将其缩小为一个简单的HelloWorld,如下所示: #include <stdio.h> #include <regex.h> int main(int argc, char *argv[]) { print

实际上,我必须调试一个旧的C程序。我是C编程的新手,在运行这个程序时,我面临着一种奇怪的行为。这个程序使用PCRE,一个移植到Windows的正则表达式库(我从中获得了“开发人员文件”),但是当程序尝试使用这个库中的函数时,甚至没有调用main方法。 我已将其缩小为一个简单的HelloWorld,如下所示:

#include <stdio.h>
#include <regex.h>

int main(int argc, char *argv[]) {
    printf("Hello, world!\n");
    regex_t re;
    regcomp(&re,"sam", 0);
    return 0;
}
执行时返回0,即使printf调用是主函数的第一个调用,也不打印任何内容

如果我对regcomp的通话发表意见:

#include <stdio.h>
#include <regex.h>

int main(int argc, char *argv[]) {
    printf("Hello, world!\n");
    regex_t re;
    //regcomp(&re,"sam", 0);
    return 0;
}
运行程序时,返回0并按预期打印:

Hello, world!

有人能帮我了解引擎盖下发生了什么并成功执行此regcomp函数吗?

函数
regcomp()
返回什么错误代码?它看起来像UB。您是否尝试在调试器中单步执行
regcomp
,或者没有库的源文件?您没有使用已编译的正则表达式
regcomp
将正则表达式“编译”为
regex\t
数据结构,可与
regexec
一起使用以匹配字符串当函数regcomp未注释时,不会调用main,因此:不可能进行调试,也不会出现错误。这就是问题所在,你的主要功能正在运行,只是没有输出任何东西。原因可能有很多;很可能是因为对
regcomp
的调用失败。您可以通过在调用
printf
fflush(stdout)之后立即刷新输出缓冲区来证明printf正在执行函数
regcomp()
返回什么错误代码?它看起来像UB。您是否尝试在调试器中单步执行
regcomp
,或者没有库的源文件?您没有使用已编译的正则表达式
regcomp
将正则表达式“编译”为
regex\t
数据结构,可与
regexec
一起使用以匹配字符串当函数regcomp未注释时,不会调用main,因此:不可能进行调试,也不会出现错误。这就是问题所在,你的主要功能正在运行,只是没有输出任何东西。原因可能有很多;很可能是因为对
regcomp
的调用失败。您可以通过在调用
printf
fflush(stdout)之后立即刷新输出缓冲区来证明printf正在执行
19:57:19 **** Rebuild of configuration Debug for project Test ****
Info: Internal Builder is used for build
gcc "-ID:\\Cpp\\pcre\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o Hello.o "..\\Hello.c" 
..\Hello.c: In function 'main':
..\Hello.c:12:10: warning: unused variable 're' [-Wunused-variable]
   12 |  regex_t re;
      |          ^~
gcc "-LD:\\Cpp\\pcre\\lib" -o Test.exe Hello.o -lpcre -lpcreposix 

19:57:21 Build Finished. 0 errors, 1 warnings. (took 2s.400ms)
Hello, world!