【问题标题】:Why can't my Qt compile script work on macOS?为什么我的 Qt 编译脚本不能在 macOS 上运行?
【发布时间】:2020-05-22 00:56:20
【问题描述】:

我几天前才开始学习 Qt。 这是我的Hello World:

#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QLabel label( QLabel::tr("Hello Qt!") );
    label.show();

    return a.exec();
}

为了在没有 Qt Creator 的情况下编译它,我编写了一个简短的编译脚本:

#!/bin/bash
filename="$(pwd | awk -F "/" '    {print $NF}')" # get name of current directory
qmake -project
qmake $filename.pro
echo 'QT += core gui widgets' >> $filename.pro
make
open $filename.app

但脚本不起作用:

/Library/Developer/CommandLineTools/usr/bin/clang++ -stdlib=libc++ -headerpad_max_install_names  -arch x86_64 -Wl,-syslibroot,/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -mmacosx-version-min=10.12 -Wl,-rpath,@executable_path/../Frameworks -o qttest.app/Contents/MacOS/qttest helloqt.o   -F/usr/local/Cellar/qt/5.13.2/lib -framework QtGui -framework QtCore -framework DiskArbitration -framework IOKit -framework OpenGL -framework AGL
Undefined symbols for architecture x86_64:
  "QApplication::exec()", referenced from:
      _main in helloqt.o
  "QApplication::QApplication(int&, char**, int)", referenced from:
      _main in helloqt.o
  "QApplication::~QApplication()", referenced from:
      _main in helloqt.o
  "QLabel::staticMetaObject", referenced from:
      _main in helloqt.o
  "QLabel::QLabel(QString const&, QWidget*, QFlags<Qt::WindowType>)", referenced from:
      _main in helloqt.o
  "QLabel::~QLabel()", referenced from:
      _main in helloqt.o
  "QWidget::show()", referenced from:
      _main in helloqt.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [qttest.app/Contents/MacOS/qttest] Error 1
The application cannot be opened because its executable is missing.

但是,如果我单独运行命令,它可以工作,make 不会显示任何错误。

这是为什么呢?我应该怎么做才能使脚本正常工作?

【问题讨论】:

  • 它在 linux 上运行良好。
  • idk 为什么 SCRIPT 不能在 mac 上应用......如果我按命令运行脚本命令,它也可以正常工作......

标签: c++ bash macos qt clang


【解决方案1】:

很明显这里有链接问题。

随机猜测:在你的shell中,你将QT += core gui widgets附加到*.pro(我猜是用来链接lib)之后qmake它。所以可能是不正确的?你能试着把qmake $filename.pro放在echo ... &gt; $filename.pro之后吗?

你壳:

#!/bin/bash
filename="$(pwd | awk -F "/" '    {print $NF}')" # get name of current directory
qmake -project
qmake $filename.pro # anchor A
echo 'QT += core gui widgets' >> $filename.pro # anchor B
# try exchange A and B?
make
open $filename.app

【讨论】:

  • 谢谢,现在可以了。一个问题:当我将命令行直接逐行粘贴到shell中时,为什么原始脚本可以工作?
  • @Andy Chen idk,但我认为可能有两个原因。 1、你的终端shell是bin/bash吗? 2. MacOS上的Shell脚本非阻塞地执行一些命令。
猜你喜欢
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多