1
votes

I am a newby in QT and cannot even get a starting example to work. The problem is that even QtCore is not found. Think something wrong with my path or not the right version is used?

#include <QtCore>
#include <iostream>
#include <QtXml/QXmlSimpleReader>

int main(int argc, char *argv[])
{
QDir xmldir("/xxx/xxx");
QXmlSimpleReader xmlReader;
}

Example of error is: /Users/frank/xxx-build-desktop/../xxx/main.cpp:1: error: QtCore: No such file or directory.

Checked the path and the path to qmake is /usr/bin/qmake If I run qmake -v this is printed:

QMake version 2.01a
Using Qt version 4.7.0 in /Library/Frameworks

I am using Mac 10.6.6 Any reply will be appreciated.

Here is the project file:

QT       += core gui
QT       += xml
QT       += webkit
QT       += xmlpatterns

TARGET = xxx
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

Results from make:

iMac:~/xxx/ qmake -spec macx-g++
iMac:~/xxx/ ls -ltr
total 56
-rw-rw----  1 frank  staff   349 Feb  5 16:06 xxx.pro
-rw-rw----  1 frank  staff  3875 Feb  6 12:56 main.cpp
-rw-rw----  1 frank  staff  7985 Feb  6 12:56 xxx.pro.user
-rw-rw----  1 frank  staff  8974 Feb  6 12:56 Makefile
iMac:~/xxx/ make
g++ -c -pipe -g -gdwarf-2 -Wall -W -DQT_WEBKIT_LIB -DQT_XMLPATTERNS_LIB -DQT_XML_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Qt4.7/mkspecs/macx-g++ -I. -I. -I/usr/include/QtCore -I. -I/usr/include/QtGui -I. -I/usr/include/QtXml -I. -I/usr/include/QtXmlPatterns -I. -I/usr/include/QtWebKit -I/usr/include -I. -F/Library/Frameworks -o main.o main.cpp
main.cpp:1:18: error: QtCore: No such file or directory
main.cpp: In function ‘int main(int, char**)’:
main.cpp:27: error: ‘QDir’ was not declared in this scope
main.cpp:27: error: expected `;' before ‘xmldir’
main.cpp:28: error: ‘xmldir’ was not declared in this scope
main.cpp:28: error: ‘QDir’ is not a class or namespace
main.cpp:28: error: ‘QDir’ is not a class or namespace
main.cpp:28: error: ‘QDir’ is not a class or namespace
main.cpp: At global scope:
main.cpp:25: warning: unused parameter ‘argc’
main.cpp:25: warning: unused parameter ‘argv’
main.cpp:63: warning: unused parameter ‘namespaceURI’
main.cpp:63: warning: unused parameter ‘localName’
main.cpp:63: warning: unused parameter ‘qName’
main.cpp:67: warning: unused parameter ‘namespaceURI’
main.cpp:67: warning: unused parameter ‘localName’
make: *** [main.o] Error 1
iMac:~/xxx/
6
When you run make, it should print the commands being executed (not sure if it does it on Mac though), what is the command for compiling? Does it contain correct paths to Qt include directories?Sergei Tachenov
Are you referring to "/usr/bin/qmake", cause that is specified as the PATH.user3111525
@frankmoss, I was asking about what happens when you run make after qmake (the usual compilation procedure if you don't use an IDE like Qt Creator). However, I'm not even sure that there is something like make on Mac. I read there is something called XCode instead. So I can't really help here as I don't know the details of the build system used.Sergei Tachenov
Sergey, updated the question with the make example. There is make, and infact QtCreator generates Makefile.user3111525
@frankmoss, oh, and here on SO, when replying to someone use @name notation, so the person you are replying to gets notified about your reply. Works only for the first @name in the comment though (and only the first three letters are used, so you may shorten names). But you don't have to do it when replying to the same person who wrote the answer or the question you are posting comment too - that one is going to be notified about your comment anyway (like my previous comment was to your question, so you get notified).Sergei Tachenov

6 Answers

3
votes

For most C++ Qt applications you write, it's not immediately obvious how to compile the application by hand. This is complicated by a number of factors:

  1. The header files may be placed in different directories depending on their version
  2. moc, the meta-object compiler, needs to be run
  3. The correct version of the DLLs need to be included
  4. A number of preprocessor defines need to be in place

Given all of the above, it's easiest to use a build system like qmake, which is native to Qt, cmake, or some other build system that is Qt aware.

For people new to Qt, I recommend qmake.

Here's the basic command line usage. Qmake provides integration with both Visual Studio and XCode, but I won't address that here:

  1. Create a directory for your project
  2. Write whatever files you believe you need
  3. Build a project file for qmake by running qmake on your project as follows: qmake -project "CONFIG+=xml". Since you're on mac, you might also want a -macx option, but I've never used it myself. At least in the past, qmake was not smart enough to pick up the XML dependency, so the "CONFIG+=xml" option adds it explicitly.
  4. Create the makefiles from the project file by running qmake again: qmake
  5. Now that you have makefiles, build the application using make, nmake, gmake, or the corresponding version of make for your system.

If everything has been picked up correctly by qmake, you should now be able to build your application by using make.

When you add new files to your project, they will need to be added to your project file (*.pro) and then qmake rerun without any options.

2
votes

#includes are for header files. QtCore isn't a header, it's a directory that contains headers.

Since you're using QDir, change the first line to this:

#include <QtCore/QDir>
1
votes

It is rather strange that your /usr/include/* directores don't exist. It seems that there is something wrong with the Qt installation. I don't understand Mac specifics, but from what I've been able to find out, it looks like the -F/Library/Frameworks parameter that make passes to G++ should allow you to use #include <QtCore/QtCore> instead of just #include <QtCore>. In a correct Qt installation on other platforms (Windows, Linux), both forms are equally acceptable though. But judging by the fact that G++ doesn't complain about the #include <QtXml/QXmlSimpleReader> line, the form #include <QtModule/ModuleHeader> works fine in your setup, so it could be used as a workaround, provided that there are no other problems.

Note that I wouldn't recommend to include QtCore in any form, though. It includes a lot of stuff most of which is probably useless to you and it will only increase compilation time and the probability of name conflicts. Include only what you intend to use instead, like #include <QDir> or #include <QtCore/QDir> in your case.

0
votes

If you are new to Qt, and there's not special build requirement (PPC, 10.4, etc), I would strongly recommend that you download prepackaged Qt SDK of your platform. And use the included IDE, Qt Creator, to start. That will get your feet wet quickly and less painfully.

0
votes

The variable settings you provided, which seems to be in your makefile, should actually be in a QMake file xxx.pro. You then run qmake, which generates the Makefile from xxx.pro, and then finally can run make. QMake uses variables such as QT and SOURCES, along with its knowledge of Qt and your installation, to produce a correct makefile.

0
votes

If you do not find QtCore in /usr/include/, then this is an installation issue. In 4.7.0 I heard people having the same problem, so I guess there is a packaging problem for macs in 4.7.0.

Try to get 4.7.1 or build your own out of the sources.