在 Linux 上使用 Qt 为 Windows 创建可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/963611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:36:18  来源:igfitidea点击:

Creating executable for Windows using Qt on Linux

windowslinuxqtexecutable

提问by

Is it possible to create an executable on Linux for both Linux and Windows using the same Qt code with Eclipse? Or is it necessary to install Qt Creator on my Linux machine?

是否可以使用与 Eclipse 相同的 Qt 代码在 Linux 上为 Linux 和 Windows 创建可执行文件?或者是否有必要在我的 Linux 机器上安装 Qt Creator?

回答by bluebrother

If you want to build a windows binary on linux you need to cross-compile. This means you need to have a windows cross-compiler installed plus the libraries you are linking with built with the cross compiler. For a basic Qt program this means you need at least a cross-compiled Qt.

如果要在 linux 上构建 windows 二进制文件,则需要交叉编译。这意味着您需要安装 Windows 交叉编译器以及使用交叉编译器构建的链接库。对于基本的 Qt 程序,这意味着您至少需要一个交叉编译的 Qt。

Cross-compiling has nothing to do with Eclipse or Qt Creator. I don't think both support cross compiling out of the box but I guess you could make them to do so.

交叉编译与 Eclipse 或 Qt Creator 无关。我不认为两者都支持开箱即用的交叉编译,但我想你可以让他们这样做。

回答by Alan Haggai Alavi

Of course, it is possible to install Qt Creator in Linux. The same Qt code can be used to compile in Linux/Win32/Mac. However, you should be using platform specific code only within:

当然,也可以在 Linux 中安装 Qt Creator。相同的 Qt 代码可用于在 Linux/Win32/Mac 中进行编译。但是,您应该只在以下范围内使用特定于平台的代码:

#ifdef Q_OS_WIN32
    qDebug() << "Executable files end in .exe";
#endif

There are other defines for other operating systems. If you do so, you are safe and you can bet it is cross-platform code. :-)

还有其他操作系统的其他定义。如果你这样做,你是安全的,你可以打赌它是跨平台代码。:-)

Please refer http://www.qtsoftware.com/downloadsand download the Qt SDK for Linux/X11. It contains Qt Creator, Assistant, Designer, et cetera.

请参考http://www.qtsoftware.com/downloads并下载适用于 Linux/X11Qt SDK。它包含 Qt Creator、Assistant、Designer 等。

回答by samuil

Some time ago I was trying to do this, and I found resources about cross-compiling here: http://silmor.de/qtstuff.cross.php. Finally I compiled win32 version under Windows, because of lack of time, but it should be possible.

前段时间我试图这样做,我在这里找到了有关交叉编译的资源:http: //silmor.de/qtstuff.cross.php。最后我在windows下编译了win32版本,因为时间不够,不过应该是可以的。

回答by brunoqc

For Eclipse, there's an official plugin.

对于 Eclipse,有一个官方插件

Qt Eclipse Integration for C++

The Eclipse plugin can be used to create programs using any Qt version since 4.1.0.

用于 C++ 的 Qt Eclipse 集成

Eclipse 插件可用于使用自 4.1.0 以来的任何 Qt 版本创建程序。

回答by George Gorelishvili

executable in windows does not work in linux and vice versa. you can do this:

Windows 中的可执行文件在 linux 中不起作用,反之亦然。你可以这样做:

#ifdef Q_WS_X11
QString *OS=new QString("Linux");
std::cout << OS->toStdString() << std::endl;
#endif
#ifdef Q_WS_WIN
QString *OS=new QString("Windows");
std::cout << OS->toStdString() << std::endl;
#endif
#ifdef Q_WS_MACX
QString *OS=new QString("Mac");
std::cout << OS->toStdString() << std::endl;    
#endif