•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 20
Reputation:
Rep Power: 1
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
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
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation:
Rep Power: 11
Solved Threads: 187
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: 20
Reputation:
Rep Power: 1
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.
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation:
Rep Power: 11
Solved Threads: 187
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 21st, 2008 at 11:51 pm.
•
•
Join Date: Jul 2008
Posts: 20
Reputation:
Rep Power: 1
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 :
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?
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
apple bbc computer core debian dell desktop development distributions drm energy enterprise fedora games gnome gpl hardware ibm install itunes kde laptop links linux microsoft mobile news novell olpc open open source openoffice operating os pc ps3 red hat robot russia security server software source sun system ubuntu unix vista web windows
- Write Your First Application in Win32 Using Assembly (Assembly)
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- Python and command Prompt (Python)
- Command Line using system() (C++)
- How to use 'top' using Python (Python)
Other Threads in the C++ Forum
- Previous Thread: [Linker error] undefined reference to `PrintDlgA@4'
- Next Thread: [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.



Linear Mode