User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,716 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,501 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 569 | Replies: 6
Reply
Join Date: Jul 2008
Posts: 20
Reputation: Hannahlv is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Newbie Poster

Call external program in C++ (Linux)

  #1  
Jul 21st, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Posts: 20
Reputation: Hannahlv is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Newbie Poster

Re: Call external program in C++ (Linux)

  #2  
Jul 21st, 2008
Ah, I forgot to write this ( will be clearer). The external program is also in C++ (.cpp). I want to call it from a parent C++ file.
Reply With Quote  
Join Date: Jun 2008
Posts: 28
Reputation: AceofSpades19 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
AceofSpades19's Avatar
AceofSpades19 AceofSpades19 is offline Offline
Light Poster

Re: Call external program in C++ (Linux)

  #3  
Jul 21st, 2008
The most basic way is to use the system function, eg. system("program");. The other way is to use the fork() system call. eg.
pid_t pid;
pid = fork();
if(fork == 0){
    execvp("programname");
}
else {
cout << "fork failed";
}
I suggest you read
man system
man fork
man exec
those will help you alot
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 187
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Call external program in C++ (Linux)

  #4  
Jul 21st, 2008
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.
Reply With Quote  
Join Date: Jul 2008
Posts: 20
Reputation: Hannahlv is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Newbie Poster

Re: Call external program in C++ (Linux)

  #5  
Jul 21st, 2008
Ok. Thank for your link. But I got error while compiling the example program :

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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 187
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Call external program in C++ (Linux)

  #6  
Jul 21st, 2008
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:
g++ -Wall -o extProg extProg.cpp execprocess.cpp

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 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 21st, 2008 at 11:51 pm.
Reply With Quote  
Join Date: Jul 2008
Posts: 20
Reputation: Hannahlv is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Newbie Poster

Re: Call external program in C++ (Linux)

  #7  
Jul 22nd, 2008
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 :
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?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:04 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC