I am trying to to run c code in c++ which uses clone inside , I got an error which I couldn’t solve it , anyone used clone in c++ before , and can help .
my code:
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 %dn", variable);
clone(do_something, child_stack,CLONE_VM|CLONE_FILES, NULL);
sleep(1);
printf("The variable is now %dn", variable);
if (read(fd, &tempch, 1) < 1) {
perror("File Read Error");
exit(1);
}
printf("We could read from the filen");
return 0;
}
and i got the errors:
dell@ubuntu:~$ g++ n.cpp -o n n.cpp: In function ‘int main()’: n.cpp:40:62: error: invalid conversion from ‘int ()()’ to ‘int ()(void*)’ [-fpermissive] /usr/include/x86_64-linux-gnu/bits/sched.h:83:12: error: initializing argument 1 of ‘int clone(int ()(void), void*, int, void*, …)’ [-fpermissive] dell@ubuntu:~$
Advertisement
Answer
Compiler is telling you that first argument to clone
is supposed to be int(*)(void*)
(a pointer to function taking one void*
parameter and returning int
) and that you’re trying to pass it int(*)()
(a pointer to function taking no parameters and returning int
).
The former can’t be implicitly converted to the latter, hence the error.
To fix it, you can define do_something
as:
int do_something(void*)
{
// your code
}