簡體   English   中英

相同的程序適用於 C,但不適用於 C++(使用 linux 系統調用)

[英]Same programm works in C, but not in C++ (uses linux system calls)

我應該為學校的 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;
}

這些是錯誤:

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

據我了解,這些都與系統調用有關,我不明白為什么需要在 C++ 中聲明它們。 但不是在 C 中? (如果必須的話,如何聲明它們?)

謝謝你的幫助

舊版本的 C(大多數編譯器在默認情況下仍然支持)具有未聲明函數的默認類型的概念。 如果函數在聲明之前被使用,C 假定函數的類型是int (*)() ,即一個接受未知數量參數並返回int的函數。 所討論函數的實際類型或多或少與該定義兼容,因此它似乎可以工作。

另一方面,C++ 沒有未聲明函數的默認類型,因此在未聲明的情況下使用函數時,它會立即引發錯誤。

對於forkexec函數,您需要#include <unistd.h>wait您需要#include <sys/types.h>#include <sys/wait.h>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM