为什么我的Qt编译脚本不能在macOS上运行?

时间:2020-02-06 03:11:28

标签: c++ bash macos qt clang

几天前我才刚刚开始学习Qt。 这是我的世界:

#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不会显示任何错误。

那是为什么?为了使脚本正常工作,该怎么办?

1 个答案:

答案 0 :(得分:0)

很明显,这里存在链接问题。

随机猜测:在外壳中,将QT += core gui widgets附加到*.pro(用于链接我猜测的库)后,在其后的 qmake之后。因此,可能不正确吗?您能否将qmake $filename.pro放在echo ... > $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