| | |
Call external program in C++ (Linux)
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 30
Reputation:
Solved Threads: 0
Hi everyone!
I want to call a child program from my parent program. Are there any methods to do this in C++ (platform is Linux)? I searched Google but the material found quite difficult to understand. If have any examples, it'll be better for me (as you know, some people can't understand things without examples
)
Thank you all.
I want to call a child program from my parent program. Are there any methods to do this in C++ (platform is Linux)? I searched Google but the material found quite difficult to understand. If have any examples, it'll be better for me (as you know, some people can't understand things without examples
Thank you all.
The most basic way is to use the system function, eg. system("program");. The other way is to use the fork() system call. eg.
I suggest you read
man system
man fork
man exec
those will help you alot
C++ Syntax (Toggle Plain Text)
pid_t pid; pid = fork(); if(fork == 0){ execvp("programname"); } else { cout << "fork failed"; }
man system
man fork
man exec
those will help you alot
I've answered this question (on another forum ...
) with a little class to do this kind of thing. You can look through it to see how it works (or just use it as-is --whatever you like). It is POSIX compliant (meaning it will work on any POSIX platform) and uses fork() and exec() to do its stuff.
http://www.cplusplus.com/forum/unices/2047/#msg7731
It doesn't matter what language was used to write the program you want to execute.
Now, if you actually want to communicate with the child in some way (like using popen() or some other form of IPC) that will involve more work, but let us know.
Hope this helps.
) with a little class to do this kind of thing. You can look through it to see how it works (or just use it as-is --whatever you like). It is POSIX compliant (meaning it will work on any POSIX platform) and uses fork() and exec() to do its stuff.http://www.cplusplus.com/forum/unices/2047/#msg7731
It doesn't matter what language was used to write the program you want to execute.
Now, if you actually want to communicate with the child in some way (like using popen() or some other form of IPC) that will involve more work, but let us know.
Hope this helps.
•
•
Join Date: Jul 2008
Posts: 30
Reputation:
Solved Threads: 0
Ok. Thank for your link. But I got error while compiling the example program :
I've already put all the files in the same folder. But this error I don't understand.
C++ Syntax (Toggle Plain Text)
In function `main': extProg.cpp:(.text+0x147): undefined reference to `ExecProcess::ExecProcess(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ExecProcess::mode_t, char const*)' extProg.cpp:(.text+0x1f9): undefined reference to `ExecProcess::exitcode() const' collect2: ld returned 1 exit status
I've already put all the files in the same folder. But this error I don't understand.
The linker is complaining that you didn't link in the execprocess.obj object data.
If you are using the GCC from the command-line, make sure to compile as:
If you are using VC++ or C++Turbo or some other IDE, make sure you add execprocess.cpp and execprocess.hpp to the project before compiling.
It looks cryptic, but
The complaints about "undefined reference to X" means that the compiler knows what X is, but it cannot find X. Since X is defined in another cpp file, that file must be compiled and linked to the cpp file containing main().
For example:
Since extProg.o uses stuff in execprocess.o, the linker puts them together so that it will work.
Hope this helps.
If you are using the GCC from the command-line, make sure to compile as:
g++ -Wall -o extProg extProg.cpp execprocess.cppIf you are using VC++ or C++Turbo or some other IDE, make sure you add execprocess.cpp and execprocess.hpp to the project before compiling.
It looks cryptic, but
ld is the name of the linker on *nix systems (and also with the GCC on all supported systems). A non-zero exit status means something went wrong...The complaints about "undefined reference to X" means that the compiler knows what X is, but it cannot find X. Since X is defined in another cpp file, that file must be compiled and linked to the cpp file containing main().
For example:
g++ -Wall -c execprocess.cpp (compile to "execprocess.o")g++ -Wall -c extProg.cpp (compile to "extProg.o")g++ extProg.o execprocess.o (have GCC tell the linker connect the two object files into a single executable file)Since extProg.o uses stuff in execprocess.o, the linker puts them together so that it will work.
Hope this helps.
Last edited by Duoas; Jul 22nd, 2008 at 12:51 am.
•
•
Join Date: Jul 2008
Posts: 30
Reputation:
Solved Threads: 0
Ok, I'll try this. If any problem I'll ask again. Hope you not feel annoyed.
Now I have another part need to solve. Sorry for this, just because my project requires quite a lot of parts.
This part is about network programming, which require a server and client and I must use socket programming (in Linux) to do. Its requirement is :
- The server will pass to client a list of songs, like this :
-Then client choose the file, and return it to server (just return number), after that the song is played.
My question is :
How can I read the user input and send it back to server ? I know that use the recv(), recvfrom() but in this case how I do this?

Now I have another part need to solve. Sorry for this, just because my project requires quite a lot of parts.
This part is about network programming, which require a server and client and I must use socket programming (in Linux) to do. Its requirement is :
- The server will pass to client a list of songs, like this :
C++ Syntax (Toggle Plain Text)
Menu: 1. Song A 2. Song B ... Which file to play?
-Then client choose the file, and return it to server (just return number), after that the song is played.
My question is :
How can I read the user input and send it back to server ? I know that use the recv(), recvfrom() but in this case how I do this?
![]() |
Similar Threads
- Write Your First Application in Win32 Using Assembly (Assembly)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Python and command Prompt (Python)
- Command Line using system() (C++)
- How to use 'top' using Python (Python)
Other Threads in the C++ Forum
Views: 4684 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






