execlp arguments problem..

Hi
i am trying to use the execlp function in C++

i made this little function..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int spawn(char *cwd, char **arg_list)
{
	pid_t child_pid;

	child_pid = fork ();
	if (child_pid != 0)
	{
		return child_pid;
	}
	else {
		chdir(cwd);
		execlp(arg_list[0],arg_list,NULL);
		abort ();
	}
}


when i compile in in C with gcc no problem..
when i compile it as c++ i get this error..

1
2
Test.cpp: In function int spawn(char*, char**):
Test.cpp:25: error: cannot convert char** to const char* for argument 2 to int execlp(const char*, const char*, ...)


Somebody knows how to fix this?? tyvm =)
Are you sure that execlp is what you want?

What line 12 does is to invoke arg_list[0] with one parameter -- the pointer *arg_list.

I think your spawn function wants to take a list of c-string style arguments (in arg_list)
and pass them to the executable.

For that, you actually want either execv or execvp, both of which take a
char* const argv[] as their second parameter.
Yes, choose the correct function.
http://linux.die.net/man/3/execv

Use execv() if you know the full path of the file to execute.
Use execvp() if you want to search for the file to execute the same way as the shell does.

The arguments to your function should be const:
1
2
3
4
int spawn(const char *cwd, const char **arg_list)
{
	...
}


Also, you should be checking against a return value of (-1) in case fork() fails:
http://linux.die.net/man/2/fork

Hope this helps.
Topic archived. No new replies allowed.