为什么我的任何 pthread 函数都无法被 g++ 或 clang 识别?
Why aren't any of my pthread functions recognized by g++ or clang?
我试过编译这个程序:
-
g++ test.cpp
-
g++ -pthread test.cpp
-
g++ -lpthread test.cpp
-
clang -pthread -c test.cpp
.
它们都告诉我函数wait()
, fork()
, exit()
没有定义。到底发生了什么事?
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <string.h>
void parse(char *line, char **argv)
{
while (*line != ' ') // If not the end of line...
{
while (*line == ' ' || *line == 't' || *line == 'n')
{
// Replace white spaces with 0.
*line++ = ' ';
}
// Save the argument position.
*argv++ = line;
while ((*line != ' ') && (*line != ' ') &&
(*line != 't') && (*line != 'n'))
{
// Skip the argument until...
line++;
}
}
// Mark the end of argument list.
*argv = ' ';
}
void execute(char **argv)
{
pid_t pid;
int status;
// Fork a child process.
if ((pid = fork()) < 0)
{
printf("*** ERROR: forking child process failedn");
exit(1);
}
else if (pid == 0) // For the child process:
{
// Execute the command.
if (execvp(*argv, argv) < 0)
{
printf("*** ERROR: exec failedn");
exit(1);
}
}
else // For the parent:
{
// Wait for completion.
while (wait(&status) != pid);
}
}
int main()
{
char line[1024]; /* the input line */
char *argv[64]; /* the command line argument */
// Repeat until done...
while (1)
{
printf("Shell -> "); /* display a prompt */
gets(line); /* read in the command line */
printf("n");
parse(line, argv); /* parse the line */
// Is it an "exit"?
if (strcmp(argv[0], "exit") == 0)
exit(0); /* exit if it is */
execute(argv); /* otherwise, execute the command */
}
}
下面是一些错误:
g++ -pthread test.cpp
test.cpp:16:11: warning: expression which evaluates to zero treated as a null pointer constant of type 'char *' [-Wnon-literal-null-conversion]
*argv = ' '; /* mark the end of argument list */
^~~~
test.cpp:24:14: error: use of undeclared identifier 'fork'
if ((pid = fork()) < 0) { /* fork a child process */
^
test.cpp:26:5: error: use of undeclared identifier 'exit'
exit(1);
^
test.cpp:29:9: error: use of undeclared identifier 'execvp'
if (execvp(*argv, argv) < 0) { /* execute the command */
^
test.cpp:31:7: error: use of undeclared identifier 'exit'
exit(1);
^
test.cpp:35:12: error: use of undeclared identifier 'wait'
while (wait(&status) != pid) /* wait for completion */
^
test.cpp:51:7: error: use of undeclared identifier 'exit'
exit(0); /* exit if it is */
^
1 warning and 6 errors generated.
Tylers-MacBook-Pro:BlastUniversal tylerpfaff$ g++ -lpthread test.cpp
test.cpp:16:11: warning: expression which evaluates to zero treated as a null pointer constant of type 'char *' [-Wnon-literal-null-conversion]
*argv = ' '; /* mark the end of argument list */
^~~~
test.cpp:24:14: error: use of undeclared identifier 'fork'
if ((pid = fork()) < 0) { /* fork a child process */
^
test.cpp:26:5: error: use of undeclared identifier 'exit'
exit(1);
^
test.cpp:29:9: error: use of undeclared identifier 'execvp'
if (execvp(*argv, argv) < 0) { /* execute the command */
^
test.cpp:31:7: error: use of undeclared identifier 'exit'
exit(1);
^
test.cpp:35:12: error: use of undeclared identifier 'wait'
while (wait(&status) != pid) /* wait for completion */
^
test.cpp:51:7: error: use of undeclared identifier 'exit'
exit(0); /* exit if it is */
^
1 warning and 6 errors generated.
Tylers-MacBook-Pro:BlastUniversal tylerpfaff$ clang -pthread -c test.cpp
test.cpp:16:11: warning: expression which evaluates to zero treated as a null pointer constant of type 'char *' [-Wnon-literal-null-conversion]
*argv = ' '; /* mark the end of argument list */
^~~~
test.cpp:24:14: error: use of undeclared identifier 'fork'
if ((pid = fork()) < 0) { /* fork a child process */
^
test.cpp:26:5: error: use of undeclared identifier 'exit'
exit(1);
^
test.cpp:29:9: error: use of undeclared identifier 'execvp'
if (execvp(*argv, argv) < 0) { /* execute the command */
^
test.cpp:31:7: error: use of undeclared identifier 'exit'
exit(1);
^
test.cpp:35:12: error: use of undeclared identifier 'wait'
while (wait(&status) != pid) /* wait for completion */
^
test.cpp:51:7: error: use of undeclared identifier 'exit'
exit(0); /* exit if it is */
这些函数都不是pthread函数,尽管pthreads确实定义了其中一个pthread_exit的pthread特定变体。wait、fork和exit函数是unistd库的一部分。
(编辑:顺便说一下,这些调用都与线程无关,而是冒险进入进程间通信(ipc)领域。)
相关文章:
- 奇怪的结构&GCC&clang(void*返回类型)
- 数据成员SFINAE的C++17测试:gcc vs clang
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- Clang bug?使用指针作为模板参数
- clang整洁10忽略了我的NOLINT命令
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在clang++预处理器中确定gcc工具链版本
- 为什么 Clang 不允许"and"作为函数名称?
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- clang格式:宏的缩进
- CLANG 编译器 说:变量"PTR"可能未初始化
- clang格式:禁用排序包含
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 循环展开 - G++ 与 Clang++
- 如何使用Clang/GCC在Mac上为C/C++设置VSCode
- 一位朋友将模板函数缩写为clang和gcc
- 类模板参数推导-clang和gcc不同