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

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.).

Related

Compiling with g++, using vector and array libraries

When compiling my cpp file on linux with the following line:
$ g++ -o blabla blabla.cpp
I get the following message on stdout:
In file included from
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/array:35,
from blabla.cpp:5: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../
include/c++/4.4.7/c++0x_warning.h:31:2:
error: #error This file requires compiler and library support for the
upcoming ISO C++ standard, C++0x. This support is currently
experimental, and must be enabled with the -std=c++0x or -std=gnu++0x
compiler options.
The script does #includes the <vector> and <array> libraries, so I don't know why it fails.
What causes this error?
Above error are coming because you are using the latest feature of C++, and you default version is older than required.
Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable.
g++ -std=c++0x -o blabla blabla.cpp

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.

gcc don't see standard header files

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

Linking C++ static Library in C using gcc

In the following code, I am trying to call a dummy function written in C++ (using C++ header files like ap_fixed.h, ap_int.h) from a C function. The code runs fine when I compile with g++. But when I use gcc for compiling test.c, it throws an error because I have included a C++ header file which is a valid error.
Is there any workaround to compile using gcc? I have read from some posts that it is not a good practice to merge C/C++ code in this manner. Please enlighten me if there are any serious repurcussions of working with a large C codebase and doing similar stuff.
Thanks
Header File: testcplusplus.h
#include "ap_fixed.h"
#include "ap_int.h"
#ifdef __cplusplus
extern "C" {
#endif
void print_cplusplus();
#ifdef __cplusplus
}
#endif
testcplusplus.cc
#include <iostream>
#include "testcplusplus.h"
void print_cplusplus() {
ap_ufixed<10, 5,AP_RND_INF,AP_SAT > Var1 = 22.96875;
std::cout << Var1 << std::endl;
}
test.c
#include <stdio.h>
#include "testcplusplus.h"
int main() {
print_cplusplus();
}
Commands Used:
g++ -c -o testcplusplus.o testcplusplus.cc
ar rvs libtest.a testcplusplus.o
gcc -o test test.c -L. -ltest
Error:
In file included from ap_fixed.h:21:0,
from testcplusplus.h:1,
from test.c:2:
ap_int.h:21:2: error: #error C++ is required to include this header file
The problem here is that the C++ header ap_fixed.h is included from the C program test.c (indirectly via testcplusplus.h).
The solution is to remove the include of headers "ap_fixed.h"
and "ap_int.h" from testcplusplus.h and include them directly from testcplusplus.cpp. The C program doesn't need to know about these anyway, only the C++ wrapper uses them directly.
In a larger example it might be appropriate to split testcplusplus.h into two headers: one that contains only declarations of the external interface you are presenting to the C environment, and another containing the rest - declarations needed internally in the C++ implementation and any required includes.
Once you have done this, you will still face linking errors because the executable that is produced will contain references to symbols from the C++ runtime libraries, plus any other libraries that your C++ code uses. To solve this, add -l directives when compiling the final executable, eg:
gcc -o test test.c -L. -ltest -lstdc++
You do not need to include ap_int.h and ap_fixed.h at this point, as the declaration of the print_cplusplus function does not need those definitions.
Rather, include them in testcplusplus.c, so the C compiler can only see the C compatible interface of the C++ code.

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.