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 07-20-2010, 01:51 PM   #1
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Rep: Reputation: 15
gcc for .cpp file


Hi,

I have few source files (.c) working perfectly fine in gcc. Now i have to change the extension to cpp. When I did that it gave me a whole bunch of errors. any suggestions ??
 
Old 07-20-2010, 01:59 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by yousafsajjad View Post
Hi,

I have few source files (.c) working perfectly fine in gcc. Now i have to change the extension to cpp. When I did that it gave me a whole bunch of errors. any suggestions ??
I do not quite understand what you want to do and why, anyway, C++ files (they might have .cpp extension) are compiled by 'g++'.
 
Old 07-20-2010, 02:02 PM   #3
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
well .. i made a project using c language and used gcc compiler. Now i am being told that all those files should be a .cpp extension files. So i thought that just changing the file extension would do the trick but sadly its not. I tried using g++ and it gave me a pretty much the same errors...
 
Old 07-20-2010, 02:59 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by yousafsajjad View Post
... Now i am being told that all those files should be a .cpp extension files. ...
Were you supposed to make the project in "C" or in C++ ? It's hard to believe someone would insist on "C" files having .cpp extension.
 
Old 07-20-2010, 11:38 PM   #5
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
well i was told to make it in c but for some compatibility reason i need the file extension to be cpp instead of c. So does anyone know any trick
 
Old 07-21-2010, 01:28 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
As sergei states in his first reply, use g++ instead of gcc.

Alternatively you can read up on the -x option (man gcc).
 
Old 07-21-2010, 01:28 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
May we confirm that the project is being done under linux and not windows?
 
Old 07-21-2010, 04:16 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
well,
C is not C++.
.cpp is a C++ extension.
g++ behaviour is not the same as gcc or cc.

things acceptable in C aren't in C++.
let me guess 'deprecated cast from const char * to char *' and such like?

you will probably need to edit the code.
some clues would be nice.
 
Old 07-21-2010, 07:05 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by yousafsajjad View Post
well i was told to make it in c but for some compatibility reason i need the file extension to be cpp instead of c.
Maybe you misunderstood the instructions.

Maybe you are supposed to limit yourself to constructs that are valid in both C and C++. In that case, post the code and compile errors. Someone will probably tell you what aspect of the C code is invalid as C++.

If you are literally correct about those instructions (code in C but have the extension be cpp) then you can use the -x option in gcc as Wim Sturkenboom suggested.

gcc -x c file.cpp
 
Old 07-21-2010, 11:03 AM   #10
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
Thank you guys, '-x c' is the thing I was looking for.

Just one last thing, can you tell me by doing so it would be following PURE c calling convention: like: when functions it would be passing all the parameters on the stack and will do NO name mangling like in c++

OS: Linux-32bit
Source files: C coding standard with cpp extension
 
Old 07-21-2010, 11:28 AM   #11
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by yousafsajjad View Post
'-x c' is the thing I was looking for.
I still think it is more likely you are misunderstanding the type of "compatibility" you are supposed to achieve and -x c is only what you think you were looking for, not what you should have been looking for.

But it's still nice to see this forum (Wim Sturkenboom in this case) can dredge up that kind of obscure answer so well. (I stopped reading Wim's post after his un helpful first line without seeing his helpful second line. I knew some such gcc option existed but no clue of details. So I skimmed through the man pages, found -x c, started to post that and only then noticed Wim already had.)

Quote:
can you tell me by doing so it would be following PURE c calling convention: like: when functions it would be passing all the parameters on the stack and will do NO name mangling like in c++

OS: Linux-32bit
Source files: C coding standard with cpp extension
The -x c will entirely treat your .cpp input files as .c files:

It will use pure c calling convention, including NO name mangling because it is processing the input as C, not in any way as C++.

It will pass calling parameters on the stack (as opposed to in registers or by other means) but that is because it is x86 architecture, not because it is C rather than C++.

On an architecture, such as x86_64, where C++ passes parameters in other ways (not all on the stack), C passes parameters the same as C++.

Last edited by johnsfine; 07-21-2010 at 11:40 AM.
 
Old 07-21-2010, 11:53 AM   #12
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
Yeah I do understand what I am trying to achieve is kinda weird but this is what the requirements demand. I was being told to code up the project using C language, C calling conventions (cdecl = __attribute__ ((cdecl))) but with .cpp extension.

Anyhow, Thank you so much everyone for your help.
 
  


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
[SOLVED] Using gcc compiler on a .cpp file mcgill Linux - Newbie 2 05-31-2008 10:25 AM
gcc/cpp install problem ironmc Fedora 1 12-05-2005 10:43 AM
gcc-cpp problems (Mandrake 10.1) fatenabu Linux - Software 0 07-27-2005 01:42 PM
problem with cpp and gcc limboonlee Debian 2 04-25-2005 12:19 PM
Now I have gcc/cpp/cpp, but still can't install sfingerh Linux - Software 2 07-30-2002 03:13 AM

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

All times are GMT -5. The time now is 10:21 PM.

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