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

user

I'm supposed to write a (very basic) shell for linux for school. Currently i'm only trying to execute a programm with it at all (via fork and execv), and only plan on adding user-input later. I started writing in C (which i do not know at all/only know the some parts that it shares with C++) because the slides were using it, but we can use either C or C++ and i planned on using C++ as soon as i learned how C handles/doesn't handle input/output/strings. (I took parts of the code (mostly lines where status, fork, execv, and wait are used) from the prof's slides and added parts of it myself.)
However, currently when using C the programm compiles, runs and seems to shows the expected output (printing out "testdesecho" by calling the echo function). But when copy-pasting the same code to C++ and trying to compile this, i get several errors (not just warnings) and so can't even compile the code.

This is the code

//#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;
}

And these are the errors:

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

As far as i understand these all pertain to system calls and i do not understand why they would need to be declared in C++. but not in C? (And how to declare them if i have to?)

Thanks for any help

dbush

Older versions of C (that most compilers still support by default) had the concept of a default type for undeclared functions. If a function is used before it is declared, C assumes the type of the function is int (*)(), i.e. a function that takes an unknown number of arguments and returns an int. The actual types of the functions in question are more-or-less compatible with that definition so it seems to work.

C++ on the other hand does not have a default type for undeclared functions, so it throws an error right away when a function is used without being declared.

For the fork and exec functions you need to #include <unistd.h>, and for wait you need to #include <sys/types.h> and #include <sys/wait.h>.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C application that copy/moves a file & uses system calls in Linux

From Dev

Linux System calls in C on OSX

From Java

Linux system calls vs C lib functions

From Dev

How to Mock Linux System Calls in C

From Dev

C system calls fails

From Dev

C system calls open()

From Java

System calls vs C/C++ system calls

From Dev

Storing System Calls in C Programming

From Dev

Compile c++ Programm with CMake that uses Boost/python.hpp

From Dev

C++. Optimize a programm that uses a lot of constant strings

From Linux

C/Linux: Uses of thread synchronization

From Dev

Basics questions regarding File and I/O System Calls in C (on Linux/UNIX)

From Dev

C: Suppress system calls from binary

From Dev

pipe stuck in read (C - system calls)

From Dev

Are ALL system() calls a security risk in c++?

From Dev

Calling C system calls from JNI

From Dev

Autorunning Delphi Programm runs in C:\windows\system32

From Dev

Concurrent system calls in Linux

From Dev

Linux:System calls for Who

From Dev

Multiple async calls in the same statement in C#

From Dev

How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

From Dev

include the amp.h lib in a c++ programm under linux

From Dev

Generic C/C++ function for execution system calls in PowerPC

From Dev

system("pause") for linux in gcc C

From Dev

Merging two text files into new one (back and forth every new line) using C in Linux using system-calls

From Dev

Troubles to compile a C programm

From Dev

Programm crashing at fgets() in c

From Dev

segmentation fault, c programm

From Dev

Linux bare system calls, not glibc

Related Related

HotTag

Archive