【问题标题】:How to open any application through c/c++ program with cross platform approach other than qt?如何通过除qt以外的跨平台方法通过c/c++程序打开任何应用程序?
【发布时间】:2020-07-01 08:07:52
【问题描述】:

您好,我想通过终端或 c/c++ 程序打开系统上已安装的任何应用程序。

我通过终端或c/c++说的原因是我们在终端中使用的命令也会在c/c++程序中使用!!。

我提到了这个链接Open Application In C,但它不是跨平台的。我知道 system 命令与 open 一起用于从 mac 启动任何应用程序。例如:- open -a "Google chrome"通过终端,如果需要在 c/c++ 程序中,它将是 system("open -a 'Google chrome'");

但是如何在 linux 中打开任何已安装的应用程序?还是窗户?以及如何使其跨平台 c/c++??我知道基于宏我们可以识别OS并根据OS执行相应的命令。但我想知道在 linux 和 windows 中打开应用程序?

示例代码:

#include<iostream>
#include<cstdlib>

int main(){
    system("open -a 'Google chrome'");
    return 0;
}

此代码适用于 Mac,但根据我在 linux 中发现的研究,它必须是足够的应用程序路径。请问有人能澄清一下吗? 我需要一个跨平台的解决方案!!

【问题讨论】:

  • 你在linux上试过了吗?
  • @RoQuOTriX 是的,我尝试过它不能以这种方式工作!例如在 linux 中,如果我使用 /usr/bin/google-chrome 它可以工作,但不适用于每个应用程序。所以我需要明确的方法将它包含在 c/c++ 程序中。
  • system 是唯一存在的标准解决方案。当然,它的工作原理是高度特定于平台的。
  • @Karthikgr 如果/usr/bin/google-chrome 中没有安装 google-chrome 并且您有其他路径,您会怎么做?你知道PATH 环境变量吗? linfo.org/path_env_var.html
  • @RoQuOTriX 我认为我会使用“哪个 google-chrome”将为您提供路径并且我会使用!!。但我需要的是一般我想从 c/c++ 程序启动任何应用程序。

标签: c++ c linux macos cross-platform


【解决方案1】:

可以使用Qt cross-platform C++ framework,具体启动进程使用QProcess启动程序,或者QDesktopServices

#include <QtWidgets/QApplication>

#include <QProcess>
#include <QDesktopServices>
#include <QUrl>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // You can specify the path in some config file
    const QString pathToApp = "/usr/bin/google-chrome-stable";

    // Open an application by given path
    // No need to add "open" or some other OS-specific prefix
    QProcess::startDetached(pathToApp);

    // OR
    // Open target web address in a default system browser in any OS
    QDesktopServices::openUrl(QUrl("www.google.com"));

    return a.exec();
}

【讨论】:

  • 还有其他解决方案吗?它必须是 c++,可能我可以使用一些库但不能使用 Qt。
【解决方案2】:

我认为这个问题已经存在一年多了,而唯一的答案意味着将几兆字节的数据注入一个原本不需要它的程序中,这太疯狂了。

您可以使用特定于平台的预处理器指令来实现您想要的。

#include <string>

#ifdef _WIN32
static int platform = 1;
#elif _WIN64
static int platform= 1;
#elif __linux__
static int platform = 2;
#elif __APPLE__
static int platform = 3;
#else
static int platform = 0;
#endif


int main(int argc, char *argv[])
{
  std::string str;
  std::string p = "https://crouton.net";
  if(platform) switch(platform)
    {
    case 1:
      str = "explorer";
      break;
    case 2:
      str = "xdg-open";
      break;
    case 3:
      str = "open";
      break;
    default:
      return 1; // Should never happen on the 3 defined platforms
    }
  str.append(" " + p);
  std::system(str.data());
  return 0;
}

请务必记住,此代码不仅可以在 Windows、Mac 和 Linux 上运行。 __APPLE__ 的使用是基于您正在开发的是桌面程序的假设——它可能无法在定义仍然有效的 iOS 上运行。

此示例仅适用于 URL(例如,您不能将 URL 替换为应用程序的名称)。如果您不知道要打开的内容的路径,您可以尝试使用其他特定于平台的命令来获取它,但即使用户安装了它,也不能保证您会找到它。这是一个非常特定于平台的事情,因为 Windows 应用程序通常不使用 PATH,因为预计它们在命令行上被调用的次数要少得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多