GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Jul 26, 2014 1:11 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: insert C++ code into C code
PostPosted: Sun Apr 15, 2012 9:09 am 
Offline
Familiar Face

Joined: Thu Mar 29, 2012 9:40 am
Posts: 14
Hi everyone!! I am developing a GTK application in C, and I need to know if it is possible to insert C++ code into my C code.

I am using Linux, gcc compiler, and I can't change to gtkmm to develop in C++.

Thank you very very much!!


Top
 Profile  
 
 Post subject: Re: insert C++ code into C code
PostPosted: Sun Apr 15, 2012 2:37 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
C compilers won't recognize keywords like new, delete, class, template, etc,

I'm in Linux, why you can't change to gtkmm or wxGTK?

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject: Re: insert C++ code into C code
PostPosted: Sun Apr 15, 2012 8:21 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
Hi,

It is not totally clear what you mean by "insert C++ code into C code". Although C++ is based on C with object orientated extras, there are enough differences between the languages that can cause you problems.

These include

- Name mangling
- Extra keywords under C++
- Differences in the way enums and chars are treated

So care must be taken when taking a C language source code and compiling it under a C++ compiler.

_________________
E.


Top
 Profile  
 
 Post subject: Re: insert C++ code into C code
PostPosted: Mon Apr 16, 2012 9:10 am 
Offline
Familiar Face

Joined: Thu Mar 29, 2012 9:40 am
Posts: 14
I can not move to gtkmm because it is a large source code I have not implemented, I have to add some functionalities, and I would like to program them using objects in C++, but my problem is that the compiler should be gcc, because of GTK.

Thank you very much!!!


Top
 Profile  
 
 Post subject: Re: insert C++ code into C code
PostPosted: Mon Apr 16, 2012 5:45 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
You may be able to do this with extra care taken to watch out for the differences between the languages. The C++ objects can only be accessed within a C++ source file. Anything that needs to be accessed by your C code needs to be made accessible using the keyword :-
Code:
extern "C"

This stops the name mangling that would be done normally in C++. It also stops function and name over-riding and other C++ features relating to the variable or function.

If you are not going to be using gtkmm then it may be better to stay with C, especially since the rest of the project is already in C.

Note you do not have to use gcc because you are using GTK+. Code using the GTK+ libraries can be compiled using other compilers.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group