building and linking a shared library

蹲街弑〆低调 提交于 2019-11-26 22:22:26

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/10749058/building-and-linking-a-shared-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!