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
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.
Comments