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>
Related
Can we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.
I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.
after following up the comments and answers:
using codeblocks ide
i have created a "add" of type File and tried it including in my source file and it worked. attaching the snapshot below.
the aim of my question is to ask if userdefined header files can also omit .h extensions and how?
i am really trying to explore this fact and don't have a good understanding of how compilers stores standard header files.
A easy to understood conclusion is really appreciated
Thankyou.
Can we remove .h extensions while we define our own header file in c++?
Sure, as long as that matches the filename of the file. As far as the language is concerned, the name of the file is largely irrelevant.
However, .h or similar such as .hpp is conventional, and helps the reader of the source to understand what the file is used for. This is an important consideration.
Another consideration is that some tools use the filename as a heuristic to determine the purpose of the file. For example, your IDE might not assume that the file contains C++ code, and thus not enable C++ features such as source analysis unless you follow a common naming convention.
I have created a header file and named it add.h and tried it including in source file using #include "add" but it didn't work.i know i am missing some important concepts here.
What you're missing is that the include directive must match the name of the file. If you include "add", then you must name the file add, not add.h. If you name a file add.h, then you must include "add.h", not "add".
Can we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.
You've misunderstood how the files in the stardard library are named. The header file iostream is actually named iostream and not iostream.hpp or iostream.h (unless you use a very old compiler).
I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.
The reason that doesn't work is because the pre-compiler tries to read the file add and you've named the file add.h.
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.
I am facing a problem regarding iostream file not found in header file.I just added a c++ file in my project a header file also included by default with some macro definition and including iostream file as
#ifndef __ObjectiveCPlus__File__
#define __ObjectiveCPlus__File__
#include <iostream>
#endif
but at this line I am getting error at include line as
I google it a lot and found various types of answer regarding this.But no one is able to correct my errors.
Please help
Thanks!
You don't need <iostream> in your header file, put it in your .cpp file. You're not referring to anything in the iostream library in your header file, using this library is more of an implementation detail.
Why?
I believe UIAppDelegate imports UIViewController.h, that includes MathUtils.h. Because UIAppDelegate's implementation is in a .m file, it's being compiled for Objective-C, and this chain of includes (which is all based on the header files) is including something that is C++. As such, the Objective-C portion is unable to find <iostream>, as that library does not exist in pure Obj-C.
Putting it in your .cpp file limits it to one compilation unit, the MathUtils unit. Having it in your header file includes it in all compilation units that have a dependancy on whatever is using it, which may not be Objective C++.
Alternative Solution
You could have your whole project as Objective C++ (in this case, by changing UIAppDelegate.m to UIAppDelegate.mm), which means C++ can be used throughout. I'm not a fan of this method, and it could mask bad coding practices.
I got the solution from another post:
Renaming your implementation file with .mm extension instead of .m will solve the issue.
I have a question and I could not find a solution, so I ask here :)
I want to create an Foundation-based terminal application/script using Xcode-->Mac-->New-->Command Line Tool-->Foundation
This works and all, but then I want to ADD header.h files to my project.
One of this header.h files does the following:
#include <iostream>
This fails with the error: iostream file not found.
For a test I make a new c++ based terminla script and it does exactly the same:
#include <iostream>
But for some unknown reason it does not fail with an error.
Can anyone tell me, why the c++ script works to include and the objective c not?
objective c: .h file
c++: .cpp file
I renamed the .h to .cpp but then it does not find the NSString and such things.. any solution to use iostream and objective c?
I really need that, thanks
If you want to use both Objective-C and C++ (called Objective-C++) from within the same source module, then use the .mm file extension.
I am trying to include my class header in main and my class implementation. The weird thing is main finds the header file perfectly, but with the class implementation I get "no such file or directory". Why is this happenning? I mean what are the possible reasons one file in my project is able to find a header file perfectly, but another file in the same project can't find the same identical header file.
I am using CodeBlocks 10.05
Thanks
The list of paths searched by the compiler for headers must be different when compiling the class implementation.
Are the class implementation and main files in the same directory? If not then the problem could be that on most compilers the first path searched is the directory containing the file being compiled*, and the header is in the same file as the main file, but not the class implementation file.
I don't know if your compiler offers it, but some have a verbose mode that will list the paths being search for headers. If my above comment doesn't help then maybe that will.
Assuming you're including the file using quotes. The only difference between using quotes and angle brackets in your includes is that using quotes means the directory containing the .cpp file is searched for headers before all the other paths that have been listed (on the command line, in environment variables, in the implicate system include paths).
Make sure your directories are configured right, and as long as the implementation is included in the project it should compile properly. Also as Fred Larson said, make sure you put "quotes" around the file, not angled brackets, otherwise it looks in the standard directories where it won't find your file.