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 :icon_cheesygrin: )

Thank you all.

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.

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

I've answered this question (on another forum ... :sweat: ) 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.

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.

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.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.