ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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++ ?
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.
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...)
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...
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.