PDA

View Full Version : How to force include a header file



rawfool
20th May 2013, 10:49
I have a header file viz., customTypes.h and I need to force include like in Visual Studio.
In Visual Studio there is a option in settings, called "Force Include" where we select the file, which does this to to the compiler settings -

/FI"customTypes.h"

Now if I'm using that header file from the path, it's linking me to the file while typying #include "customTypes.h", but on compilation, it's giving some type errors.
Kindly help me how to do this in Qt. Thank you.

ChrisW67
20th May 2013, 11:34
Assuming you are still using the Microsoft compiler then you do it by arranging for exactly the same command line switch. Something like:


QMAKE_CXXFLAGS += /FI"customTypes.h"

I don't know why this would be useful though.

rawfool
20th May 2013, 12:04
I'm using MinGW 4.7 and when I do QMAKE_CXXFLAGS += /FI"customTypes.h", it's saying, error: No such file or directory.
But I've included that header file path in INCLUDEPATH.
What do I do now? Thank you for the reply.

wysota
20th May 2013, 21:07
I don't think GCC supports forced include, whatever that is. If you want a file included in your source code then include it there so that people reading your code can learn the file gets included instead of having to analyze crypting compiler invokations.

ChrisW67
20th May 2013, 21:50
I'm using MinGW 4.7 and when I do QMAKE_CXXFLAGS += /FI"customTypes.h", it's saying, error: No such file or directory.
But I've included that header file path in INCLUDEPATH.
What do I do now? Thank you for the reply.

It may have helped to have mentioned that you were not using the Microsoft compiler. As it stands at the moment the compiler is probably complaining because there is no file called literally '/FI"customtypes.h"': even cursory inspection of existing command lines or the manual would have highlighted that "/" does not introduce a command line switch to GCC.

GCC has an equivalent switch that can be found with less than three minutes and the GCC manual (http://gcc.gnu.org/onlinedocs/cpp/Invocation.html#Invocation):
-include "someobscurefilethatisboundtobelost.h"
Make sure read about where the compiler will be looking for someobscurefilethatisboundtobelost.h.

The correct approach is to fix your source code, not to try and fudge it with obscure compiler options.

rawfool
21st May 2013, 06:45
My customTypes.h file is in C:/svn/trunk/vtd-xml, how do I force include ?
I tried each of the following ways in my .pro file, but it's giving error

-include "customTypes.h"
INCLUDEPATH += <some/path> \
C:/svn/trunk/vtd-xml

-include "C:/svn/trunk/vtd-xml/customTypes.h"

// Without quotes
-include C:/svn/trunk/vtd-xml/customTypes.h

-include -I"C:/svn/trunk/vtd-xml/customTypes.h"
All the attempts giving -

Extra characters after test expression.
[Makefile] Error 3

wysota
21st May 2013, 07:27
QMAKE_CXXFLAGS += "-include ..."

rawfool
21st May 2013, 08:45
Thank you Wysota for the reply.
I did that it's almost working, still I need some more clarifications regd., this.
The file C:/svn/trunk/vtd-xml/customTypes.h is being used by any other files and those needs to be specified. I tried including all those files below mentioned way. But no luck. kindly help me.

QMAKE_CXXFLAGS += "-include C:/svn/trunk/vtd-xml/customTypes.h C:/svn/trunk/vtd-xml/arrayList.h C:/svn/trunk/vtd-xml/autoPilot.h C:/svn/trunk/vtd-xml/binaryExpr.h C:/svn/trunk/vtd-xml/bookMark.h C:/svn/trunk/vtd-xml/cachedExpr.h ...(many other files separated by space)"

error: fatal error: cannot specify -o with -c, -S or -E with multiple files

The files other than customTypes.h need not be force included, but just included because, customTypes.h file is being used by all the other files (and the files are in that specified directory).

wysota
21st May 2013, 09:37
This is not a GCC usage kindergarden. Read the manual. Or better yet fix your code to avoid using forced includes.

ChrisW67
21st May 2013, 23:11
The GCC "-include" option, as the manual clearly indicates (http://gcc.gnu.org/onlinedocs/cpp/Invocation.html#Invocation), takes a single file name as an argument.

If the customTypes.h is already "being used by any other files" then I fail to see why a forced included is required. If the "any other files" contained the line:


#include "customTypes.h"

you would not have the problem you have made for yourself.

rawfool
22nd May 2013, 06:55
I'm force including customTypes.h file which is part of other process developed using Visual C++. Also the files that are including customTypes.h file are part of that process.
I'm trying to load a DLL that parses a huge xml file. That parser DLL is created by other team and they use Visual C++ for development. So that's the reason I'm left with only option of force including the required files for using required DLL APIs.
Thank you.

wysota
22nd May 2013, 06:59
That's not true. If you physically have the file there and its presence is mandatory for the compilation to work then you can as well include the file directly in your files. Using your currrent approach only obscures your source code making it more error prone and more difficult to manage. If the other team provides a library you have to use, then just use it.

ChrisW67
22nd May 2013, 08:12
Here are the relevant docs. Fix your source code, adjust the PRO file and you are done.

Of course, if the other team is producing a library of C++ classes using Microsoft VC++ (and have not produced a C-style interface) then you will not be able to link to it using GCC/GNU ld and this exercise has been largely pointless. You will more than likely need to use MSVC yourself.

rawfool
22nd May 2013, 08:55
Yeah, I've added the required libs and includes the way document says. I'm installing MSVC for Qt for better sync. Thank you.

artt
13th December 2015, 01:48
I have very strange problem. I have main.cpp file with class declaration and definition.
So I decided to move class declarations to separate class.h just by copy-past in notepad and saving it under h extension in working directory.
So I commented the declaration in main.cpp and put "include Class.h" -- but i get error - no such file or directory.

I put additional HEADERS and INCLUDEPATH line in .pro file, but it does not help.

INCLUDEPATH+=D:\Qt1\bin\Filewalker\Filewalker\File walker1
SOURCES += main.cpp
HEADERS+=Filewalker.h Lister.h
What should I do to work class headers in QtCreator - I use MinGW32.
I also want to use this headers with slot function declarations in another widget .cpp file in connect() function.

Added after 37 minutes:

I also do not understand the green wavy line under include statement in my cpp file such as
#include "Filewalker.h" - what kind of error do it show. Once it happened in usual #include <Qt...> but then disappeared

anda_skoa
13th December 2015, 10:06
include Class.h

is not a valid include, it either has to be


#include "Class.h"

or


#include <Class.h>


Make sure you didn't accidentally call the files "class.h".

Also you have a space in your include path but no quotes around it.

Cheers,
_

artt
13th December 2015, 14:05
I insert in my main.cpp such lines -
#include "Filewalker.h"
#include <Filewalker.h>
and both of them are with green wavy line - and is absent in the list of auto-complete of includes.
And no spaces around includepath do not help

anda_skoa
13th December 2015, 14:23
And no spaces around includepath do not help
What?
I wrote that you have a space in your path but you haven't put quotes around it.

Cheers,
_

artt
13th December 2015, 14:49
Should I put it n such manner:
INCLUDEPATH+= "D:\Qt1\bin\Filewalker\Filewalker\Filewalker1"

artt
13th December 2015, 14:50
Error The same message

anda_skoa
13th December 2015, 15:48
INCLUDEPATH+= "D:\Qt1\bin\Filewalker\Filewalker\Filewalker1"

That is now a different path than you had earlier.

Anyway, just put the header into the same directory as the cpp file.

Cheers,
_

artt
13th December 2015, 17:14
The header is in the same directory as cpp file.
why it is different path?

Added after 1 14 minutes:

So are there alternatives to make custom include to works?

anda_skoa
13th December 2015, 20:14
The header is in the same directory as cpp file.

Then you don't need an include path, the same directory is always searched.



why it is different path?

Oh, I don't know, lets see, the first path you posted


D:\Qt1\bin\Filewalker\Filewalker\File walker1

and then the one you posted later


D:\Qt1\bin\Filewalker\Filewalker\Filewalker1

Look different to me.

Cheers,
_

artt
13th December 2015, 21:01
It was in the same directory since the very beginning, but I got such warning that I noted at start of my discussion in this topic. Maybe I wrongly created the .h file? How to save it corecctly in notepad?

anda_skoa
13th December 2015, 22:15
Notepad? Seriously?

Have you checked that the file is actually named what you think it is named?
Not something like Class.h.txt?

I can only recommend to use any form of code editor, or at least a real text editor.

Cheers,
_

artt
13th December 2015, 23:34
Really after browsing in 7-zip I saw that Filewalker.h.txt is really such one - so after renaming to
Filewalker.h it also showed the h-pictogram. Anyway when saving in notepad-I did not saved as textfile but as "allfiles", but I probably should also define encoding not unicode, but UTF-8.
So I should guess so -- but in working folder it is seen as Filewalker.h and as I said it was not saved as text file.