Call external program in C++ (Linux)

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 30
Reputation: Hannahlv is an unknown quantity at this point 
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Light Poster

Call external program in C++ (Linux)

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 30
Reputation: Hannahlv is an unknown quantity at this point 
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Light Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Jun 2008
Posts: 61
Reputation: AceofSpades19 is on a distinguished road 
Solved Threads: 10
AceofSpades19's Avatar
AceofSpades19 AceofSpades19 is offline Offline
Junior Poster in Training

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

 
0
  #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.
  1. pid_t pid;
  2. pid = fork();
  3. if(fork == 0){
  4. execvp("programname");
  5. }
  6. else {
  7. cout << "fork failed";
  8. }
I suggest you read
man system
man fork
man exec
those will help you alot
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #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 Quick reply to this message  
Join Date: Jul 2008
Posts: 30
Reputation: Hannahlv is an unknown quantity at this point 
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Light Poster

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

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

  1. In function `main':
  2. 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*)'
  3. extProg.cpp:(.text+0x1f9): undefined reference to `ExecProcess::exitcode() const'
  4. collect2: ld returned 1 exit status
  5.  

I've already put all the files in the same folder. But this error I don't understand.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #6
Jul 22nd, 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 22nd, 2008 at 12:51 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 30
Reputation: Hannahlv is an unknown quantity at this point 
Solved Threads: 0
Hannahlv Hannahlv is offline Offline
Light Poster

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

 
0
  #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 :
  1. Menu:
  2. 1. Song A
  3. 2. Song B
  4. ...
  5. 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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4684 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC