Is it possible to include a header file in the middle of the code in a c++ program? I mean i wanted to include a header file "cstdlib" in the middle of a program.
It is possible to use the #include preprocessor directive anywhere in your code. However, all it does is that it adds the content of the file you are including instead of the directive. So it would not make sense to #include an external library in the middle of a function.
Related
I am using an external library in my C++ program. This library has a fie named "Common.h". Without knowing about this file, I also created a "Common.h" in my program. Using the compiler flag "#pragma once" in the headers I could ensure that both the files can be included in the compilation. However, I realized that when I call my "Common.h" in my program, the preprocessor wrongly includes the "Common.h" from the external library which breaks the compilation. Is there any option like "namespace" which allows me to include the correct file. I find it really difficult, as we may not (indeed need not) aware about all the files in the external library.
Usually program has several so called include paths to look for header files. It seems you have included both path to directory containing your "Common.h" file, as well as library headers directory. As for me, perfect solution seems to remove include path of library files and use explicit relative path, as:
#include "mylib/include/Common.h"
You can simply rename your "common.h" file and then include that
If I have a program header file named program.h and a template named program.template, I learned that you need to #include "program.template" at the bottom of the program.h file before #endif.
This seems inconsistent with previous methods of having #include on the top of the file. Why is this?
My other question is, do you need #include "program.h" in the program.template file? Why or why not? If so, on the top or on the bottom of the file?
Thanks!
#include is a C/C++ preprocessor directive. It tells the compiler (pre processor component) to dump the contents of file X (e.g. a header file) into the source code of the current file at the #include location.
The #include directive can be used in many ways which require placing it at the start, end or the middle of another header/C/CPP file.
Without seeing your code, it's hard to tell what or why it was done.
I have a question regarding where to #include iosteam and vector? In the main.cpp, header.h or in the memberfunction.cpp? Seems I needed using namespace std in main.cpp but #include< functional > in the header file>. Is there any robust way to do it? Thanks!
Simple Rule:
Include the header file only in those files which need it.
If your source or header file does not use any constructs defined/declared in the header file then there is no need to include that header. Doing so only brings unnecessary code in to those translation units thereby corrupting the namespace and possibly increasing the compilation time.
For readability reasons you want to include headers only in those translation units using them. So in a source code not using at all std::vector template you would not #include <vector>, hence you would put that include before your own #include "myheader.h"
However, you may want to decrease compilation time by pre-compiling the header.
As I explain in this answer, precompiled headers with GCC works only if all your program has one single header containing all the includes. For that reason you would want to have a single myheader.h which is itself including <vector> (even for the few compilation units not using it).
Pre-compilation of header files is compiler (and perhaps system) specific.
If you use vectors, or input/output streams in any way in your header.h (for instance, parameters of that type etc.), then it is better to include iostream and/or vector there. If you use them only internally in your memberfunction.cpp, include it there (it is of no use to the rest of the code).
I have "Hello World" code that uses function fhi from another hi.cpp file that has it's header.
Correct my if my understanding is wrong according following:
I can do include cpp file like #include "c:\c\hi.cpp" instead of using header without any problems except that fact that it looks more readable in header file.
If I include header like sample in my main program hi.h, must hi.h include hi.cpp, or it is done automatically according the same file name hi. I'm wondering how compiler knows where is function fhi body.
Is it possible to have different names for header and cpp files?
Programm:
#include "stdafx.h"
#include "c:\c\hi.h"
int _tmain(int argc, _TCHAR* argv[])
{
fhi(1);
return 0;
}
hi.h
#include <cstdlib>
#include <iostream>
int var;
int fhi(int f);
hi.cpp
#include <cstdlib>
#include <iostream>
int fhi(int f)
{
return 0;
}
must hi.h include hi.cpp
No. hi.h contains only declarations, that can be other by other .cpp files.
I'm wondering how compiler knows where is function fhi body.
It doesn't. You need to compile all *.cpp files into the object files. In your case, you will have two object files: program.o and hi.o. The linker can now take these two object files, and spit out the executable. References to other functions(in this case the actual definition of fhi(..)) is resolved in this stage.
Also why are you using absolute paths in #includes? It will break when you move the "c" directory around.
What normally happens is that the build system compiles the .cpp files into object files, that then are used to build the main executable. The means to tell this to the build system vary greatly.
One important point is that your hi.cpp must include hi.h. You should also put an include guard in hi.h, to make it safe to be included more than once in a translation unit.
I can do include cpp file like #include "c:\c\hi.cpp" instead of using
header without any problems except that fact that it looks more
readable in header file.
yes, you can do so but it is not recommended, one of the problems is encapsulation; you are not hiding implementation details. readability as you mention is also a concern, a header is easier to read since it clearly shows what methods are public.
If I include header like sample in my main program hi.h, must hi.h
include hi.cpp, or it is done automatically according the same file
name hi. I'm wondering how compiler knows where is function fhi body.
the header needs to be explicitly included in hi.cpp and any .cpp file that use the class defined in the header.
Is it possible to have different names for header and cpp files?
yes but it is not recommended, it makes it more difficult to find things.
as a general rule: think about that other programmers may want to look in your code so you need to structure it so that it is easy to read and understand as well as making it easier for you 2 years down the road to remember where things are.
In Visual Studio all CPP files included in the project will be compiled to produce OBJ files. These OBJ files will be linked together to form the EXE or DLL.
Including files are similar to pasting the contents of the file at that location. The only difference is that this pasting is done by the pre-compiler during compilation.
Finding out where a function body resides is done by the either the compiler if the function is inline or by the linker when the final binary is created.
First, if the header file is in the same directory as the source file including it, you can use just
#include "hi.h"
In other words, you don't have to use a full path. (See e.g. the inclusion of "stdafx.h".)
Second, in your header file you don't need to include other header files, unless you need types from those. In your header file you don't have anything that needed from the header files you include.
Third, you should protect header files header files from being included more than once in the same source file, this can be done with a so called include guard, on in some compiler via a special directive called #pragma once.
Fourth, in your header file you define a global variable var. This variable will then be defined in every source file you include the header file in, which will lead to errors. You need to declare the variable as extern:
extern int var;
Then in one source file you define the variable like you do now.
Fifth, you should never include source files in header file (with some special exceptions that you don't have to think about yet). Instead you add all source files to the project (I assume you are in MS VisualStudio) and it they will all be built and linked together automatically.
Sixth, since you seem to be using VisualC++, then you are probably using something called precompiled headers. This is something the compiler uses to speed up compilation. However, for this to work you have to include "stdafx.h" in all source files. That include actually has to be the first non-comment line in each source file.
I wrote a simple C++ program to parse an XML string, called sample.cpp. The program includes a header file, tinyxml.h. When I compiled the program on a unix machine I got the error:
tinyxml.h: No such file or directory
How can we add new header files to the standard library and make them compile? Can anyone please help to get it done? Thank you
You need to tell your compiler where to find the header file. This depends on the compiler, but is typically done by specifying -I<directory> on the command line.
If the header file is in the same directory as the cpp file, you need to include it in quotes, instead of angle brackets, ie.
#include "tinyxml.h"
Instead of
#include <tinyxml.h>