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:
我正在尝试使用g++在windows cygwin平台上构建一个共享库,然后将它与另一个cpp文件链接:我使用以下命令:
// 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?)
这个库创建得很好,但是我只能找到libbeat.so.1.0.1,而不是libbeat。所以还是libbeat.so。1(或者他们不应该在那里?)
one of the other questions suggests creating a symlink to libbeat.so.1.0.1, but that too didnt work
还有一个问题建议创建一个到libbeat.so.1.0.1的符号链接,但这也行不通
1 个解决方案
#1
16
When using -l<libname>
to specify library to link, the linker will first search for lib<libname>.so
before searching for lib<libname>.a
.
当使用-l
In your case it doesn't work, because the library filename is not with .so
suffix.
在您的例子中,它不起作用,因为库文件名没有.so后缀。
You may create simlink
你可以创建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
您还可以使用-l:libbeat.so.1.0.1(如果您的链接器支持它,请检查-l参数的man ld描述)。另一个选项是在没有-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.
注意,链接到的库应该使用其符号放在对象/源文件之后——否则链接器可能找不到这些符号。
#1
16
When using -l<libname>
to specify library to link, the linker will first search for lib<libname>.so
before searching for lib<libname>.a
.
当使用-l
In your case it doesn't work, because the library filename is not with .so
suffix.
在您的例子中,它不起作用,因为库文件名没有.so后缀。
You may create simlink
你可以创建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
您还可以使用-l:libbeat.so.1.0.1(如果您的链接器支持它,请检查-l参数的man ld描述)。另一个选项是在没有-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.
注意,链接到的库应该使用其符号放在对象/源文件之后——否则链接器可能找不到这些符号。