C++ 同样的程序在C语言中工作,但在C++;(使用linux系统调用)

C++ 同样的程序在C语言中工作,但在C++;(使用linux系统调用),c++,c,linux,system-calls,C++,C,Linux,System Calls,我应该为学校的linux编写一个(非常基本的)shell。目前,我只尝试使用它执行一个程序(通过fork和execv),并且只计划以后添加用户输入。我开始用C编写(我根本不知道,只知道它与C++共享的一些部分),因为幻灯片使用它,但是一旦我学会C如何处理/不处理输入/输出/字符串,我们就可以使用C或C++和我计划使用C++。(我从教授的幻灯片中提取了部分代码(主要是使用status、fork、execv和wait的行),并自己添加了部分代码。) 但是,当前使用C时,程序编译、运行并显示预期的输出

我应该为学校的linux编写一个(非常基本的)shell。目前,我只尝试使用它执行一个程序(通过fork和execv),并且只计划以后添加用户输入。我开始用C编写(我根本不知道,只知道它与C++共享的一些部分),因为幻灯片使用它,但是一旦我学会C如何处理/不处理输入/输出/字符串,我们就可以使用C或C++和我计划使用C++。(我从教授的幻灯片中提取了部分代码(主要是使用status、fork、execv和wait的行),并自己添加了部分代码。)
但是,当前使用C时,程序编译、运行并显示预期的输出(通过调用echo函数打印“testdesecho”)。但是当复制粘贴到C++的相同代码并试图编译它时,我会得到几个错误(不仅仅是警告),甚至不能编译代码。 这是密码

//#include <iostream>

#include <stdio.h>
#include <stdlib.h>

#include <stdio.h>  
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
//#include <unistd.h> 
/*
readparsefunc() {

}
*/


int main() {
    int childPid;

    int status;
    char befehlstr[20] = "/bin/echo";                                                   //location              
    char* parameterstr[61] = { "echo", "testdesecho" };                                 //argv


    childPid = fork();

    //befehlstr = "";                                       
    //parameterstr = " ";
    //cout << parameterstr[1];
    printf("%d", childPid);     printf("<- childPid(test) \n");
    printf(befehlstr);          printf("<- befehlstr(test) \n");
    printf(*parameterstr);      printf("<- parameterstr(test) \n");

    if (childPid == -1) { printf("Konnte keinen neuen Prozess erstellen"); }            
    else if (childPid == 0) {                           
        printf("child process \n");

        int testintueberlagerung = 0;
        testintueberlagerung = execv(befehlstr, parameterstr);
        if (testintueberlagerung != 0) {
            printf("%d" "<- testueberlagerungswert \n", testintueberlagerung);
        }
        //execv(befehlstr, parameterstr);                   

        exit(0);                                        
    }
    else if (childPid > 0) {
        printf("parent process \n");
        wait(&status);                                  
        printf("parent process beendet sich nun auch \n");
    }

    /*
    while (1 != 0) {

    }
    */





    return 0;
}
据我所知,这些都属于系统调用,我不明白为什么它们需要在C++中声明。但不是用C? (如果必须申报,如何申报?)

感谢您的帮助

旧版本的C(大多数编译器在默认情况下仍然支持)具有未声明函数的默认类型的概念。如果在声明函数之前使用了该函数,则C假定该函数的类型为
int(*)(
),即接受未知数量的参数并返回
int
。所讨论的函数的实际类型或多或少与该定义兼容,因此它似乎是可行的

另一方面,C++没有未声明函数的默认类型,因此在未声明函数的情况下使用函数时,它会立即抛出错误


对于
fork
exec
函数,您需要
#include
,对于
wait
函数,您需要
#include
#include

这样您就可以尝试用
C++
编译器来编译
C
代码了吗?这些是不同的语言。您是否在Linux手册页中包含了必要的标题(
unistd.h
sys/types.h
)?旧的C规范允许隐式函数声明;新的标准,和C++,不。你为什么评论这个<代码> //y包含< /代码>?我的代码的视图被滚动下来,我没有看到包含-但是,当调拨位被问到,为什么它被注释掉?你需要它。注意:C现在不是C++的子集。可以编写C和C++编译器所接受的代码,但要做不同的事情。你需要选择一种语言——它们是不能互换的。它已经没有了。@AnttiHaapala取决于编译C语言OP的版本。我们不知道。非常感谢您的快速和有益的回复/解释。作品now@Mgetz他们可以支持。允许编译器成功编译无效程序。(前提是它发布所需的诊断)至于声称“版本很重要”,这就像告诉你可以写“þe olde pizza palor”,这是正确的英语。只有版本才重要。
testmitcanders.cpp:27:13: error: ‘fork’ was not declared in this scope
  childPid = fork();
             ^~~~
testmitcanders.cpp:41:26: error: ‘execv’ was not declared in this scope
   testintueberlagerung = execv(befehlstr, parameterstr);
                          ^~~~~
testmitcanders.cpp:51:3: error: ‘wait’ was not declared in this scope
   wait(&status);        
   ^~~~
testmitcanders.cpp:51:3: note: suggested alternative: ‘main’
   wait(&status);        
   ^~~~
   main