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]#