LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 07-30-2012, 11:39 PM   #1
cmccullo
Member
 
Registered: Jan 2011
Posts: 31

Rep: Reputation: 0
Mixing Cpp and C


I am trying to compile a program with three parts main.c, file1.c, and file2.cpp. Main.c and file1.c use MIT Kerberos, krb5, functions but no cpp statements. File2.cpp uses MPIR classes so I need to use the g++ compiler. I don't know if these libraries are important, but I include them for thoroughness.

If I compile and link everything with g++, I get a link error 'undefined reference to krb5_read_message' which is in both main.c and file1.c. I even tried changing the file extensions of main and file1 to cpp - no change.

If I compile main.c and file1.c with gcc, and compile file2.cpp with g++, then link with g++, I get a link error 'undefined reference to MyFunction' which is in file2.cpp.

I don't know if the krb5 build is compiled with gcc or g++, but would it help to force the krb5 build to use the g++ compiler? If so, how do I do that?

Last edited by cmccullo; 07-30-2012 at 11:40 PM. Reason: typo
 
Old 07-31-2012, 04:57 AM   #2
business_kid
Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 6,628

Rep: Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586
I'm sure you can grok someone else's makefile for ideas
One way might be to set it up in the Makefile as 2 targets, or 2 directories.

target1:
CC=g++
<stuff>

target2:
CC=gcc
<stuff>

install:
Linking here
<stuff>

Then run make target1; make target2; make install
 
Old 08-06-2012, 11:02 PM   #3
jmajor
Member
 
Registered: Nov 2004
Location: Australia
Distribution: Fedora, Ubuntu
Posts: 55
Blog Entries: 2

Rep: Reputation: 17
The object files between C and C++ use different name mangling. C prepends an underscore, whereas C++ mangles in some argument type info.

To call C from C++ you need to put your your C function declarations (in the header included by C++) inside of an 'extern "C" { .... }' block.

Calling C++ from C is more problematic as the C++ class initialisations for globals may not get called properly without some work. The overloading of functions for differing argument types is likely to upset your poor old C compiler somewhat.

Should be possible for you to write a C++ main() and rename and wrap you C main(). You'll need to do some work to ensure that C is called from C++ and not the other way around.

See http://en.wikipedia.org/wiki/Compati..._C_and_C%2B%2B
 
Old 08-16-2012, 06:17 PM   #4
cmccullo
Member
 
Registered: Jan 2011
Posts: 31

Original Poster
Rep: Reputation: 0
I have no idea what grok is. I tried business_kid's suggestion as best I could understand it and it did not work. Make said target1 was already up to date, or something like that.

Jmajor's suggestion seems very complicated. Thanks, but I'm not trying to make a library.

What I found works is to either change the file extensions from .c to .cpp, or add the compiler option -xc++. Then compile everything with g++.

Last edited by cmccullo; 08-16-2012 at 06:22 PM. Reason: More explanation
 
  


Reply

Tags
cpp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Dj mixing B-Boy Linux - Software 2 06-10-2008 03:41 AM
debian dpkg cpp depends on cpp error darkleaf Linux - Software 2 06-25-2004 02:47 AM
package mixing greythorne Red Hat 3 06-15-2004 09:58 PM
Mixing C and C++ Cruelpeace Programming 1 02-01-2003 11:41 PM
Now I have gcc/cpp/cpp, but still can't install sfingerh Linux - Software 2 07-30-2002 04:13 AM


All times are GMT -5. The time now is 06:50 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration