建立和链接共享库 [英] building and linking a shared library
问题描述
我试图使用g ++在Windows cygwin平台上构建共享库,然后将其与另一个cpp文件链接:
i使用以下命令:
//生成对象文件
g ++ -g -c -Wall -fPIC beat11.cpp -o beat11.o
从目标文件生成库
g ++ -shared -Wl,-soname,libbeat.so.1 -o libbeat.so.1.0.1 beat11.o -lc
//将其与另一个cpp文件链接; -I选项引用库头文件
g ++ -L。 -1beat-1。 -o checkbeat checkbeat.cpp
链接时,出现以下错误:
/usr/lib/gcc/i686-pc-cygwin/4.5.3 /../../../../ i686-pc -cygwin / bin / ld:
找不到-llibbeat.so.1.0.1
collect2:ld返回1退出状态
库创建正好,但我只能找到libbeat.so.1.0.1,而不是libbeat.so或libbeat.so.1(或他们不是应该在那里?)
其他问题之一建议创建一个到libbeat.so.1.0.1的符号链接,但这太工作
当使用 -l< libname>
指定要链接的库时,链接器将首先搜索在搜索 lib< libname> .a
之前为 lib< libname> .so
在您的情况下,它不工作,因为库文件名不是 .so
p>
您可以创建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 </ code>参数)。另一种选择是指定不含
-l </ code>
g ++ -o checkbeat checkbeat.cpp -I。 -L。 libbeat.so.1.0.1
请注意,链接到的库应放在object / source文件使用其符号 - 否则链接器可能找不到符号。
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
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.
这篇关于建立和链接共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!