C++ 为什么不';t g++;还是叮当声?
我已尝试使用以下工具编译此程序:C++ 为什么不';t g++;还是叮当声?,c++,linux,gcc,clang,C++,Linux,Gcc,Clang,我已尝试使用以下工具编译此程序: g++测试.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
g++测试.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 != '\0') // If not the end of line...
{
while (*line == ' ' || *line == '\t' || *line == '\n')
{
// Replace white spaces with 0.
*line++ = '\0';
}
// Save the argument position.
*argv++ = line;
while ((*line != '\0') && (*line != ' ') &&
(*line != '\t') && (*line != '\n'))
{
// Skip the argument until...
line++;
}
}
// Mark the end of argument list.
*argv = '\0';
}
void execute(char **argv)
{
pid_t pid;
int status;
// Fork a child process.
if ((pid = fork()) < 0)
{
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) // For the child process:
{
// Execute the command.
if (execvp(*argv, argv) < 0)
{
printf("*** ERROR: exec failed\n");
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 */
}
}
#包括
#包括
#包括
#包括
无效解析(字符*行,字符**argv)
{
while(*line!='\0')//如果不是行尾。。。
{
而(*line=''| |*line='\t'| |*line=='\n')
{
//将空白替换为0。
*行+='\0';
}
//保存参数位置。
*argv++=行;
而((*line!='\0')&&(*line!='')&&
(*行!='\t')&&(*行!='\n'))
{
//跳过争论直到。。。
line++;
}
}
//标记参数列表的结尾。
*argv='\0';
}
无效执行(字符**argv)
{
pid_t pid;
智力状态;
//派生一个子进程。
如果((pid=fork())<0)
{
printf(“***错误:分叉子进程失败\n”);
出口(1);
}
else if(pid==0)//对于子进程:
{
//执行命令。
if(execvp(*argv,argv)<0)
{
printf(“***错误:exec失败\n”);
出口(1);
}
}
else//对于父级:
{
//等待完成。
while(等待和状态)!=pid);
}
}
int main()
{
字符行[1024];/*输入行*/
char*argv[64];/*命令行参数*/
//重复,直到完成。。。
而(1)
{
printf(“Shell->”);/*显示提示*/
获取(行);/*在命令行中读取*/
printf(“\n”);
解析(行,argv);/*解析行*/
//这是“出口”吗?
如果(strcmp(argv[0],“退出”)==0)
退出(0);/*如果是,则退出*/
执行(argv);/*否则,执行命令*/
}
}
以下是一些错误:
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 = '\0'; /* 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 = '\0'; /* 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 = '\0'; /* 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 */
g++-pthread test.cpp
test.cpp:16:11:警告:计算结果为零的表达式被视为“char*”类型的null指针常量[-Wnon literal null conversion]
*argv='\0';/*标记参数列表的结尾*/
^~~~
test.cpp:24:14:错误:使用未声明的标识符“fork”
如果((pid=fork())<0){/*fork子进程*/
^
test.cpp:26:5:错误:使用未声明的标识符“exit”
出口(1);
^
test.cpp:29:9:错误:使用未声明的标识符“execvp”
如果(execvp(*argv,argv)<0{/*执行命令*/
^
test.cpp:31:7:错误:使用未声明的标识符“exit”
出口(1);
^
test.cpp:35:12:错误:使用未声明的标识符“wait”
while(等待(&status)!=pid)/*等待完成*/
^
test.cpp:51:7:错误:使用未声明的标识符“exit”
退出(0);/*如果是,则退出*/
^
生成1个警告和6个错误。
Tylers MacBook Pro:BlastUniversal tylerpfaff$g++-lpthread test.cpp
test.cpp:16:11:警告:计算结果为零的表达式被视为“char*”类型的null指针常量[-Wnon literal null conversion]
*argv='\0';/*标记参数列表的结尾*/
^~~~
test.cpp:24:14:错误:使用未声明的标识符“fork”
如果((pid=fork())<0){/*fork子进程*/
^
test.cpp:26:5:错误:使用未声明的标识符“exit”
出口(1);
^
test.cpp:29:9:错误:使用未声明的标识符“execvp”
如果(execvp(*argv,argv)<0{/*执行命令*/
^
test.cpp:31:7:错误:使用未声明的标识符“exit”
出口(1);
^
test.cpp:35:12:错误:使用未声明的标识符“wait”
while(等待(&status)!=pid)/*等待完成*/
^
test.cpp:51:7:错误:使用未声明的标识符“exit”
退出(0);/*如果是,则退出*/
^
生成1个警告和6个错误。
Tylers MacBook Pro:BlastUniversal tylerpfaff$clang-pthread-c test.cpp
test.cpp:16:11:警告:计算结果为零的表达式被视为“char*”类型的null指针常量[-Wnon literal null conversion]
*argv='\0';/*标记参数列表的结尾*/
^~~~
test.cpp:24:14:错误:使用未声明的标识符“fork”
如果((pid=fork())<0){/*fork子进程*/
^
test.cpp:26:5:错误:使用未声明的标识符“exit”
出口(1);
^
test.cpp:29:9:错误:使用未声明的标识符“execvp”
如果(execvp(*argv,argv)<0{/*执行命令*/
^
test.cpp:31:7:错误:使用未声明的标识符“exit”
出口(1);
^
test.cpp:35:12:错误:使用未声明的标识符“wait”
while(等待(&status)!=pid)/*等待完成*/
^
test.cpp:51:7:错误:使用未声明的标识符“exit”
退出(0);/*如果是,则退出*/
这些函数都不是pthread函数,尽管pthread确实定义了其中一个pthread_exit的pthread特定变体。wait、fork和exit函数是unistd库的一部分
(编辑:顺便说一句,这些调用都与线程无关,而是冒险进入进程间通信(ipc)领域。)这些函数都不是pthread函数,尽管pthread确实定义了其中一个pthread_exit的pthread特定变体。wait、fork和exit函数是unistd库的一部分 (编辑:顺便说一句,这些都不是cal