Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

building and linking a shared library

im trying to build a shared library on a windows cygwin platform using g++, and later link it with another cpp file: i use the following commands:

// generate object file

g++ -g -c -Wall -fPIC beat11.cpp -o beat11.o

// to generate library from the object file

g++ -shared -Wl,-soname,libbeat.so.1 -o libbeat.so.1.0.1 beat11.o -lc

// to link it with another cpp file; -I option to refer to the library header file

g++ -L. -lbeat -I . -o checkbeat checkbeat.cpp

while linking, the following error crops up:

/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld: 
     cannot find -llibbeat.so.1.0.1

collect2: ld returned 1 exit status

the library gets created just fine, but i can only find libbeat.so.1.0.1, not libbeat.so or libbeat.so.1(or are they not supposed to be there?)

one of the other questions suggests creating a symlink to libbeat.so.1.0.1, but that too didnt work

like image 570
jayant Avatar asked May 25 '12 05:05

jayant


1 Answers

When using -l<libname> to specify library to link, the linker will first search for lib<libname>.so before searching for lib<libname>.a.

In your case it doesn't work, because the library filename is not with .so suffix.

You may create simlink

libbeat.so -> libbeat.so.1.0.1

or

libbeat.so -> libbeat.so.1
libbeat.so.1 -> libbeat.so.1.0.1

You can also use -l:libbeat.so.1.0.1 (if your linker supports it, check in man ld description of -l parameter). Another option is to specify the library without -l

g++ -o checkbeat checkbeat.cpp -I . -L. libbeat.so.1.0.1

Note that the library you link to should be put after object/source file using its symbols - otherwise the linker may not find the symbols.

like image 171
Dmitry Yudakov Avatar answered Oct 16 '22 09:10

Dmitry Yudakov