August 19th, 2003, 02:38 AM
System commands in linux
I am coding with C++ in linux and need the program to execute some commands as if it were at a command line. I know that system() works in windows, but when I try it with g++ I get an undeclared identifier error. Any ideas on what command will let me do this? Thanks.
August 19th, 2003, 03:57 AM
The libraries with most variants of Unix, including Linux, support the system() function. The trick is to find which header file it is in, as that is not standardised --- in some cases, not even between versions of operating systems.
man 4 system
(to search for library functions) at the command prompt. Obviously this relies on your system having man pages installed. Otherwise simply grep through the header files in the directory your g++ uses by default (IIRC, /usr/local/include).
One thing to remember about the system function() is that it invokes a command in your default command shell. As the interpretation of strings varies a bit between shells, you need to be careful to find a way to construct your command string so it is not interpreted in different ways by different shells.
August 19th, 2003, 10:35 AM
ah, was just missing an include, didnt realize that some were included by default when i was coding in windows
August 19th, 2003, 05:59 PM
What is the include header file you need to use? Is the system() command the best way to runlinux commands? Do you guys have an example of using the system() command?
August 19th, 2003, 09:12 PM
i included stdlib.h
system("mkdir newdir"); <-- like that
August 19th, 2003, 10:46 PM