Eclipse, QT and “C++ project”: is it possible?

断了今生、忘了曾经 提交于 2019-11-29 04:29:27
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.

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.

By the way, isn't it possible for you to use Qt Creator?

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.

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").

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

This is very easy making use of Netbeans, since qt is integrated in the c++ projects.

But if you use Eclipse, as is my case, you could follow these two steps (for linux users):

  1. Include the directories with the Qt headers, for example /usr/include/qt4/Qt.
  2. 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

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 {} \;

  1. Build your project.

By the way, have you tried the qt plugging?

J.

Here is an improved variant of the jwernerny's makefile:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!