20

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

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
jayant
  • 473
  • 1
  • 6
  • 12

1 Answers1

23

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.

Dmitry Yudakov
  • 15,364
  • 4
  • 49
  • 53
  • I had the same problem and your comment using -l:libname.so worked. However, I don't really understand why it doesn't work with -L -lname as you said that the linker should search for lib.so as well. it was also my understanding that it should work but at least with cygwin it doesn't seem to be the case. Havent't tried with other compilers. – Devolus Apr 23 '13 at 06:59
  • The answer here helped me: http://stackoverflow.com/questions/16154130/cygwin-g-linker-doesnt-find-shared-library – solstice333 Feb 11 '16 at 01:57