Eclipse、QT 和“C++ 项目”:可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7775398/
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
Eclipse, QT and "C++ project": is it possible?
提问by MTuner
Need your help:
需要你的帮助:
I want to use Eclipse CDT and QT without creating a "Qt gui project". Is it possible? How to include QT libraries to my C++ project, and how to call qmake/make to compile the program? This Similar questiondidn't help me(
我想在不创建“Qt gui 项目”的情况下使用 Eclipse CDT 和 QT。是否可以?如何在我的 C++ 项目中包含 QT 库,以及如何调用 qmake/make 来编译程序?这个类似的问题对我没有帮助(
I want to use 'C++ project' instead of 'QT Gui project' because there is an issue with external libraries indexing in the QT project (this problem)
我想使用“C++ 项目”而不是“QT Gui 项目”,因为 QT 项目中的外部库索引存在问题(这个问题)
Thank you a lot! Nikolai.
非常感谢!尼古拉。
采纳答案by Luca Carlon
Doing this is quite bothering, I suggest you don't do it. I've tried it only on small projects.
这样做很麻烦,我建议你不要这样做。我只在小项目上尝试过。
As far as I know you'll have to write a correct Makefile yourself (or setup CDT to create it) by including all the include paths you need for Qt headers. Then you'll have to link to all the Qt libraries your project is using.
据我所知,您必须自己编写一个正确的 Makefile(或设置 CDT 来创建它),方法是包含 Qt 头文件所需的所有包含路径。然后您必须链接到您的项目正在使用的所有 Qt 库。
If you make use of the Qt meta-object system you'll have to run the moc before compiling and linking. The moc generates C++ sources that you'll have to link to the other sources. If you're using GNU make, and I guess you are, it seems to be possible to automate the moc writing the correct instructions in the Makefile CDT will create. For detailed information read this: http://doc.qt.io/qt-5/moc.html#writing-make-rules-for-invoking.
如果您使用 Qt 元对象系统,则必须在编译和链接之前运行 moc。moc 生成您必须链接到其他源的 C++ 源。如果您正在使用 GNU make,而且我猜您是,似乎可以自动化 moc,在 Makefile CDT 将创建的 Makefile 中写入正确的指令。有关详细信息,请阅读:http: //doc.qt.io/qt-5/moc.html#writing-make-rules-for-invoking。
By the way, isn't it possible for you to use Qt Creator?
顺便问一下,您不能使用Qt Creator吗?
回答by jwernerny
We've done something similar using Qt with a vendor customized version of Eclipse (Momentics) and CDT. To get it to work, we ended up creating a generic makefile project in Eclipse with our own, hand generated Makefile.
我们使用 Qt 和供应商定制版本的 Eclipse (Momentics) 和 CDT 做了类似的事情。为了让它工作,我们最终在 Eclipse 中使用我们自己的手工生成的 Makefile 创建了一个通用的 makefile 项目。
The hand generated Makefile basically contained enough information to invoke QMake on the appropriate .pro file ("qt.pro") and then invoke the resulting Makefile ("qtmake.mk").
手动生成的 Makefile 基本上包含足够的信息来调用适当的 .pro 文件(“qt.pro”)上的 QMake,然后调用生成的 Makefile(“qtmake.mk”)。
all: qtmake.mk
$(MAKE) -f qtmake.mk
qtmake.mk: qt.pro
qmake -r -o qtmake.mk qt.pro
clean: qtmake.mk
$(MAKE) -f qtmake.mk clean
install: qtmake.mk
$(MAKE) -f qtmake.mk install
回答by J..
This is very easy making use of Netbeans, since qt is integrated in the c++ projects.
使用 Netbeans 非常容易,因为 qt 集成在 c++ 项目中。
But if you use Eclipse, as is my case, you could follow these two steps (for linux users):
但是如果你使用 Eclipse,就像我的情况一样,你可以按照以下两个步骤(对于 linux 用户):
- Include the directories with the Qt headers, for example /usr/include/qt4/Qt.
- Generate the moc files from the headers that contain Qt macros, such as Q_OBJECT. This can be done using the following command in the project directory before the build process: find . -name ".h" | sed 's/(.)(/)(.*)(.h)/moc-qt4 -D & -o \1\2moc_\3.cpp/' | sh
- 包含带有 Qt 头文件的目录,例如 /usr/include/qt4/Qt。
- 从包含 Qt 宏(例如 Q_OBJECT)的头文件生成 moc 文件。这可以在构建过程之前使用项目目录中的以下命令完成: find 。-name " .h" | sed 's/(.)(/)(.*)(.h)/moc-qt4 -D & -o \1\2moc_\3.cpp/' | 嘘
where you have to define the you want. Just run it once, or use the following command before from the project directory: find . -name "moc_*.cpp" -exec -rm -f {} \;
你必须定义你想要的地方。只需运行一次,或者之前从项目目录中使用以下命令: find 。-name "moc_*.cpp" -exec -rm -f {} \;
- Build your project.
- 构建您的项目。
By the way, have you tried the qt plugging?
顺便问一下,你试过qt插件吗?
J.
J。
回答by Serge Roussak
Here is an improved variant of the jwernerny's makefile:
这是 jwernerny 生成文件的改进变体:
first: all
all clean distclean install uninstall: qtmake.mk
$(MAKE) -f qtmake.mk $@
qtmake.mk: *.pro
qmake -r -o qtmake.mk $<
.PHONY: first all clean distclean install uninstall
It should not to be edited when will be copied to another project, and actually the same rules was merged into one.
复制到另一个项目时不应该编辑,实际上相同的规则合并为一个。