LinuxQuestions.org
Help answer threads with 0 replies.
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 03-15-2010, 09:22 AM   #1
Mactub
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
how to make g++ behave like gcc in this case


Hi guys

I am compiling some c++ code trying to connect it with

libi2cbrdg.a

library where I have a lot of functions. So when I am doing this

gcc -g rand.c -lasound -li2cbrdg -o rand.o

I don't have missed functions instead I have some other C/C++ connected errors, when I am using g++ compiler like this

g++ -g rand.c -lasound -li2cbrdg -o rand.o

it does not see functions inside that library but everything fine with c++ code.

How can I make g++ behave like gcc in this case?
 
Old 03-15-2010, 03:39 PM   #2
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by Mactub View Post
I don't have missed functions instead I have some other C/C++ connected errors, when I am using g++ compiler like this

g++ -g rand.c -lasound -li2cbrdg -o rand.o

it does not see functions inside that library but everything fine with c++ code.

How can I make g++ behave like gcc in this case?
Hey,

it sounds like you hit a name mangling issue. When using g++, you call the C++ compiler (cc1cplus), while you call the C compiler (cc1) when using gcc. Though with the C++ compiler, if you call a function like

int func(int, long int)

from your code, the C++ compiler will name mangle the symbol and finally tries to call a function named

_Z4funcil

which the linker then couldn't find in the libraries, because the libraries only have the function symbol 'func'. Though, if you really have C++ code in your source file rand.c, you should first of all change the extension to .cpp or .cc to make it in general clearer that this is C++ code. Then you should check the prototypes of the library functions and surround them by a

extern "C" {

<function prototypes>
}

This instructs the C++ compiler to NOT name mangle the functions listed in the extern "C" block.

I hope this is what you're searching for, otherwise, please post your concrete error message with a small source code example ...

- Andi -
 
Old 03-17-2010, 02:19 AM   #3
Mactub
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ForzaItalia2006 View Post
Hey,

it sounds like you hit a name mangling issue. When using g++, you call the C++ compiler (cc1cplus), while you call the C compiler (cc1) when using gcc. Though with the C++ compiler, if you call a function like

int func(int, long int)

from your code, the C++ compiler will name mangle the symbol and finally tries to call a function named

_Z4funcil

which the linker then couldn't find in the libraries, because the libraries only have the function symbol 'func'. Though, if you really have C++ code in your source file rand.c, you should first of all change the extension to .cpp or .cc to make it in general clearer that this is C++ code. Then you should check the prototypes of the library functions and surround them by a

extern "C" {

<function prototypes>
}

This instructs the C++ compiler to NOT name mangle the functions listed in the extern "C" block.

I hope this is what you're searching for, otherwise, please post your concrete error message with a small source code example ...

- Andi -
Cool Thanks, problem solved
 
  


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
How to make mediatomb behave similar to ftp/nfs/sshfs server? 10110111 Linux - Software 2 08-12-2010 06:00 AM
how do i make firefox behave? smeezekitty Linux - Software 8 10-10-2009 12:43 PM
Make mysql order by to behave differently raven Linux - Server 2 11-30-2007 03:33 AM
How can I make Murasaki behave like Hotplug for an unkown device? milewis1 Linux - Hardware 1 05-25-2005 12:57 PM
Can I make 'cd' behave this way? exitsfunnel Linux - Software 4 08-30-2003 05:57 PM

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

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