GCC dynamic link problem

I am new to linux compiling with dynamic link library. I am getting segmentationfault error.

I have two files

ctest1.c

void ctest1(int *i)
{ 
   *i =10;
}

      

ctest2.c

void ctest2(int *i)
{ 
   *i =20;
}

      

I have compiled both files into a shared library called libtest.so using the following command

  gcc -shared -W1,-soname,libtest.so.1 -o libtest.so.1.0.1 ctest1.o ctest2.o -lc

      

And I wrote another prog.c program that uses the functions exported by this library

prog.c

#include <stdio.h>

void (*ctest1)(int*);
void (ctest2)(int*);


int main()
{
  int a;

  ctest1(&a);

  printf("%d",a);

  return 0;

}

      

And when I created the executable with the following command

gcc -Wall prog.c -L. -o prog

But when I run the generated executable, I get the SegmentationFault error.

When I checked the prog header using ldd it shows

linux-vdso.so.1 => (0x00007f99dff000) libc.so.6 => /lib64/libc.so.6 (0x0007feeaa8c1000) / lib 64 / ld-linux-x86-64.so.2 (0x00007feeaac1c000)

Can anyone tell me what the problem is?

+2


a source to share


3 answers


You don't call ctest1.c or ctest2.c. Instead, you create pointers to the ctest1 and ctest2 functions in prog.c, which you don't initialize, so you throw a segmentation fault when you try to call them.

You need to declare your functions so that prog.c can see them and then link prog.c with the libraries (perhaps using the -l option to gcc).

#include <stdio.h>

extern void ctest1(int*);
extern void ctest2(int*);


int main()
{
  int a;

  ctest1(&a);

  printf("%d",a);

  return 0;

}

      



And something like:

gcc -Wall -L. -ltest prog.c -o prog

      

+6


a source


Try this after using the information WhirlWind gave you (lines starting with "#" are comments, you don't need to enter them):

# Ensure that any shared objects you use are available in the current directory.
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

# Compile the library with a real name of "libctest.so.1.0.1"
# and a soname of "libctest.so.1".
gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0.1 ctest1.o ctest2.o

# Create a symbolic link with soname as the name that points to the library.
# (libctest.so.1 -> libctest.so.1.0.1)
/sbin/ldconfig -v -n .

# Create a symbolic link using the "linker name" that points to the newly
# created library.
ln -sf libctest.so.1 libctest.so

# Compile your program.
gcc -Wall -L. prog.c -o prog -l ctest

# Run your program (it won't work without setting LD_LIBRARY_PATH because
# it won't be able to find your library).
./prog

      

This worked for me. It looks like a lot of work, but after a few trial mistakes I think it becomes almost a chore.



You can find more information at http://www.ibm.com/developerworks/library/l-shobj/ . :)

Edit: I almost forgot to mention that it seems like many tutorials suggest using the -fPIC option to generate position-independent code (don't confuse it with -fpic, as it might make your resulting library less portable). It doesn't hurt to have it, but I've omitted it from the lines above for simplicity.

+1


a source


.so libraries on linux are dynamically linked. You will need to open the .so file with dlopen () in prog.c, find the symbols, and then call ctest1 () and ctest2 () using function pointers.

-4


a source







All Articles