构建和链接共享库 [英] building and linking a shared library
问题描述
我正在尝试使用 g++ 在 windows cygwin 平台上构建共享库,然后将其与另一个 cpp 文件链接:我使用以下命令:
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
链接时出现以下错误:
/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
图书馆创建得很好,但我只能找到 libbeat.so.1.0.1,而不是 libbeat.so 或 libbeat.so.1(或者它们不应该在那里?)
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?)
其他问题之一建议创建指向 libbeat.so.1.0.1 的符号链接,但这也不起作用
one of the other questions suggests creating a symlink to libbeat.so.1.0.1, but that too didnt work
推荐答案
当使用-l
指定要链接的库时,链接器会先搜索lib
,然后再搜索 lib
.
When using -l<libname>
to specify library to link, the linker will first search for lib<libname>.so
before searching for lib<libname>.a
.
在你的情况下它不起作用,因为库文件名没有 .so
后缀.
In your case it doesn't work, because the library filename is not with .so
suffix.
你可以创建simlink
You may create simlink
libbeat.so -> libbeat.so.1.0.1
或
libbeat.so -> libbeat.so.1
libbeat.so.1 -> libbeat.so.1.0.1
您也可以使用 -l:libbeat.so.1.0.1
(如果您的链接器支持它,请查看 man ld
描述 -l
参数).另一种选择是指定不带 -l
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.
这篇关于构建和链接共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!