LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   difference between c and c++ (https://www.linuxquestions.org/questions/programming-9/difference-between-c-and-c-186017/)

nimra 05-26-2004 08:38 AM

difference between c and c++
 
I am really confused at the moment. All the time I believed that C is just a subset of C++. I thought that I can just compile a c program with a c++ compiler... I got some c code and now I like to extend the program. But I don't want to program without the possiblity of the object orientation.

How can I change the c code in a easy way to be compatible with c++? Or even better: how can I call the 'c' - functions by c++ code? Do I have to compile the c code with a c compiler and than I just include the .h files in the c++ code?

Thanks for any informations

Nis 05-26-2004 08:42 AM

I'm confused by your explanation, but I believe you're confused by the relationship C and C++. C was first. C++ was next, and was an extension of C. C++ added some very useful stuff, like real support for classes, and handy typing stuff, like the increment operator (++). The ++ also gave C++ it's name: C++ was C incremented.
As for your question, I assume you have some C code you would like to compile. The gcc can compile both C and C++ code so you're good there.

nimra 05-26-2004 08:58 AM

Well, that was what I thought before. So I tried to compile a Hello World program (.cpp) with gcc and it didn't work. I was adviced to take g++ instead, and it worked...

How did you succeded to compile c++ programs with gcc?

Nis 05-26-2004 09:01 AM

I might have just used gcc to compile some object code. Not sure, because it's been awhile. Sorry for the misinformation.

nimra 05-26-2004 10:26 AM

Hmm, I think a have a specific problem.... Mostly, I can compile c code with g++. g++ just produces an error for one specific function (which is not a problem for the gcc).

In general, is it true, that the g++ compile can manage c files? Or did I just use c files which are compatible to the g++ ?

itsme86 05-26-2004 11:05 AM

In general, g++ can compile C programs. However, you should always use g++ instead of gcc for compiling C++ programs.

jlliagre 05-26-2004 12:56 PM

btw, Nis, the ++ operator wasn't added by C++, but existed since the beginning of C, although C++ allows it to apply to non numerical things ...

Nis 05-26-2004 01:50 PM

Oh. Didn't know that. Thanks for the update. :)

vasudevadas 05-26-2004 03:15 PM

Quote:

Originally posted by nimra
Well, that was what I thought before. So I tried to compile a Hello World program (.cpp) with gcc and it didn't work. I was adviced to take g++ instead, and it worked...

How did you succeded to compile c++ programs with gcc?

You can compile C programs with a C++ compiler. You were trying to compile a C++ program with a C compiler. It only works one way.

See, in your C++ hello world program it probably had a statement like

Code:

cout << "Hello world\n";
but that won't work in gcc because that's not valid C. The C way of doing it would be

Code:

printf("Hello world\n");
which as you can see is very different. But that will probably work in g++. I haven't used g++ so I can't really speak for it, but regular C code definitely compiles ok with cxx, the C++ compiler for Compaq Tru64 Unix.

nimra 05-27-2004 02:14 AM

Thanks for your input. Your right, I feel more comfortable with c++ and I also think that I know the c++ specific terms. What I don't understand is, why I can't do it the other way round. Why I can't use the printf comand in c++.

I just want not to substitute printf by cout...

Well, with my g++ compiler, I can compile my own c code (including printf,etc...) :p

I just have executables, already compiled with gcc and I like to include there functions (I've got the header files) in c++ code. At the momentI have c code, which does this. But if I compile this code with g++ instead of gcc then I get errors... :confused:

jlliagre 05-27-2004 02:52 AM

and what are these errors ?

nimra 05-27-2004 03:40 AM

Thanks for your help

I get this output (simplified):

malloc : undeclared
free : undeclared
close : undeclared

So, I added
#include <cstdlib>

And now there is only 'close' left. Do you know which library I have to include ? I tried quite a lot of libraries. I am a little bit surprised, because there is also an open function in the code, which works fine.

jlliagre 05-27-2004 07:39 AM

try:

#include <cstdio>

nimra 05-27-2004 10:32 AM

cstdio doesn't work either

It makes no sense to try every c++ library, doesn't it? I can just find a close function in the fstream header (fgrep ;-)).

The open and close function is used to open and close a pci device. I suppose that the functions are declared in the driver somewhere... But why I didn't find the prototypes in the headers then? And why it works with gcc and not with g++? So there must be another reason... :confused:

jlliagre 05-27-2004 03:07 PM

This works with C because this language doesn't care a lot about prototypes while C++ enforce them.

As last try:

#include <unistd.h>
...

This is where system calls like close() prototypes are declared.


All times are GMT -5. The time now is 04:30 PM.