tags:

views:

70

answers:

7

Hi. I'm trying to create project structure like below

test/
test.pro
build/
  build.pro
  main.cpp
gui/
  gui.pro
  mainwindow.h
  mainwindow.cpp

My test.pro looks like this:

TEMPLATE = subdirs
SUBDIRS = gui
CONFIG += ordered
SUBDIRS +=build

build.pro

TEMPLATE = app
QT += core gui
SOURCES += main.cpp
LIBS += -L../gui

gui.pro

TEMPLATE  = lib

SOURCES += \
   mainwindow.cpp

HEADERS  += \
   mainwindow.h

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(){}

MainWindow::~MainWindow(){}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
   public:
     MainWindow();
     ~MainWindow();
 };

 #endif // MAINWINDOW_H

main.cpp

#include <QApplication>
#include "gui/mainwindow.h" //error

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //MainWindow w;
    //w.show();
    return a.exec();
}

I got an error during compilation:

../../test/build/main.cpp:2:25: error: gui/gui.h: No such file or directory

What am I doing wrong?

A: 
test/
test.pro
build/
  build.pro
  main.cpp
gui/
  gui.pro
  mainwindow.h
  mainwindow.cpp

There is no file named gui.h in gui directory - causing the error.

EDIT

May be you wanted gui/mainwindow.h to include?

Donotalo
+3  A: 

error: gui/gui.h: No such file or directory

That's because there is no such file in that directory. :)

There are only 3 files in that directory gui.pro mainwindow.h and mainwindow.cpp.

I think you wanted to include "gui/mainwindow.h", right?

Prasoon Saurav
A: 

It should be gui/mainwindow.h, my mistake.

paw4el
A: 
#include <QApplication>
#include "gui/mainwindow.h" //error

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //MainWindow w;
    //w.show();
    return a.exec();
}

Yes, In the first post I made a mistake. But this code also causes an error:

../../test/build/main.cpp:2:28: error: gui/mainwindow.h: No such file or directory

I do not know what I'm doing wrong? When I comment out #include "gui/mainwindow.h" then everything is OK.

paw4el
A: 

I do not know what I'm doing wrong? When I comment out #include "gui/mainwindow.h" then everything is OK.

I think the problem is in your include directive. Try changing it to

#include "../gui/mainwindow.h"

When the preprocessor finds an #include directive it replaces it by the entire content of the specified file. There are two ways to specify a file to be included:

#include "file" #include <file>

The only difference between both expressions is the places (directories) where the compiler is going to look for the file. In the first case where the file name is specified between double-quotes, the file is searched first in the same directory that includes the file containing the directive. In case that it is not there, the compiler searches the file in the default directories where it is configured to look for the standard header files. If the file name is enclosed between angle-brackets <> the file is searched directly where the compiler is configured to look for the standard header files. Therefore, standard header files are usually included in angle-brackets, while other specific header files are included using quotes.

http://msdn.microsoft.com/en-us/library/36k2cdd4%28VS.80%29.aspx

Edited:

Now for the undefined reference error. You need to change your build.pro file.

Change:

LIBS += -L../gui 

to something like

LIBS += -L../gui/debug -lgui

(the path ../gui/debug works for me because that is the place where the lib "gui" is located)

The idea is that you have to separate the directory where the library is located from the library name (without the extension and without any 'lib' prefix).

See this question here: http://stackoverflow.com/questions/718447/adding-external-library-into-qt-creator-project

bruno
+1  A: 

Thanks bruno. I changed the line:

#include "gui/mainwindow.h"

to

#include "../gui/mainwindow.h"

But now I have error like below:

undefined reference to `MainWindow::MainWindow()' 
undefined reference to `MainWindow::~MainWindow()'

My mainwindow.cpp file looks like below:

#include "mainwindow.h"

MainWindow::MainWindow()
{}

MainWindow::~MainWindow()
{}

How should I add a mainwindow.h file to mainwindow.cpp??

paw4el
+2  A: 

When I changed build.pro file to:

TEMPLATE = app
QT += core gui

SOURCES += main.cpp

LIBS += -L../gui/debug -lgui

application exited with code -1073741515.

Maybe the path in libs section is wrong?

Below files tree generated by qt:

test/
  test.pro
  build/
    build.pro
    main.cpp
  gui/
    gui.pro
    mainwindow.h
    mainwindow.cpp

test-build-desktop/
  Makefile
  build/
    debug/
    release/
    Makefile
    Makefile.Debug
    Makefile.Release
  gui/
    debug/
      gui.dll
      libgui.a
      mainwindow.o
    release/
    Makefile
    Makefile.Debug
    Makefile.Release

I checked the exe file by Dependency Walker: LINK to image

paw4el
what is test-build-desktop?
bruno
I've created a project with a tree equal to yours and all is working for me. I just needed to change the include directive and the LIBS section of the .pro file.
bruno
Only one last thing; when running your app, don't forget to place the gui.dll in the some directory as the .exe of your application or in a directory in the PATH. The same should be said about the qtcored4.dll and qtguid4.dll
bruno
Everything is working OK. The application works correctly. Thank you very much for your help.
paw4el