用Qt组件编译C++代码
Compiling C++ code with Qt components
我正试图为我一直在编写的一组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 Creater也会生成丢失的符号错误)。
我确信我明显遗漏了一些东西,但我需要尽快继续在项目的非GUI方面工作。
我在64位MacBook Air上运行Mac OS 10.7.5。我从Qt网站上提供的Mac安装程序安装了Qt5.1.0。我的clang++版本是4.2(clang-425.0.28),通过XCode中的命令行工具安装。
提前谢谢。
您似乎正在尝试使用Qt 5进行构建,其中小部件被移到了QtWidgets。
这就是为什么您需要将includes从QtGui更改为QtWidgets。
除此之外,您还需要向Qt添加"小部件"。此外,core和gui是默认存在的,因此您应该在这个特定场景中编写:QT+=小部件。
不确定这是否解决了与架构相关的问题,但这些问题至少是错误的。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值