I am trying to build my code using the Intel C++ Compiler, but for some reason it fails with this error:
catastrophic error: cannot open source file "stdio.h"
In this line #include <stdio.h>.
Any ideas?
stdio.h is a standard header file; it's a bad idea to have a local file of the same name. If you meant to include the standard header, it should be on your include path, and you should include it with
#include <stdio.h>
You should also consider whether you might get more benefit from including <iostream> or including <cstdio> (like including <stdio.h>, but puts the symbols safely into the std namespace).
If you're running on Windows, then installing Visual Studio, then invoking "psxevars.bat" might solve your problem, it solved it for me.
Related
MS Visual Studio 2017, new Windows Console Application project from VisualC++ category. The default stdafx.h header includes the following lines:
#include <stdio.h>
#include <tchar.h>
And below them:
// TODO: reference additional headers your program requires here
Because this comment is put below, not above #include <stdio.h>, I am inclined to believe that stdio.h is included by default not as a convenience for the user who would probably #include it anyway, but is for some reason required? The fact that stdio.h is included rather than cstdio only seems to support this interpretation?
AM I right? Can I safely remove #include <stdio.h>? I'm asking because the very first line in my main function reads:
std::ios_base::sync_with_stdio(false);
Which is obviously incorrect if facilities from stdio are being used anyway, as this header being included would suggest.
1) Can I safely remove the line that says #include <stdio.h> from stdafx.h?
2) Can I safely call std::ios_base::sync_with_stdio(false)?
3) Is the fact that tchar.h is also included by default relevant here?
Yes you can remove it.
You can even name your precompiled header not stdafx.h, or disable percompiled header at all.
It is just convenience and example to start with.
Its up to you if you want to use it, however before removing it read on this MSDN about precompiled Header Files
Hope this helps!
how do I compile a .c or .cpp file using GCC?
I need to include some standard libraries (fstream, string, iostream) - how do I do this?
For clarification, here:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include"ocgenerator.h";
#include"structures.h";
#include"util.h";
#include"util2.h";
using namespace std;
(my .h files are in the same directory as the src file)
If I use the command:
gcc src.cpp -o src.o
I get a lot of errors: memcpy, atoi, atol, strncmp, etc, ... "are not declared in this scope". What should I add to the command?
edit: Or is it a scope thing, and do I have to add std:: to all those functions?
memcpy and strncmp are declared in <cstring>, atoi and atol in <cstdlib>. Just include these headers to bring in their declarations.
Side notes :
No semicolon after preprocessor directives, including #include "".
No using namespace std;, especially not in headers !
(Why is "using namespace std" considered bad practice?)
Since you're building a C++ project, don't forget to link with the standard library via -lstdc++, or use g++ which forwards it to the linker for you.
Note that with GCC you don't have to prefix standard C functions with std:: (as they are also declared in the global namespace), but you should anyway for your code to be standard-compliant.
The cplusplus site helps out. If you search for a specific function, on the top you will find the necessary references for the code to compile:
http://www.cplusplus.com/reference/cstdlib/atol/
You can even check in the URL. In this case, you need 'cstdlib'.
Regarding the compiler, there are several available, g++ is a good option.
I also suggest creating a makefile for automating the building process, because it becomes an headache when your codebase grows.
For any library function you use in your code, read the man page for that function to see which header declares it, and #include that header in any source file that uses the function.
For example, man memcpy shows the memcpy(3) man page (the (3) refers to section 3 of the manual; use man 3 memcpy to specify the section).
NAME
memcpy - copy memory area
SYNOPSIS
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
...
memcpy is part of the C standard library, and is declared in <string.h>. For C++ you can use <string.h>, but it's probably better to use <cstring>, which puts the function name in the std namespace. In general, each standard C header <foo.h> is duplicated as a C++ header <cfoo>.
I get a lot of errors: memcpy, atoi, atol, strncmp, etc,
memcpy and strncmp are declared in <cstring>. atoi and atol are <cstdlib> (or in <string.h> and <stdlib.h> if you're programming in C rather than in C++).
And be sure to use the g++ command, not the gcc command, to compile C++ source. They both invoke the same compiler (gcc compiles C++ code if the source file name ends in .cpp), but the g++ command adds some options that are needed for C++.
I'm trying to make something in Linux, but it complains that it can't find iostream.h. What do I need to install to get this file?
The correct name of this standard header is just iostream without an extension.
If your compiler still cannot find it, try the following:
find /usr/include -name iostream -type f -print
...and add it to your include path, following your compiler's documentation.
The header <iostream.h> is an antiquated header from before C++ became standardized as ISO C++ 1998 (it is from the C++ Annotated Reference Manual). The standard C++ header is <iostream>. There are some minor differences between the two, with the biggest difference being that <iostream> puts the included contents in namespace std, so you have to qualify cin, cout, endl, istream, etc. with "std::". As somewhat of a hack (it is a hack because header files should never contain "using" directives as they completely defeat the purpose of namespaces), you could define "iostream.h" as follows:
#ifndef HEADER_IOSTREAM_H
#define HEADER_IOSTREAM_H
#include <iostream>
using namespace std; // Beware, this completely defeats the whole point of
// having namespaces and could lead to name clashes; on the
// other hand, code that still includes <iostream.h> was
// probably created before namespaces, anyway.
#endif
While this is not exactly identical to the original antiquated header, this should be close enough for most purposes (i.e. there should be either nothing or very few things that you will have to fix).
I needed to compile partport on Debian and had problems (CentOS 4.5 worked fine). I did this without any success:
ln -s /usr/include/c++/4.5/iostream /usr/include/c++/4.5/iostream.h
I discovered that iostream.h was provided from C++, and I found it on CentOS 4.5.
So I copied the file iostream.h from CentOS 4.5 to Ubuntu 11.04 (Natty Narwhal), and it worked:
scp root#ip.centos-4.5:/usr/include/c++/3.3.4/backward/iostream.h /usr/include/c++/4.5/iostream.h
I just wrote a simple C++ program in Visual Studio 2010 and I use ceil function. But I forgot to include the <cmath> and only included the <iostream>. Surprisingly my code compiled successfully and ran without any error. I read a C++ book and it clearly says that to use ceil function you must include <cmath> or <math.h>. Why this happens? Can anyone explain me? Thanks!
The header is indirectly included from some other (indirectly) included header.
To find out which one, enable 'keep preprocessed source' (/P) from the project options and inspect the resulting (*.i) file
Update Just found out that VS2010 has renamed the related option:
Technically speaking, implementations are allowed to automatically include any header in the system headers. But this is implementation defined.
In some cases, <cmath> is already included, in other cases, it isn't - same applies to all the other standard headers.
This issue came up on this question: https://stackoverflow.com/questions/7632926/is-this-a-c-program-or-c-program-how-to-decide
That aside, it's possible that it could be indirectly included by other includes.
I got a fatal error that the file or directory <stdlib> is not found on ubuntu 11.xx when I typed #include <stdlib>.
Is <stdlib> deprecated/removed, or is there something wrong with my GCC installation?
In C++ code, include 'cstdlib' instead.
#include <cstdlib>
If you are using C, include 'stdlib.h'
#include <stdlib.h>
In c++ code, always prefer the cXXX include instead of XXX.h
Presumably you are attempting to include the C standard library header stdlib.h.
Thing is, in C++, the old C headers x.h are deprecated; you should not use them. Fortunately, C++ allows you to use C++ versions of them:
#include <cstdlib>
It's pretty much the same thing, but wrapped into the std:: namespace ... and not deprecated.
Anyway, you got your error because there's certainly no standard header named just stdlib.