LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-30-2005, 03:10 AM   #1
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Rep: Reputation: 30
Intermixing C / C++ code


Is it possible?
I have certain aspects of my programs that are favourably to be written in C, while the large part is still C++...
I know I have to take care of data mangling. What other aspe cts do I need to look after?
Thx
Jack
 
Old 12-30-2005, 04:53 AM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
All valid C programs are valid C++ programs. So long as you load the proper libraries, you should be able to intermix the code with no problems.
 
Old 12-30-2005, 05:24 AM   #3
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
I have heard function name mangling in C++.
It involves renaming overloaded fns to identify them and that too will be done by the compiler(even for normal fns).(So what is data mangling?)

To overcome this use extern "C" prefix before functions.

Apart from this to my knowledge there isn't any mangling stuff.
And as scuzzman said almost all C programs are C++ progs.
I cant understand what u mean by a C/C++ design
 
Old 12-31-2005, 04:00 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If you #include your C into a C++ file to compile then your C will get mangled. There are 2 ways around this:

1) Keep your C in separate files, compile them as C into .o files, then make .o files from your C++, and then link them together

2) Put the things you don't wan't mangled into this:
Code:
extern "C"
{
        //C is safe here!
}
That second way you can even put some of your C++ declarations in there, then define them outside of it using C++:
Code:
extern "C"
{
        void *getHandle();
        //Can only do C here...
}

void *getHandle()
{
        //But I can put C++ in the definition here!
}
By the way, name mangling only really matters if you are building a shared library or something that you will link to from other programs. If you don't do that, you don't really have to worry about it; even though g++ mangles C++ names, it's consistent, so it knows what all the mangled names are supposed to be.
ta0kira
 
Old 12-31-2005, 08:06 PM   #5
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Yes, and to make them compile safer in both C++ & C compilers you may use:

Code:
#if defined(__cplusplus)
extern "C" {
#endif

/* stuff */

#if defined(__cplusplus)
}
#endif
 
  


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
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM
Diffrerence between position independent code and Relocatable code? eshwar_ind Programming 7 05-11-2004 01:40 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM

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

All times are GMT -5. The time now is 10:43 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