0

So I've read a few posts and articles about how to go about doing this and one option was to do:

#include "../file/file.h"

But when I use this method, it still cannot find the file. The other way was to add the root of the project to the include path.

So here is an example directory:

main_dir
   - dir2
      -dir3
        -header.h
   -dir4
      -dir 5
        -source.cpp

So if I am trying to include header.h from source.cpp, how do I do this?

My .pro file

#-------------------------------------------------
#
# Project created by QtCreator 2015-02-17T12:52:00
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = project1
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    dir4/dir 5/source.cpp \

HEADERS  += mainwindow.h \
    dir2/dir3/header.h

FORMS    += mainwindow.ui
UioShi
  • 21
  • 1
  • 7
  • 1
    Many operating systems would consider `.../file/file.h` to be a malformed path. Did you mean `../../file/file.h`? – Drew Dormann Feb 17 '15 at 17:36
  • Well, I meant ../file/file.h. I put an extra period, but I was essentially restating what I found from other threads and articles. – UioShi Feb 17 '15 at 17:43
  • My version is `#include "dir2/dir3/header.h"`. Paths should be relative to a folder that contains `.pro` file (no mater where the `*.cpp` file is) – Ivan Aksamentov - Drop Feb 17 '15 at 17:46
  • @Drop I also read that. When I begin to use it in Qt Creator, Qt Creator recognizes it and shows me what files I can use when I attempt the inclusion. However, when I begin to build it, it says that the file does not exist and then stops. – UioShi Feb 17 '15 at 17:50
  • Then show us your `.pro` file. Does `dir2/dir3/header.h` physically exist? – Ivan Aksamentov - Drop Feb 17 '15 at 17:50

1 Answers1

1

There are many ways to solve the problem.

  1. Use #include "../../dir2/dir3/header.h" in source.cpp.

  2. If you can add main_dir to the list of include directories, (-I<main_dir_path>), then you can use #include "dir2/dir3/header.h" in source.cpp.

  3. If you can add the toplevel directories under main_dir -- main_dir/dir2 and main_dir/dir4 -- to the list of include directories, then you an use #include "dir3/header.h" in source.cpp.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Okay, that makes sense. But how do I do this in Qt Creator? – UioShi Feb 17 '15 at 17:51
  • @UioShi, I am not familiar with Qt Creator. Can't help you there. – R Sahu Feb 17 '15 at 17:54
  • 1
    @UioShi Add this line to you `.pro` file to add an include path: `INCLUDEPATH += `. This causes QMake (used by Qt Creator) to set everything correctly. So you do not need to manually add the `-I` parameter. – Simon Warta Feb 17 '15 at 21:30