gcc don't see standard header files - c++

I've got simple program in c++ which include two standard headers
stdio.h and iostream. When I type
gcc main.c
I got error: fatal error: iostream: There is no file or directory
even if I rewrite iostream to iostream.h
But when I type:
g++ main.c
everything works fine.
I have to fix this problem cause my IDE(CodeLite) probably use gcc command

That's how you'd compile a C source file. If you're compiling C++ rather than C, then
rename the file extension to something GCC recognises as C++, such as .cpp, .cxx or .cc
invoke the compiler as g++ rather than gcc

Related

How do I generate and use precompiled headers with Clang++?

The official docs state how precompiled headers are to be used through the -cc1 interface, like so to generate them:
$ clang -cc1 test.h -emit-pch -o test.h.pch
And to use them:
$ clang -cc1 -include-pch test.h.pch test.c -o test.s
The problem is that the -cc1 interface is way too low-level to be used by developers from the CLI. In fact, the regular high-level interface ultimately calls into the low-level -cc1 interface by supplying it with a very large set of arguments that are necessary for its correct operation, for example the include paths appropriate for the compile time system. Without these arguments, the -cc1 interface has no prayer of working:
$ clang++ -cc1 /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h -emit-pch -o std.pch
/usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33:10: fatal error: 'cassert' file not found
#include <cassert>
^~~~~~~~~
1 error generated.
Is there a way to use precompiled headers from the high-level interface, such that a developer may conveniently tap into this feature during their daily work?
I don't know why the clang docs do not explain this, but indeed as #selbie surmises, it is possible to use Clang precompiled headers (PCH) without using -cc1.
To generate a PCH file:
$ clang -c -o big-header.hh.pch big-header.hh <other options>
This is the normal compile command, except the input file has the .hh (C++ header) extension. clang will recognize that means to generate a PCH. Alternatively, pass -xc++-header before the header file name if you do not want to use that extension (or another, like .H or .hpp, that is unambiguously C++).
You can tell that big-header.hh.pch is not object code (despite the -c on the command line) because file will say it is "data" (at least my version does) rather than object code. To be extra sure, run strings big-header.hh.pch | head. The first line should be "CPCH" (meaning "Clang PCH").
To use the PCH file:
$ clang -c -include-pch big-header.hh.pch <other compile options>
The addition of -include-pch big-header.hh.pch is the key step that is different compared to gcc. Clang will not automatically pick up PCH files just due their name.
The above was tested with Clang+LLVM-14.0.0 on Linux.
I think the root of your problem is that your filename is test.h and clang thinks you are compiling C code, not C++ code. Hence, when you include <cassert>, clang doesn't know it should be looking at the C++ include path. Try naming your file test.hpp. You only have to name the file you want as the precomp header with .hpp. You can keep all your header files with .h extensions.
In any case, I might be getting this confused with gcc/g++, but Clang follows the same behavior whenever I compile my code on Mac. This is how I make use of precompiled headers. Read on...
If you've got a C++ header file you want to precompile, just compile it as any other .cpp file. Notice that I'm using .hpp as the file extension so the compiler picks it up as a C++ header file.
clang -c precomp.hpp
This will produce precomp.hpp.gch
Now to use the precomp by any other ordinary C++ file, just include the ordinary .hpp file:
// main.cpp
#include "precomp.hpp"
void func1() {...}
void main() {...}
The compiler will automatically use the corresponding .gch file if its present in place of the original .hpp file.

Defining strings in C++ causes a compiler error [duplicate]

