#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0

    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.
  2. #2
  3. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    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.

    Try typing

    man system

    or possibly

    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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0
    ah, was just missing an include, didnt realize that some were included by default when i was coding in windows
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    79
    Rep Power
    12
    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?
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0
    i included stdlib.h

    system("commandtorun");

    system("mkdir newdir"); <-- like that
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    79
    Rep Power
    12
    thanks..

IMN logo majestic logo threadwatch logo seochat tools logo