LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-09-2012, 04:57 AM   #1
toredo
Member
 
Registered: Feb 2009
Posts: 83

Rep: Reputation: 25
Using a c Shared Object in C++


Hello,

I'm just writing a bit c and c++, now a have a problem. I have a shared-object, compiled with gcc, and a program, compiled with g++. The program should call a function of the library.

But the linking doesn't work?

An example:
lib.c
Code:
#include "lib.h"

int kalk(int n1, int n2) {
    return n1 + n2;
}
lib.h
Code:
#ifndef __MY_LIB
#define __MY_LIB

int kalk(int n1, int n2);

#endif
test.cpp
Code:
#include <iostream>
#include "lib.h"

using namespace std;

int main(void) {
    cout << kalk(1, 2);
    return 0;
}
Then i compile the library with the following command:
Code:
gcc lib.c -shared -olib.so
And the program itself with:
Code:
g++ test.cpp lib.so -otest
but this compilation failes:
Code:
/tmp/ccoYXf6C.o: In function `main':
test.cpp:(.text+0x19): undefined reference to `kalk(int, int)'
collect2: ld returned 1 exit status
If i compile the library with g++, then it works:
Code:
g++ lib.c -shared -olib.so

it should be possible to use c-librarys in c++, shouldn't it be? I don't know. I think i did something wrong.

Thanks for your help

best regards
toredo
 
Old 05-09-2012, 05:16 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Have you tried telling the C++ compiler the header file is C and not C++? i.e., write lib.h as
Code:
#ifndef __MY_LIB
#define __MY_LIB
#ifdef __cplusplus
extern "C"
{
#endif

int kalk(int n1, int n2);

#ifdef __cplusplus
}
#endif
#endif
 
Old 05-09-2012, 05:46 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
There is plenty on web search about this.

You know that C++ mangles function names, so basically it is inherently incompatible with .so libs
except by methods like the previous poster said.
(the caller doesn't know what the mangled name is in the callee)

Specifying as extern "C" means the name isn't mangled.

If you want to act on objects it will be quite a lot of hassle.
You need to keep it quite simple is my advice.

if you use nm on the object files you'll see the what the names mangle to.
nm is a useful tool if you don't know it well worth remembering.

Last edited by bigearsbilly; 05-09-2012 at 05:47 AM.
 
1 members found this post helpful.
Old 05-10-2012, 01:19 AM   #4
toredo
Member
 
Registered: Feb 2009
Posts: 83

Original Poster
Rep: Reputation: 25
Thank you for your help and the explanation.

It just worked and nm seems to be very usefull.

Thanks

Best regards
toredo
 
Old 05-10-2012, 06:21 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by bigearsbilly View Post
There is plenty on web search about this.
Although you are absolutely correct about C++ mangling function names, C++ and C are two completely different programming languages. I much prefer my explanation better.

There is, obviously, common ancestry between C++ and C, and you can compile most C code using a C++ compiler and get a working C++ binary (it will differ a lot from when compiled using a C compiler, most importantly it will require the C++ runtime), but that does not make them the same language, or even variations of the same language. It just makes most C compatible with C++.

I realise most programmers find that distinction unimportant. Working with embedded devices (or something like the Linux kernel) where the C++ runtime is unavailalbe, the distinction is very real and quite important.

I wish people (not you, Bigearsbilly, specifically; I do mean in general) would stop mixing the two languages; it's just confusing. Perhaps I should start intentionally conflating Perl with Scheme in retaliation? Just kidding.
 
Old 05-10-2012, 08:53 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by Nominal Animal View Post
Although you are absolutely correct about C++ mangling function names, C++ and C are two completely different programming languages.
No I was referring to C++ name mangling and shared objects in particular.
It's a common problem.

I wasn't comparing C and C++.

But i forgive you
 
  


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
error while loading shared libraries: libgda-4.0.so.4: cannot open shared object file mahesh1234 Linux - Newbie 2 10-22-2013 11:06 PM
honeyd: error while loading shared libraries: libdnet.1: cannot open shared object fi secbuddy Linux - Software 2 12-24-2011 02:01 PM
data sharing between c++ shared object and Fortran shared object Madhubala Debian 2 08-19-2011 02:22 AM
error while loading shared libraries: libtermcap.so.2: cannot open shared object file astroboy2000ir Linux - Software 3 12-07-2010 11:16 PM
error while loading shared libraries: libgvc.so.3: cannot open shared object file coolrock Slackware 6 01-17-2007 05:10 PM

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

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