Hi i want to open any application that has been installed on the system through terminal or c/c++ program.
The reason i said through terminal or c/c++ is the command which we use in terminal will also be used in c/c++ program!!.
I referred this link Open Application In C but its not a cross platform one. I came to know that system command is used with open to launch any application from mac.For Example:- open -a "Google chrome" through terminal and if wanted in c/c++ program it will be system("open -a 'Google chrome'"); .
But how to open any installed application in linux? Or Windows? and how to make it cross platform c/c++?? i know based on macros we can identify OS and execute respective commands based on OS. But i want to know to open application in linux and windows?
Sample code:
#include<iostream>
#include<cstdlib>
int main(){
system("open -a 'Google chrome'");
return 0;
}
This code works in Mac, But based on my research i found in linux it has to be the path of the application that is enough. Please can anyone clarify this? I need a cross platform solution!!
2条答案
按热度按时间hs1ihplo1#
I think it's insane that this question is over a year old and the only answer implies pumping several megabytes into a program that otherwise wouldn't need it.
You can use platform-specific preprocessor directives to achieve what you want.
It is important to keep in mind that this code can be run on more than just Windows, Mac and Linux. The usage of
__APPLE__
is based on the assumption that what you're developing is a desktop program -- it likely wouldn't work on iOS, where the definition is still valid.This example will only work for URLs (e.g. you can't replace the URL with the name of an application). If you don't know the path of what you're trying to open, you can attempt to get it using other platform-specific commands, but it's never guaranteed that you will find it even if the user has it installed. It is a very platform-specific thing because Windows applications often don't utilize the PATH, as they're expected far less to be invoked on the command line.
ha5z0ras2#
可以使用Qt cross-platform C++ framework,具体启动进程可以使用QProcess启动程序,也可以使用QDesktopServices: