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 |
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. |
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? |
I might have just used gcc to compile some object code. Not sure, because it's been awhile. Sorry for the misinformation.
|
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++ ? |
In general, g++ can compile C programs. However, you should always use g++ instead of gcc for compiling C++ programs.
|
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 ...
|
Oh. Didn't know that. Thanks for the update. :)
|
Quote:
See, in your C++ hello world program it probably had a statement like Code:
cout << "Hello world\n"; Code:
printf("Hello world\n"); |
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: |
and what are these errors ?
|
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. |
try:
#include <cstdio> |
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: |
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. |