I have been trying to import iostream into a custom block, I added the line
#include <iostream.h>
in the .h file and in the .cc file but I get the error:
fatal error: iostream.h: No such file or directory
The iostream header is <iostream>, not <iostream.h>. The error you're getting suggests that the compiler is looking for iostream.h, which suggests that you might be including the wrong header.
Try changing the header to <iostream> and see if that fixes the problem. More generally, make sure you aren't including any C++ standard library header files suffixed with .h unless they come from C as well (in which case you should probably use the C++ versions of the headers anyway).
Hope this helps!
Related
When I try to compile a C++ program on MAC I get the following error
In file included from
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ext/pb_ds/assoc_container.hpp:45:
In file included from
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ext/typelist.h:46:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ext/type_traits.h:35:10: fatal error: 'bits/cpp_type_traits.h' file not found
#include <bits/cpp_type_traits.h>
Does anyone know how to fix this? If you do please let me know, help would be deeply appreciated, thank you.
bits/...h files are not part of the standard library. They are some files some popular tutorial had that are distributed with some C++ compilers.
Don't use them.
Instead include the proper C++ standard header files.
If you google "cppreference some_std_token", you'll find a page documenting it, and the first thing will be an #include directive saying which header file it is from.
Delete all of your #include <bits/blah.h> style headers. Rebuild. When you hit an error for a missing token, search for it on cppreference, add the #include to the top of the file, and repeat.
Your code is now a lot more portable and standard compliant.
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.
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 objective c/c++ project under iOS, moving it from OS/X and I get a 'file not found' error on
#include <string>
It's a clean project, and I've just added the files from the old project. Are the STL includes set up in XCode? A find produces a number of possibilities e.g.
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/usr/include/c++/4.2.1/debug/
but adding this to the search path just threw up more errors. Any suggestions?
(apart from don't use string - it's in house code I'm porting)
xcode 4.2.1, ios5.0 running on OS/X 10.7.3 and it's in a .cpp file, the code works fine on OS/X
Are you really sure <string> is included only from a .cpp file?
I just tested on a fresh project, by adding a .cpp file and including <string>, and it works, just as expected (same Xcode version, same SDK version).
But if I include <string> in a .m file, then of course I got a «file not found» compiler error.
So double-check this, as the error may come from here.
Do you include <string> from a .cpp file only, or from a .h file, intended to be used by a .cpp implementation?
Then maybe it's also included by a .m file, hence the error.
Also check your precompiled headers, if any, to see if you include some C++ stuff there...
Also remember, in that later case, that you can rely on the __cplusplus macro, when needed.
If you include a header in an ObjC file and it includes <string> then you hit errors like this. For all .m files XCode uses a C compiler (clang or llvm-gcc). For all .mm files it will use (clang++ or llvm-g++).
I suggest going through and renaming all your .m files to .mm. Including main.m to main.mm.
For me the reason was
MyHeader.h (which includes #include ) target was public. Changed it to project and it compiled.
For cocoa pod:
s.public_header_files = 'MyProject/Classes/**/*.h'
s.project_header_files = 'MyProject/Classes/MyHeader.h'
I've made a struct which does cached file manipulation for my application. I built and tested to in a separate project before putting it into my current one.
Ever since I've moved it over, Xcode refuses to build it. Except when I don't include the file from any Objective-C based header file.
I get one error when I try to include iostream:
And more when I comment it out:
Its file extension is .mm, however I have tried it with .cpp and .hpp, but all of them refuse to build unless I don't #include it from the Objective-C header file.
I've also tried #import from iostream and the file itself in the Objective-C header file.
Any clues as to why this is happening?
As a matter of principle, you cannot include a C++ header file from an objective-C source file.
After all, #including (or #importing) a file only means that the preprocessor replaces the #include directive by the contents of the #included file, before passing the result on to the "actual" compiler. The file extension of the header file is a matter of convention, only, it has no actual meaning.
The error messages your are seeing are clearly the result of the file being compiled as [Objective-]C rather than [Objective-]C++.
Solution: All the source files that include your C++ header file have to be either C++ (.cpp or .cc or a few other extensions) or Objective-C++ (.mm). All source files that include a header file that includes your C++ header file, also have to be C++ or Objective-C++.
EDIT: I just saw that you are defining non-inline, non-template functions in your C++ file that you want to include. This is an unrelated problem, but it will lead to "multiple definition" errors sooner or later. Those function definitions belong in a .cpp, which shouldn't get #included anywhere, only the struct/class definition belongs in a header.
Take a look here and here. You need to tell the compiler to include libstdc++. When mixing Objective-C and C++ all you're files need to have the ".mm" extension, as stated in the second link.
I suspect the error is occurring when you compile a .m or .c file that includes the same header.