I am working in eclipse with MinGw (g++) compiler.
So my problem is when I import .h file from library I have downloaded and I try to build(compile) my project, error is "no such file or directory" for that .h file you can see on picture but still the class from that header file is recognised in the code!
Another strange thing is if I make intentional error in that .h file #import is succesfull and the error from that .h file is shown, that means it trys to compile that .h file.
So it does not know where the file is but it still compiles it ??? what???
cmd line:
g++ -Ic:D:\Documents\cpp_testing\bignum_testing\lib Main.cpp
error:
Main.cpp:10:22: fatal error: Cbignums.h: No such file or directory
#include "Cbignums.h"
^
compilation terminated.
I hope somone will know how to fix this and that it will help other people!
Picture without error in .h file:
Picture with intentional error in .h file!
I suspect that you should
#include "lib/Cbignums.h"
in bignum_testing.cpp if that is the path of the Cbignums.h - relative to the source file.
The IDE recognizes the type because you have the header in your project but the red lines indicate that the include will fail.
EOF is mostly a C thing from <cstdio> (or <stdio.h> form the C standard library, not the C++ one) which you probably forgot to include (e.g. by commenting its //#include <cstdio>)
Try compiling with appropriate -I and -H preprocessor options to g++ (of course, take time to read the documentation to find what they are doing).
BTW, you could ask for the preprocessed form with g++ -C -E Cbignums.cpp > Cbignums.ii then look, with some editor or pager, into the generated Cbignums.ii file.
I strongly recommend you to read a good book about Programming using C++ and to read the GCC documentation. In general, read the documentation of everything you are using for development (tools, e.g. compilers, and libraries)
PS. Every free software C++ compiler I know (GCC & Clang/LLVM...) are command line tools, so run them in a terminal, perhaps thru GNU make. Notice that Eclipse is not a compiler (it is an editor, self-glorified as an IDE), and you probably are not using it cleverly. Don't forget to pass -Wall -g to g++
Related
The following problem: Cygwin or Visual Studio give the error.
CGAL/Splitters.h: No such file or directory
This error appears for every header! The header is implemented by the code
#include <CGAL/Splitters.h>
The error disappears, if I change the code, such that I write the whole path:
#include <c:/path1/path2/CGAL/Splitters.h>
But this is no solution which satisfies me, because I would have to change hundreds of such code fractions.
I think it should be a problem of Visual studio or cygwin. In cygwin I wrote the command:
$ g++ -std=c++11 example.cpp -o example
What is the reason for the error? How can I fix it?
Please give easy understandable instructions, since I am a beginner in C++.
The compiler does not know where to look for the header file
referred to by #include <CGAL/Splitters.h> unless you tell it,
because that header file is not located in any of the compiler's
default search directories for header files.
You tell the compiler where to look by passing it an -I option:
$ g++ -I/path/to/cgal/headers -std=c++11 example.cpp -o example
where that header file will be:
/path/to/cgal/headers/CGAL/Splitters.h
Further reading: An Introduction to GCC - for the GNU compilers gcc and g++
Later
So for every of the header files I have to write the - I option?
No. -I/path/to/cgal/headers/ by itself will of course tell the compiler
where to find every CGAL header file used in your program.
You can check include path in Visual Studio :
Tools > Options > Projects and Solutions > VC++ Directories > Include files
So, check default header file path.
I hope this can help you.
It's been a while since I've dealt with C/C++, so forgive me if this is a ridiculously easy to answer question - I just don't quite know how to "Google" it.
I have a file, "MyFile.h" that includes file "includedFile.h". However, the compiler cannot find the file. Please see below picture:
What I'm doing is moving the project from an old Solaris box to a Linux box. The weird thing is that it worked on the Solaris box as-is but Linux is a little confused.
The makefile that I use for the project hasn't changed either which makes me think that it may be a compiler option...
So how do I tell the compiler on Linux where that include file is, or how do I specify it in "MyFile.h?"
With gcc and clang, you specify the include path using -I:
g++ -o myprogram main.cc extra.cc -I/usr/include/boost -I/my/extra/include/files
You can specify full paths in your files, as in #include "/path/to/my/includedfile.h", but I strongly discourage this as it forces everyone who wants to compile your code to comply with that directory layout.
Also relevant: Read the following link for the difference between #include <file> and #include "file" in gcc: http://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html
Assuming you are using g++, you pass a path with the -I flag.
g++ ..... -I<a path to your includes> -I<another path to includes>
I'm trying to understand how to compile C++ programs from the command line using g++ and (eventually) Clang on Ubuntu.
I found a webpage which explains MakeFiles and I am following their directions. http://mrbook.org/tutorials/make/
I downloaded the four example files into their own directory.
main.cpp
hello.cpp
factorial.cpp
functions.h
I then went ahead and ran their example of how to manually compile without a MakeFile.
g++ main.cpp hello.cpp factorial.cpp -o hello
When I ran the command from above, I received the following error from g++:
main.cpp:1:22: fatal error: iostream.h: No such file or directory
compilation terminated.
hello.cpp:1:22: fatal error: iostream.h: No such file or directory
compilation terminated.
My only experience with writing c++ is using an IDE such as VS C++ Express or CodeBlocks. Isn't the compiler supposed to know what iostream.h is and where to find it?
How do I get rid of this error so the program willl compile?
Thanks for any help.
Before the C++ language was standardized by the ISO, the header file was named <iostream.h>, but when the C++98 standard was released, it was renamed to just <iostream> (without the .h). Change the code to use #include <iostream> instead and it should compile.
You'll also need to add a using namespace std; statement to each source file (or prefix each reference to an iostream function/object with a std:: specifier), since namespaces did not exist in the pre-standardized C++. C++98 put the standard library functions and objects inside the std namespace.
<iostream.h> has never been a standard C++ header, because it did not make it into the C++ standard.
Instead we got <iostream>, in 1998.
Steer well clear of teaching material using non-standard stuff such as <iostream.h> or void main.
However, as a practical solution for your current pre-standard code, you may try to replace
#include <iostream.h>
with
#include <iostream>
using namespace std;
It’s not guaranteed to work, but chances are that it will work.
Another related issue that wasn't mentioned here, so I will include it for anyone's future reference, is from the command line the compiler needs the environment path variable updated to find the location of the c++ header files. In windows you can just update the path environment using the 'advanced system properties' GUI and add the location of the c++ include files. This will update the PATH environment variable in Windows cmd & Cygwin automatically upon restarting the shell.
To update your PATH from Linux or the Cygwin shell type...
PATH=$PATH:/your_path_here
Example:PATH=$PATH:/cygdrive/c/cygwin/lib/gcc/i686-pc-mingw32/4.7.3/include/c++
Also a good idea to add just the include directory as well:
PATH=$PATH:/cygdrive/c/cygwin/lib/gcc/i686-pc-mingw32/4.7.3/include/
...or check the proper directories for the location of your installation's include files, I recommend installing mingw for use with Cygwin, which is envoked with g++.
To install additional needed packages in Cygwin re-run the Cygwin install utility & check install from Internet to add packages from web repositories and add mingw-gcc-g++ & mingw-binutils.
To compile: g++ hello.cpp -o hello
If using the gcc utility instead compile with the command:
gcc hello.cpp -o hello -lstdc++
... to get your executable.
As long as you have either gcc or mingw installed and the path to the c++ include files is in your path environment variable, the commands will work.
I use gcc (running as g++) and GNU make.
I use gcc to precompile a header file precompiled.h, creating precompiled.h.gch; the following line in a Makefile does it:
# MYCCFLAGS is a list of command-line parameters, e.g. -g -O2 -DNDEBUG
precompiled.h.gch: precompiled.h
g++ $(MYCCFLAGS) -c $< -o $#
All was well until i had to run g++ with different command-line parameters.
In this case, even though precompiled.h.gch exists, it cannot be used, and the compilation will be much slower.
In the gcc documentation i have read that to handle this situation,
i have to make a directory called precompiled.h.gch and put
the precompiled header files there,
one file for each set of g++ command-line parameters.
So now i wonder how i should change my Makefile to tell g++ to create
the gch-files this way.
Maybe i can run g++ just to test whether it can use any existing file
in the precompiled.h.gch directory,
and if not, generate a new precompiled header with a unique file name.
Does gcc have support for doing such a test?
Maybe i can implement what i want in another way?
It seems weird to answer my own question; anyway, here goes.
To detect whether a suitable precompiled header file exists, i add a deliberate error to my header file:
// precompiled.h
#include <iostream>
#include <vector>
...
#error Precompiled header file not found
This works because if gcc finds a precompiled header, it will not read the .h file, and will not encounter the error.
To "compile" such a file, i remove the error first, placing the result in a temporary file:
grep -v '#error' precompiled.h > precompiled.h.h
g++ -c -x c++ $(MYCCFLAGS) precompiled.h.h -o MORE_HACKERY
Here MORE_HACKERY is not just a plain file name, but contains some code to make a file with unique name (mktemp). It was omitted for clarity.
There is a simpler way than introducing an #error in precompiled.h: never create this file at all. Neither G++ nor Visual C++ (at least up to 2005) expect the "real" file to be there, if a precompiled version is around (and if they get the necessary compilation flags).
Let's say the list of #includes that we want to precompile is called "to_be_precompiled.cpp". The filename extension doesn't matter much, but I don't like to call this a .h file, since it has to be used in a way different from genuine header files, and it's easier in Visual C++ if this is a .cpp. Then pick a different name to refer to it throughout the code, let's say "precompiled_stuff". Again, I I don't like to call this a .h file, because it's not a file at all, it's a name to refer to precompiled data.
Then in all other source files, the statement #include "precompiled_stuff" is not a genuine include, but simply loads precompiled data. It's up to you to prepare the precompiled data.
For g++, you need a build rule to create "precompiled_stuff.gch" from a source file whose name doesn't matter to the compiler (but would be "to_be_precompiled.cpp" here).
In Visual C++, the string "precompiled_stuff" equals the value of the /Yu flag and the precompiled data loaded comes from a .pch file with an unrelated name, that you also created from an unrelated source file (again "to_be_precompiled.cpp" here).
Only when building with a compiler without precompiled header support, a build rule needs to generate an actual file called "precompiled_stuff", preferably in the build directory away from the real source files. "precompiled_stuff" is either a copy of "to_be_precompiled.cpp", a hard or symbolic link, or a small file containing #include "to_be_precompiled.cpp".
In other words, you take the viewpoint that every compiler supports precompilation, but it's just a dumb copy for some compilers.
I'm experiencing an extremely weird problem in a fresh OSX 10.4.11 + Xcode 2.5 installation. I've reduced it to a minimal test case. Here's test.cpp:
#include "macros.h"
int main (void)
{
return 1;
}
And here's macros.h:
#ifndef __JUST_TESTING__
#define __JUST_TESTING__
template<typename T> void swap (T& pT1, T& pT2)
{
T pTmp = pT1;
pT1 = pT2;
pT2 = pTmp;
}
#endif //__JUST_TESTING__
This compiles and works just fine if both files are in the same directory. HOWEVER, if I put macros.h in /usr/include/gfc2 (it's part of a custom library I use) and change the #include in test.cpp, compilation fails with this error :
/usr/include/gfc2/macros.h:4: error: template with C linkage
I researched that error and most of the comments point to a "dangling extern C", which doesn't seem to be the case at all.
I'm at a complete loss here. Is g++ for some reason assuming everything in /usr/include/gfc2 is C even though it's included from a .cpp file that doesn't say extern "C" anywhere?
Any ideas?
EDIT : It does compile if I use the full path in the #include, ie #include "/usr/include/gfc2/macros.h"
EDIT2 : It's not including the wrong header. I've verified this using cpp, g++ -E, and renaming macros.h to foobarmacros.h
G++ may well indeed be assuming that everything in /usr/include is C. Try compiling your code with -E and studying the line markers in the preprocessor output:
g++ -E test.cpp | grep '^#'
You'll likely see things like
# 1 "/usr/include/gfc2/macros.h" 1 3 4
The 4 is the preprocessor hinting to G++ that it should wrap everything in extern "C", on the supposition that your platform's ancient header files in /usr/include predate C++. See Preprocessor Output in the CPP manual.
These days G++ mostly ignores this hint, because most platforms' C headers are no longer ancient. See the NO_IMPLICIT_EXTERN_C target macro in the GCC Internals manual. But it may be that this old version of Xcode has GCC configured without NO_IMPLICIT_EXTERN_C and thus is listening to the preprocessor's hint. (This is set when GCC itself is built -- I don't think there's a command-line switch to override it.)
You may be able to work around this by wrapping the contents of your header file in extern "C++".
This is a shot in the dark, but is there another file named macros.h somewhere under /usr/include or in your GCC installation? GCC has a facility for wrapping headers, called #include_next, which might be the cause of your problem.
One thing you can do to disambiguate your macros.h from any other macros.h in the include path is to include it as gfc2/macros.h. This way, the compiler will search every directory in the include path for a subdirectory named gfc2 containing a file named macros.h, reducing the chance of a collision. It also prevents you from having to add /usr/include/gfc2 to the include path.
BTW, #include "file.h" searches the current directory first. To skip that and go straight to the include path, use #include <file.h>:
#include <stdio.h>
#include <gfc2/macros.h>
Another approach is to choose a filename that is more likely to be unique, like gfc2macros.h.
Well, it really looks weird...
How does XCode calls g++?
I don't think g++ spontaneously decides that an include file has C linkage just because it's in a different directory. Did you try to compile your project by hand?
Try "g++ main.cpp -I/usr/include/gfc2/". If this solves your problem than it's not g++. Maybe does XCode precompile headers?
Have you tried not changing the test.cpp file at all, but instead when you compile also say:
-I/usr/include/gfc2/
You can see where g++ is looking for includes with the verbose flag:
g++ -v -o test test.cpp
And this will just run the preprocessor and show what is actually included in the file and compiled:
g++ -E test.cpp | less
If the wrong files are getting included (or your header is getting wrapped in another, as bk1e suggests) you'll be able to find out with that output.
I just ran into this issue as well when compiling a C++ project that we normally build on 10.5 and 10.6 (Xcode 3.0+) on a 10.4 PPC machine with Xcode 2.5 installed. It looks as if the preprocessor treats anything added to the gcc include path with '-isystem' as if it should be "extern C". Changing '-isystem' to '-I' resolved the issue.