It seems to me that gcc can deal with both c and c++ projects,so why is g++/gcc-c++ needed?
What's the difference between g++ and gcc-c++?
gcc will compile C source files as C and C++ source files as C++ if the file has an appropriate extension; however it will not link in the C++ library automatically.
g++ will automatically include the C++ library; by default it will also compile files with extensions that indicate they are C source as C++, instead of as C.
From http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b:
C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx; C++ header files often use .hh, .hpp, .H, or (for shared template code) .tcc; and preprocessed C++ files use the suffix .ii. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).
However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and treats .c, .h and .i files as C++ source files instead of C source files unless -x is used, and automatically specifies linking against the C++ library. This program is also useful when precompiling a C header file with a .h extension for use in C++ compilations.
For example, to compile a simple C++ program that writes to the std::cout stream, I can use either (MinGW on Windows):
g++ -o test.exe test.cpp
gcc -o test.exe test.cpp -lstdc++
But if I try:
gcc -o test.exe test.cpp
I get undefined references at link time.
And for the other difference, the following C program:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int* new;
int* p = malloc(sizeof(int));
*p = 42;
new = p;
printf("The answer: %d\n", *new);
return 0;
}
compiles and runs fine using:
gcc -o test.exe test.c
But gives several errors when compiled using:
g++ -o test.exe test.c
Errors:
test.c: In function 'int main()':
test.c:6:10: error: expected unqualified-id before 'new'
test.c:6:10: error: expected initializer before 'new'
test.c:7:32: error: invalid conversion from 'void*' to 'int*'
test.c:10:9: error: expected type-specifier before '=' token
test.c:10:11: error: lvalue required as left operand of assignment
test.c:12:36: error: expected type-specifier before ')' token
As far as I know, g++ uses the correct C++ linker options whereas gcc uses the C linker options (so you may get undefined references, etc.).

C++ compilation clang error multiple output files

I can't figure out why i can't compile my program with
g++ -std=c++0x main.cpp Sale.h iProduct.h -o w7
every time i try and compile with this command i get a clang error
clang: error: cannot specify -o when generating multiple output files
the program complies fine as a.out and i know i could just rename the a.out file and be on my way but i would like to know why I'm getting this error and how i should fix it.
Thanks
why I'm getting this error and how i should fix it
may I ask why do the .h files affect it?
Because of recent versions of gcc can compile heaader files,
Example:
g++ test.h -o out
file out
out: GCC precompiled header (version 014) for C++
It(gcc) produce precompiled files (https://en.wikipedia.org/wiki/Precompiled_header) in this case.
So when you compile .cpp file and header at the same time,
it can not decide what produce as output precompiled headers, or elf executable.

Compiling in Cygwin: 'EOF' was not declared in this scope, compiles fine in CentOS

I am running into issues compiling Linux-bound applications in Cygwin.
This error:
error: ‘EOF’ was not declared in this scope
is produced by the following code snippet
if (option == EOF) break;
Compiling this in CentOS directly produces no errors.
These are the g++ params passed by the make file:
-g -O0 -Wall -Wextra -std=gnu++11
GCC version on centOS:
4.8.1 20130715
GCC version in Cygwin
4.8.2
I am wondering if I am just missing some libraries in Cygwin, or if this is just a limitation of Cygwin and can't be resolved.
EOF is defined in stdio.h / cstdio. What is likely happening is that you aren't including one of those headers, but are including, for example, iostream. Standard library headers are permitted to cause other headers to get included as well, and some implementations' iostream headers do exactly this, but not all. You shouldn't rely on it. If you use EOF, add an explicit include for the appropriate header in your own code. (Even if it isn't your code, which it isn't in this case, the modification required in the source code is the same.)

Compiling using Command Line g++ Question

I'm trying to create a Makefile which compiles a specific program.
I have 1x .cpp file and 2x .h files.
So I would go
g++ source.cpp header1.h header2.h -o programOut
The thing is i'm getting an error.
Mainly in the second header file where Appointments is a class defined in the first header file.
The second header file mainly contains (function prototypes?) I just got the other functions that I used, removed the implementation and placed it in there.
error: ‘Appointments’ does not name a type
error: ISO C++ forbids declaration of ‘left’ with no type
error: ‘Appointments’ does not name a type
error: ISO C++ forbids declaration of ‘right’ with no type
error: ‘string’ does not name a type
error: ‘time_t’ does not name a type
It compiles fine if I go g++ source.cpp -o programOut
however when I added the .h files when i type g++, it gives me the error shown above.
Any ideas why?
Typically, you should not include header files in your list of files to compile. Rather, your source files (like source.cpp) will already #include the headers they need. If you actually have non-prototype code in your headers that you need compiled into their own .o files, you're doing it wrong.
g++ expects the files passed on the command line to be compilation units. Header files are not compilation units, and as such should not be passed on the command line in the first place.
g++ -Wall -Werror source.cpp
but you have to be in the same directory where the code is present.