[SOLVED]having trouble including a file
-
hello,
I am a beginner in qt programming, and stumbled across the following problem.
I have this project in which i needed to include some files from other projects however when I tried including a certain file the compiler flagged it as an error. This file belonged to a previous project, and was added to the current project by right clicking on the project tree and clicking on "add existing files". additionally, when I added another file from a different project (both located in the same directory though), it was able to be included on the project just fine. Additionally, when I checked to see if the file (the one that is having trouble being included), is not in my current project directory. does anyone know what may be wrong? perhaps the compiler does not know where to look for the file (even though it found the other one) . all these files are header files.
any help is greatly appreciated
-
the compiler flagged it as an error
What compiler?
Which IDE?
What did the error say?
What are the paths to your project and the include file in question?
What did you try already? -
Does the error also say the path to the file it can't find?
Can you paste the full path (including file name) of the include file, the path to where your .pro file is located and content of the
HEADERS +=
section in your .pro file?Btw. Qt Creator is not versioned with years as new versions come out a couple of times in any given year, so there's no Qt Creator 2015. Current version is 3.5.0 (see Help->About).
-
sorry, thanks for the reply
these are the contents of my HEADERS += file
HEADER += mainwindow.h \ finddialog.h \ sortdialog.h \ spreadsheet.h \ ../../hideDialog/sortdialog.h \ ../../qtdesighn/cells.h \ gotocelldialog.h
it is cells.h that is having trouble being included
-
The paths here are relative to the location of the .pro file. As it is now it is 2 directories up and then
qtdesighn
directory.
This is correct if your .pro file is located inC:/Users/myName/Documents/<something>/<something>/yourprofile.pro
.
If that is not the case then you need to adjust the path in the HEADERS section.Oh, an for the record. This makes this file part of your project. If you only want to include it without making it part of the project you should remove it from HEADERS variable and instead specify a path to directory it is placed in in the INCLUDEPATH variable.
-
sorry, but i tried changing the HEADER section of the .pro file as such
HEADERS += mainwindow.h \ finddialog.h \ sortdialog.h \ spreadsheet.h \ gotocelldialog.h \ ../../C++/mainWindow/cells.h
C++/mainWindow/cells.h is the exact same directory that the .pro file is located in.
however cells.h is still not being included. and also, cells.h is not showing up in the directory of the project even after i added it to the project.
-
That makes no sense. You said the
cells.h
file was located inC:/Users/myName/Documents/qtDesighn
. If so then of course adding../../C++/mainWindow/cells.h
to the HEADERS won't work as there is no such file in that directory.As I said the paths in HEADERS are relative to the location of the .pro file.
..
means "1 directory up".
You didn't post the path to the .pro file, so I'm gonna make up a couple of examples so that you can see how that works.C:/some_directory/some_project/some_header.h
C:/some_directory/some_project/project.pro
HEADERS += some_header.h
C:/some_directory/some_project/include/some_header.h
C:/some_directory/some_project/project.pro
HEADERS += include/some_header.h
C:/some_directory/some_library/include/some_header.h
C:/some_directory/some_project/project.pro
HEADERS += ../some_library/include/some_header.h
C:/some_directory/some_library/include/some_header.h
C:/some_directory/some_project/some_subdirectory/project.pro
HEADERS += ../../some_library/include/some_header.h
I hope you see the pattern. If you post a full path to the .pro file I can tell you what you should put in the HEADERS.
-
alright, i made a new project called tests in which my .pro file has the current path:
C:\Users\myName\documents\tests\tests.pro
the file i am trying to include is:
C:\Users\myName\documents\hideDialog\sortdialog.h
in my HEADERS section of the .pro file i included sortDialog.h as so:
HEADERS += ../hideDialog/sortdialog.h
however when i try to include sortdialog.h in main as so:
#include "sortdialog.h"
the compiler flags it as an error saying "no such file or directory found"
-
HEADERS += ../hideDialog/sortdialog.h
I'm assuming this correctly adds this file to the project and it shows up in the project tree in Creator, right? That's one thing, but...
#include "sortdialog.h"
Adding a file to project doesn't mean you're automatically adding its directory to the paths that are searched for includes.
The above would make the compiler look for the include in the same directory as the file including, which is not the case here.You can do one of these:
- specify the correct path in the include statement:
#include "../hideDialog/sortdialog.h"
- add the directory to searched paths in your .pro file:
INCLUDEPATH += ../hideDialog
and then you can use#include "sortdialog.h"
- specify the correct path in the include statement: