LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-27-2009, 10:06 AM   #1
morty346
Member
 
Registered: Feb 2009
Posts: 52

Rep: Reputation: 15
Linux + Code Blocks + Shared Library + my Implementation = Not Working!


Hello
I have been trying to create shared objects since about midnight, and am at the point where I need some help.

I have a .c and .h file that I am creating a .so for, I seem to be able to compile the files fine, but the second I go and try to link them in to a .c console app I get unrefereced errors.

I started by using just a text editor and the terminal, and go the creation of the .so down with a simple hello world app, and even linked it in nicely.

But now I am trying to do it with my own program and seem not to be able to get it down.

Currently I am using code blocks, I create a new project of type shared library, i use the gcc compiler, and i make a c file that looks like this
Code:
int SampleAddInt(int i1, int i2)
{
    return i1 + i2;
}
An .h file that looks like this:
Code:
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
int SampleAddInt(int i1, int i2);
#endif
which when compiled gives me a file named libtest.so

From here I create a new project of type console
I create a main.cpp file where I:

Code:
#include <iostream>
#include "main.h" //.h file shown above
using namespace std;
int main()
{
    cout << "Hello world!" << endl;
    int returnval = SampleAddInt(4, 10); //function in so
    return 0;
}
and I get the error
Code:
||warning: ..liblibtest.so, needed by ..libtest1231.so, not found (try using -rpath or -rpath-link)|
obj/Debug/main.o||In function `main':|
/main.cpp|9|undefined reference to `SampleAddInt(int, int)'|
||=== Build finished: 1 errors, 3 warnings ===|
I right click on the project, go to build properties, linker, and add a relative path to the .so file



I tried this on my own in the terminal as well and seem to have no luck, when using the terminal I am following a few different tutorials
the one I am specificly using is
http://www.ibm.com/developerworks/library/l-shobj/
Code:
$ldconfig -v -n .
$ ln -sf libprint.so.1 libprint.so
$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
$ gcc -o client client.c -L. -lprint
Now for this code, I place the so in the same directory, it worked just fine for there example, but not for the code above



What is it that I am doing wrong, why can I only get a simple hello world to work, but not this simple .so with the one function to work?

Thank you



Code:
[root@GMFedora Test1]# gcc -fPIC -c main.c
[root@GMFedora Test1]# ld -shared -soname libtest.so.1 -o libtest.so.1.0 -lc main.o
[root@GMFedora Test1]# ldconfig -v -n .
.:
	liblibtest.so -> liblibtest.so
	libtest.so.1 -> libtest.so.1.0 (changed)
[root@GMFedora Test1]# ln -sf libtest.so.1 libtest.so
[root@GMFedora Test1]# export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
[root@GMFedora Test1]# gcc -o client client.c -L. -lprint
/usr/bin/ld: cannot find -lprint
collect2: ld returned 1 exit status
[root@GMFedora Test1]# gcc -o client client.c -L.
/tmp/ccFvAAvm.o: In function `main':
client.c:(.text+0x21): undefined reference to `SampleAddInt'
collect2: ld returned 1 exit status
[root@GMFedora Test1]#

Last edited by morty346; 04-27-2009 at 10:24 AM.
 
Old 04-27-2009, 04:21 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You're mixing C and C++ code. This will not work that easily. C++ mangles function names, so probably it's searching for something completely different than SampleAddInt which is in the library. That's why you get an error. You need to modify the header file:
Code:
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif 
int SampleAddInt(int i1, int i2);
#ifdef __cplusplus
}
#endif 
#endif
Note also that you need to compile the program (not the library, in this case it's your choice) using g++, not gcc!
 
  


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
Converting Windows DLL code to Linux shared library DavidW567 Programming 7 07-27-2010 08:15 AM
gcc link shared library against another shared library qcp Linux - Newbie 1 07-25-2008 11:15 AM
What is the correct way to write a working shared library in C++? golden97 Programming 1 12-14-2007 04:01 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM

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

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