我试图在c ++中运行c代码,里面使用了clone,我得到了一个我无法解决的错误,任何人之前在c ++中使用了clone,并且可以提供帮助.
我的代码:
int variable, fd;
using namespace std ;
int do_something() {
variable = 42;cout << "sana" << endl ;
close(fd);
_exit(0);
}
int main() {
void **child_stack;
char tempch;
variable = 9;
fd = open("test.file", O_RDONLY);
child_stack = (void **) malloc(16384);
printf("The variable was %d\n", variable);
clone(do_something, child_stack,CLONE_VM|CLONE_FILES, NULL);
sleep(1);
printf("The variable is now %d\n", variable);
if (read(fd, &tempch, 1) < 1) {
perror("File Read Error");
exit(1);
}
printf("We could read from the file\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到了错误:
dell @ ubuntu:〜$ g ++ n.cpp -on n.cpp:在函数'int main()'中:n.cpp:40:62:错误:从'int()()'到'int()的无效转换(void*)'[-fpermissive] /usr/include/x86_64-linux-gnu/bits/sched.h:83:12:error:初始化'int clone(int()(void)的参数1 ,void*, int,void*,...)'[-fpermissive] dell @ ubuntu:〜$
编译器告诉你第一个参数clone应该是int(*)(void*)(一个函数接受一个void*参数并返回的指针int),并且你试图传递它int(*)()(一个指向函数的指针不带参数并返回int).
前者不能隐式转换为后者,因此错误.
要修复它,您可以定义do_something为:
int do_something(void*)
{
// your code
}
Run Code Online (Sandbox Code Playgroud)