LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-20-2009, 07:21 AM   #1
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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, 07:29 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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 07:35 AM.
 
Old 11-20-2009, 07:41 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
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, 08:05 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
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, 11:02 AM   #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, 12:22 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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


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
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 02:28 PM
gcc not seeing libraries kuloch Linux - Software 1 08-30-2008 09:30 PM
skipping incompatible C libraries? rgmayoral Linux - Software 2 10-18-2007 06:29 AM
gcc-libraries yashwantpinge Programming 3 11-22-2005 12:17 AM
ld is "skipping incompatible" libraries The_Nerd Programming 3 09-13-2005 11:34 PM

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

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