c++ - 使用 Qt 组件编译 C++ 代码

标签 c++ qt qmake qtgui qtwidgets

我正在尝试为我一直在编写的一组 C++ 文件构建一个 GUI,因此我决定尝试使用 Qt。然而,尝试编译我添加了 Qt 组件的文件被证明是困难的。我在调用时使用 clang

clang++ -std=c++0x -stdlib=libc++ -framework QtGui F/Users/[uname]/Qt5.1.0/5.1.0/clang_64/lib -I/Users/[uname]/Qt5.1.0/5.1.0/clang_64/include -L/Users/[uname]/Qt5.1.0/5.1.0/clang_64/lib  -o QT qt.cpp

如果我只包含这个就可以了

#include "QtGui/QGuiApplication"

看来我应该使用,不过,

#include <QtGui/QApplication>

因错误而失败

fatal error: 'QtGui/QApplication' file not found

如果我尝试包含 qpushbutton.h 文件,用

#include "QtWidgets/QPushButton"

我得到一个错误:

clang++ -std=c++0x -stdlib=libc++ -framework QtGui -F/Users/[uname]/Qt5.1.0/5.1.0/clang_64/lib -I/Users/[uname]/Qt5.1.0/5.1.0/clang_64/include -L/Users/[uname]/Qt5.1.0/5.1.0/clang_64/lib -v  -o QT qt.cpp
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
"/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.7.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name qt.cpp -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 136 -v -resource-dir /usr/bin/../lib/clang/4.2 -F/Users/[uname]/Qt5.1.0/5.1.0/clang_64/lib -I /Users/[uname]/Qt5.1.0/5.1.0/clang_64/include -fmodule-cache-path /var/folders/lw/wq_l1f1936q0dmc1b_tkp94m0000gn/T/clang-module-cache -stdlib=libc++ -std=c++0x -fdeprecated-macro -fdebug-compilation-dir "[path]" -ferror-limit 19 -fmessage-length 204 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.7.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/lw/wq_l1f1936q0dmc1b_tkp94m0000gn/T/qt-YxX3PY.o -x c++ qt.cpp
clang -cc1 version 4.2 based upon LLVM 3.2svn default target x86_64-apple-darwin11.4.2
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /Users/[uname]/Qt5.1.0/5.1.0/clang_64/lib (framework directory)
 /Users/[uname]/Qt5.1.0/5.1.0/clang_64/include
 /usr/bin/../lib/c++/v1
 /usr/local/include
 /usr/bin/../lib/clang/4.2/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.7.0 -o QT -lcrt1.10.6.o -L/Users/[uname]/Qt5.1.0/5.1.0/clang_64/lib -framework QtGui /var/folders/lw/wq_l1f1936q0dmc1b_tkp94m0000gn/T/qt-YxX3PY.o -lc++ -lSystem /usr/bin/../lib/clang/4.2/lib/darwin/libclang_rt.osx.a -F/Users/[uname]/Qt5.1.0/5.1.0/clang_64/lib
Undefined symbols for architecture x86_64:
  "QArrayData::deallocate(QArrayData*, unsigned long, unsigned long)", referenced from:
  QTypedArrayData<unsigned short>::deallocate(QArrayData*) in qt-YxX3PY.o
  "QPushButton::QPushButton(QString const&, QWidget*)", referenced from:
  _main in qt-YxX3PY.o
  "QPushButton::~QPushButton()", referenced from:
  _main in qt-YxX3PY.o
  "QString::fromAscii_helper(char const*, int)", referenced from:
  QString::QString(char const*) in qt-YxX3PY.o
  "QWidget::show()", referenced from:
  _main in qt-YxX3PY.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我也一直在尝试 qmake,它由于与上述 x86_64 体系结构相同的缺失符号而失败,qt.pro 文件是

QT += core
QT += gui
SOURCE += qt.cpp

qt.cpp 文件本身只是一个示例:

#include <QtGui/QApplication>
#include <QtGui/QPushButton>

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

    QApplication app(argc, argv);

    QPushButton but ("Button");
    but.show();

    return app.exec();
}

我正在使用的文件需要在 C++11 标准中编译,我真的不想切换 IDE,所以 Qt Creator 不是一个理想的解决方案(Qt Creator 也会生成丢失的符号错误) .

我确定我遗漏了一些明显的东西,但我需要尽快继续处理项目的非 GUI 方面。

我在 MacBook Air 64 位上运行 Mac OS 10.7.5。我从 Qt 站点上提供的 Mac 安装程序安装了 Qt5.1.0。我的 clang++ 版本是 4.2 (clang-425.0.28),通过 XCode 中的命令行工具安装。

提前致谢。

最佳答案

您似乎正在尝试使用 Qt 5 进行构建,其中 Widgets 已移至 QtWidgets。

这就是为什么您需要将包含从 QtGui 更改为 QtWidgets。

最重要的是,您需要向 Qt 添加“小部件”。此外,默认情况下存在 core 和 gui,因此您应该编写:QT += widgets in this particular scenario。

不确定这是否解决了您的架构相关问题,但至少这些事情是错误的。

关于c++ - 使用 Qt 组件编译 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18078477/

相关文章:

c++ - 在 Windows 10 上使用 D3D11 调试层和 VS2013

c++ - 为什么不推荐使用 std::iterator ?

c++ - 确定 QVariant 是 String 还是 Int

c++ - QTextEdit实现一个onclick事件

c++ - g++4.9 错误允许 std::vector<C_type_array>

c++ - Qt 去除边距

visual-studio - 为什么禁用在 Visual Studio 中不起作用的特定警告

qt - 如何为arm交叉编译qt4生成bin/lrelease和lib/libQtDesigner.so

c++ - qmake转cmake链接错误

c++ - 理解错误 "terminate called after throwing an instance of ' std::length_error' what(): basic_string::_S_create Aborted (core dumped)"