LinuxQuestions.org
Visit Jeremy's Blog.
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 11-20-2009, 08:21 AM   #1
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
are g++ gcc libraries incompatible?


I have a library of homegrown C functions;
I write stuff in C and C++
if I compile the library with g++, then gcc compiled sources won't link
and vice versa.

is there a way to get round this or do I need to make
two libraries, one for gcc and one for g++?
 
Old 11-20-2009, 08:29 AM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,125

Rep: Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119
I don't see enough info in your post to be sure of what you are trying to do, but I have a good guess.

If you want functions that can be called from either C or C++, they must have a C interface. In C++ you use
Code:
extern "C"
to declare a C interface for a function (both for declaring an external function with C interface that is called here and defined elsewhere and for defining a function here with C interface to be called elsewhere).

You often need an include file that can declare the C interface and can be included in either C or C++ compiles. Usually that requires something like conditionally #defineing some symbol to be either nothing or "C" so it can be used every place a C interface must be predeclared with extern if including into a C module but with extern "C" if including into C++. Other cases may require two symbols, one for nothing vs. extern "C" { and the other for nothing vs. }

Last edited by johnsfine; 11-20-2009 at 08:35 AM.
 
Old 11-20-2009, 08:41 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Original Poster
Rep: Reputation: 175Reputation: 175
ahh thanks dude!
this works...

Code:
extern "C" {
#include "bill.h"
}
but...

do you happen to know if there's an #ifdef I could use
depending on whether it's gcc or g++?

then I could put the wrapper in the header file.
 
Old 11-20-2009, 09:05 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Original Poster
Rep: Reputation: 175Reputation: 175
Quote:
Originally Posted by bigearsbilly View Post
do you happen to know if there's an #ifdef I could use
depending on whether it's gcc or g++?
amazing what you can find in /usr/local/include

Code:
#ifdef __cplusplus
extern "C" {
    
}
#endif
 
Old 11-20-2009, 12:02 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
As a side note, you should link g++-compiled objects using g++, not ld, which if you didn't already know you'd find out sooner or later.
Kevin Barry
 
Old 11-20-2009, 01:22 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Original Poster
Rep: Reputation: 175Reputation: 175
yes, I always use GNU make so it generally works ok
with the implicit rules.
 
  


Reply


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
Compile error, 'H5G_obj_t' does not name a type, is gcc 4.3 incompatible with gcc 3 ? samrat_rao Linux - Software 5 03-03-2009 03:28 PM
gcc not seeing libraries kuloch Linux - Software 1 08-30-2008 10:30 PM
skipping incompatible C libraries? rgmayoral Linux - Software 2 10-18-2007 07:29 AM
gcc-libraries yashwantpinge Programming 3 11-22-2005 01:17 AM
ld is "skipping incompatible" libraries The_Nerd Programming 3 09-14-2005 12:34 AM


All times are GMT -5. The time now is 04:19 PM.

Main Menu
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