Reputation: 27
I have an application and I want it to somehow open a new command line window for input. Is there a way to do this with C++?
Upvotes: 2
Views: 4738
Reputation: 99
As pointed out in the comment system
is synchronous which will block the main process until the called sub-program finishes. Therefore, you would want to make it asynchronous by putting &
in each shell command (as I edited below), and the main program could proceed concurrently together with your called sub-program.
However, it still depends on your use case anyway.
I'm not sure what is your "application" in this case. However, for applications to interact with each other, usually we would need some kind of APIs (Application Programming Interface), so that what you have on another application (a new terminal as you said) could be properly used in the "main" application.
If your desired result is just to get another terminal opened, we just have to call it. But remember, every Operating System has its own way to open a terminal, so if you like to automate everything, we have to use macros
to check to current OS of the user, here is a suggestion:
#include <cstdlib>
int main()
{
#ifdef _WIN32
std::system("start program.exe &");
// program.exe is app you have built to get input.
#endif
#ifdef _WIN64
std::system("start program.exe &");
#endif
#ifdef __APPLE__
std::system("open program &");
#endif
#ifdef __linux__
std::system("open program &");
#endif
// #ifdef and #endif are just like if - statement.
}
Every compiler has the macros
like _WIN32
-stands for windows 32bit, _WIN64
-windows 64bit, and so on. Its pretty handy to use them.
Hope this helps!
Upvotes: 1
Reputation: 3883
To open a terminal or any other process, you need to call it.
For example, I use ubuntu OS and for open terminal If I say gnome-terminal
, it will open it as I run my program.
This is that code:
#include <cstdlib>
int main()
{
std::system("gnome-terminal");
return 0;
}
For windows, you should call your cmd process. means that instead of gnome-terminal
you need to write cmd
or your process name
Upvotes: 2