如何在 mac os x 上使用 g++ 编译一个简单的 Qt 和 c++ 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/441128/
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-08-27 15:22:04  来源:igfitidea点击:

How to compile a simple Qt and c++ application using g++ on mac os x?

c++macosqtmakefileqmake

提问by Sam

I am trying to use Qt for a project in school but am running into problems. I started following the tutorials and I am running into Makefile problems. Most of the tutorials say to run qmake -project, then qmakeand finally make. But when I try this I run into the error make: *** No targets specified and no makefile found. Stop.I dont know much about Makefiles really. Could someone help point me in the right direction?

我正在尝试将 Qt 用于学校的一个项目,但遇到了问题。我开始遵循教程,但遇到了 Makefile 问题。大多数教程都说要运行qmake -project,然后qmake和最后make。但是当我尝试这个时,我遇到了一个错误,make: *** No targets specified and no makefile found. Stop.我真的不太了解 Makefiles。有人可以帮我指出正确的方向吗?

回答by danieldk

qmake on OS X creates Xcode project files. You can create a Makefile with:

OS X 上的 qmake 创建 Xcode 项目文件。您可以使用以下命令创建 Makefile:

qmake -spec macx-g++

If you don't want the Makefile to create an app bundle, you could also remove 'app_bundle' your configuration, for example by adding the following lines to your project file.

如果您不希望 Makefile 创建应用程序包,您还可以删除“app_bundle”您的配置,例如通过将以下行添加到您的项目文件中。

mac {
  CONFIG -= app_bundle
}

回答by Sam

As other posters have pointed out, the default behavior of qmake on the Mac is to create xCode project files. You can type in something special every time you run qmake or add a few lines to each project file. There is a more permanent solution. I run into this every time I install Qt on my Mac. From a command line type in the following:

正如其他海报指出的那样,Mac 上 qmake 的默认行为是创建 xCode 项目文件。你可以在每次运行 qmake 时输入一些特殊的东西,或者在每个项目文件中添加几行。有一个更永久的解决方案。每次我在 Mac 上安装 Qt 时都会遇到这个问题。从命令行输入以下内容:

cd /usr/local/Qt4.5/mkspecs/

sudo rm default

sudo ln -sf macx-g++ default

cd /usr/local/Qt4.5/mkspecs/

sudo rm 默认

须藤 ln -sf macx-g++ 默认

The directory specified in the first online command may need some tweaking. The first "sudo" will require an administrative password.

第一个在线命令中指定的目录可能需要一些调整。第一个“sudo”将需要一个管理密码。

What this does is remove the offending file that specifies that the default -spec switch is mac-xcode or something like that. We then replace it with a file specifying we use the macx-g++ switch by default.

这样做是删除指定默认 -spec 开关是 mac-xcode 或类似的有问题的文件。然后我们用一个文件替换它,指定我们默认使用 macx-g++ 开关。

回答by banbul lary

configure -platform macx-g++ gmake

配置 -platform macx-g++ gmake

that should suffice for all questions

这应该足以解决所有问题

回答by Arcane

It sounds like there might be a problem with your Qt install. Did you build it yourself, or install a build from someone else? Also, do you have an example .pro file that is giving you this trouble?

听起来您的 Qt 安装可能存在问题。您是自己构建的,还是安装了其他人的构建?另外,你有没有给你带来这个麻烦的示例 .pro 文件?

回答by Caleb Huitt - cjhuitt

It's been a little while, but I think your problem is that qmake on Mac OS X creates xcode project files by default, instead of makefiles. That's why no makefile was found when you ran make. Instead, look in the command lines for qmake to specify how to target makefiles. Also, there might be an option you can add to the .pro file to force a makefile output every time.

已经有一段时间了,但我认为您的问题是 Mac OS X 上的 qmake 默认创建 xcode 项目文件,而不是 makefile。这就是为什么当你运行 make 时没有找到 makefile 的原因。相反,查看 qmake 的命令行以指定如何定位 makefile。此外,可能有一个选项可以添加到 .pro 文件以强制每次生成生成文件输出。