7

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 question didn't help me(

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)

Thank you a lot! Nikolai.

Community
  • 1
  • 1
MTuner
  • 437
  • 1
  • 6
  • 14

4 Answers4

8

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
jwernerny
  • 6,978
  • 2
  • 31
  • 32
  • But where do you get .pro file? How would it change if you have custom big hand-written Makefile also (legacy have-to-support-project)? – Roman Feb 13 '18 at 14:40
  • @Roman, the .pro is part of the qt project you are working with. It can be generated from the source [qmake -project](http://doc.qt.io/qt-5/qmake-running.html), created while in Qt Creator, or just written by hand. – jwernerny Feb 13 '18 at 19:32
3

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?

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Luca Carlon
  • 9,546
  • 13
  • 59
  • 91
  • Thank you for the answer! I added /usr/include/qt4 to project includes, but make returns an error "QtGui: No such file or directory" (main.cpp)... Thus, I can't even get Makefile... It seems to be really bothering, you right). I can use QT Creator, but I like Eclipse and some Eclipse plugins very much. – MTuner Oct 15 '11 at 14:27
  • 1
    Pay attention to insert the correct include paths. Usually /usr/include/qt4 does not contain all the headers. Also, the error seems to signal a simple error in the Makefile. Anyway I've been using Eclipse with Qt, but I find Qt Creator better. – Luca Carlon Oct 15 '11 at 14:51
  • Oh... I hoped other solutions (may be, other integration plugins) exist... Ok, let's your tips would be "the answer") – MTuner Oct 16 '11 at 00:42
  • Not sure if @MTuner has solved `include` issue, but as @Luca Carlon pointed out, precise path is necessary. For ex., I needed /usr/include/{ qt4, qt4/QtCore, qt4/QtGui } to compile even the simplest GUI program in this tutorial (http://goo.gl/W8ssQ). – IsaacS May 24 '12 at 16:54
  • Btw @Luca Carlon, I found this (i.e. using Qt from Eclipse w/o making QtGui project) isn't that bothering - instead I thought quite normal steps that I need for referencing libraries, by using Eclipse' GUI to configure include & linking libs (w/o manipulating `Makefile` by myself). I've installed Nokia's Qt Eclipse plugin but I think my project doesn't take advantage of the plugin since it's not a QtGui prj. – IsaacS May 24 '12 at 16:59
3

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.

J..
  • 31
  • 1
1

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.

Serge Roussak
  • 1,731
  • 1
  • 14
  • 28