I am looking for a way to execute a Linux executable from a separate Linux Executable that was compiled from C or C++. However, I have looked at numerous Stack Overflow posts which all direct the user asking to use the system() function or a wrapper of the system function and I do not want a program that relies on the shell, because it could easily fall apart if it was transferred to a different operating system with a different shell.
In the post How do I execute an external program within C in Linux with arguments, the second answer states that execve() is a wrapper for the system() function, and this makes me wary of the other functions in the exec() family.
I have also looked at the following articles:
- How do you write a C program to execute another program?
- http://www.cplusplus.com/forum/beginner/168287/
- Run Another Program in Linux from a C++ Program
All help is appreciated!
systemis a wrapper forexecve. The system manaul gives that info: "The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3)". So using theexecfunctions might actually be what you need?systemis really, really dumb. Many consider it to be a security hole because of how stupid it is.execve()is a wrapper for thesystemfunction. It sayssystemusesexec*functions.system()is the portable way to do the job; it is required by the Standard C library so, for most practical purposes, it will be available anywhere C is available. However, there are some security risks associated with it, and it doesn't necessarily handle all the scenarios you want. In that case, you almost invariably end up usingfork()and theexec()family of functions, possibly withpipe()anddup2(), or maybe some more esoteric functions for handling pseudo-terminals (aka pseudo-ttys or ptys).fork()andexec*()functions is theposix_spawn()function and its relatives (findable from pubs.opengroup.org/onlinepubs/9699919799/toc.htm).