LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2004, 08:38 AM   #1
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Rep: Reputation: 15
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
 
Old 05-26-2004, 08:42 AM   #2
Nis
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: Ubuntu Hoary (5.04)
Posts: 550

Rep: Reputation: 31
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.
 
Old 05-26-2004, 08:58 AM   #3
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Original Poster
Rep: Reputation: 15
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?
 
Old 05-26-2004, 09:01 AM   #4
Nis
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: Ubuntu Hoary (5.04)
Posts: 550

Rep: Reputation: 31
I might have just used gcc to compile some object code. Not sure, because it's been awhile. Sorry for the misinformation.
 
Old 05-26-2004, 10:26 AM   #5
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Original Poster
Rep: Reputation: 15
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++ ?
 
Old 05-26-2004, 11:05 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
In general, g++ can compile C programs. However, you should always use g++ instead of gcc for compiling C++ programs.
 
Old 05-26-2004, 12:56 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ...
 
Old 05-26-2004, 01:50 PM   #8
Nis
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: Ubuntu Hoary (5.04)
Posts: 550

Rep: Reputation: 31
Oh. Didn't know that. Thanks for the update.
 
Old 05-26-2004, 03:15 PM   #9
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
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.
 
Old 05-27-2004, 02:14 AM   #10
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Original Poster
Rep: Reputation: 15
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...
 
Old 05-27-2004, 02:52 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
and what are these errors ?
 
Old 05-27-2004, 03:40 AM   #12
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Original Poster
Rep: Reputation: 15
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.
 
Old 05-27-2004, 07:39 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
try:

#include <cstdio>
 
Old 05-27-2004, 10:32 AM   #14
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Original Poster
Rep: Reputation: 15
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...
 
Old 05-27-2004, 03:07 PM   #15
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
is there any difference/use of su - twice? darkleaf Linux - Software 2 10-12-2004 04:54 PM
what's the difference between ... marlor Slackware 4 06-05-2004 02:38 PM
what is the difference shanenin Linux - Software 4 10-24-2003 09:14 AM
What is the difference? drewski *BSD 5 05-21-2003 03:13 PM
What's the Difference New2Linux1978 Linux - Newbie 4 05-14-2003 06:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration