Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

g++ - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...