[英]Same programm works in C, but not in C++ (uses linux system calls)
I'm supposed to write a (very basic) shell for linux for school.我应该为学校的 linux 编写一个(非常基本的)shell。 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.
目前我只是试图用它来执行一个程序(通过 fork 和 execv),并且只计划稍后添加用户输入。 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.
我开始用 C 编写(我完全不知道/只知道它与 C++ 共享的某些部分)因为幻灯片使用了它,但是我们可以使用 C 或 C++,我计划在我使用 C++ 时尽快学习了 C 如何处理/不处理输入/输出/字符串。 (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.)
(我从教授的幻灯片中提取了部分代码(主要是使用 status、fork、execv 和 wait 的行)并自己添加了部分代码。)
However, currently when using C the programm compiles, runs and seems to shows the expected output (printing out "testdesecho" by calling the echo function).但是,目前在使用 C 时,程序编译、运行并似乎显示了预期的输出(通过调用 echo 函数打印出“testdesecho”)。 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.
但是当将相同的代码复制粘贴到 C++ 并尝试编译它时,我收到了几个错误(不仅仅是警告),因此甚至无法编译代码。
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++.据我了解,这些都与系统调用有关,我不明白为什么需要在 C++ 中声明它们。 but not in C?
但不是在 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.旧版本的 C(大多数编译器在默认情况下仍然支持)具有未声明函数的默认类型的概念。 If a function is used before it is declared, C assumes the type of the function is
int (*)()
, ie a function that takes an unknown number of arguments and returns an int
.如果函数在声明之前被使用,C 假定函数的类型是
int (*)()
,即一个接受未知数量参数并返回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.另一方面,C++ 没有未声明函数的默认类型,因此在未声明的情况下使用函数时,它会立即引发错误。
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>
.对于
fork
和exec
函数,您需要#include <unistd.h>
, wait
您需要#include <sys/types.h>
和#include <sys/wait.h>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.