简体   繁体   English

C ++无法使用cygwin链接到Boost库

[英]C++ cannot link to boost library using cygwin

I am wondering why I cannot link boost library. 我想知道为什么我不能链接Boost库。

I use gcc from Cygwin on windows 64 bit platform. 我在Windows 64位平台上使用来自Cygwin的gcc。

Makefile 生成文件

all:
    g++ -c main.cpp -std=c++11 -I'../include'
    g++ main.o ../libs/libboost_filesystem-vc120-mt-1_60.lib

main.cpp main.cpp

#include <boost/filesystem.hpp>


int main()
{
    return 0;
}

Error: 错误:

D:\tmp\test\example>make
g++ -c main.cpp -std=c++11 -I'../include'
g++ main.o ../libs/libboost_filesystem-vc120-mt-1_60.lib
main.o:main.cpp:(.text+0x36): undefined reference to `boost::system::generic_category()'
main.o:main.cpp:(.text+0x36): relocation truncated to fit: R_X86_64_PC32 against
 undefined symbol `boost::system::generic_category()'
main.o:main.cpp:(.text+0x42): undefined reference to `boost::system::generic_category()'
main.o:main.cpp:(.text+0x42): relocation truncated to fit: R_X86_64_PC32 against
 undefined symbol `boost::system::generic_category()'
main.o:main.cpp:(.text+0x4e): undefined reference to `boost::system::system_category()'
main.o:main.cpp:(.text+0x4e): relocation truncated to fit: R_X86_64_PC32 against
 undefined symbol `boost::system::system_category()'
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 1

D:\tmp\test\example>

Boost library files: Boost库文件:

ls ../include/boost


accumulators
algorithm
align
align.hpp
aligned_storage.hpp
any.hpp
archive
array.hpp
asio
asio.hpp
assert.hpp
assign
assign.hpp
atomic
atomic.hpp
bimap
bimap.hpp
bind
bind.hpp
blank.hpp
blank_fwd.hpp
call_traits.hpp
cast.hpp
cerrno.hpp
checked_delete.hpp
chrono
chrono.hpp
circular_buffer
circular_buffer.hpp
circular_buffer_fwd.hpp
compatibility
compressed_pair.hpp
concept
concept_archetype.hpp
concept_check
concept_check.hpp
config
config.hpp
container
context
convert
convert.hpp
core
coroutine
coroutine2
crc.hpp
cregex.hpp
cstdfloat.hpp
cstdint.hpp
cstdlib.hpp
current_function.hpp
cxx11_char_types.hpp
date_time
date_time.hpp
detail
dynamic_bitset
dynamic_bitset.hpp
dynamic_bitset_fwd.hpp
enable_shared_from_this.hpp
endian
exception
exception_ptr.hpp
filesystem
filesystem.hpp
flyweight
flyweight.hpp
foreach.hpp
foreach_fwd.hpp
format
format.hpp
function
function.hpp
function_equal.hpp
function_output_iterator.hpp
function_types
functional
functional.hpp
fusion
generator_iterator.hpp
geometry
geometry.hpp
get_pointer.hpp
gil
graph
heap
icl
implicit_cast.hpp
indirect_reference.hpp
integer
integer.hpp
integer_fwd.hpp
integer_traits.hpp
interprocess
intrusive
intrusive_ptr.hpp
io
io_fwd.hpp
iostreams
is_placeholder.hpp
iterator
iterator.hpp
iterator_adaptors.hpp
lambda
last_value.hpp
lexical_cast
lexical_cast.hpp
limits.hpp
local_function
local_function.hpp
locale
locale.hpp
lockfree
log
logic
make_default.hpp
make_shared.hpp
make_unique.hpp
math
math_fwd.hpp
mem_fn.hpp
memory_order.hpp
move
mpi
mpi.hpp
mpl
msm
multi_array
multi_array.hpp
multi_index
multi_index_container.hpp
multi_index_container_fwd.hpp
multiprecision
next_prior.hpp
non_type.hpp
noncopyable.hpp
nondet_random.hpp
none.hpp
none_t.hpp
numeric
operators.hpp
optional
optional.hpp
parameter
parameter.hpp
pending
phoenix
phoenix.hpp
pointee.hpp
pointer_cast.hpp
pointer_to_other.hpp
polygon
polymorphic_cast.hpp
polymorphic_pointer_cast.hpp
pool
predef
predef.h
preprocessor
preprocessor.hpp
program_options
program_options.hpp
progress.hpp
property_map
property_tree
proto
ptr_container
python
python.hpp
random
random.hpp
range
range.hpp
ratio
ratio.hpp
rational.hpp
ref.hpp
regex
regex.h
regex.hpp
regex_fwd.hpp
scope_exit.hpp
scoped_array.hpp
scoped_ptr.hpp
serialization
shared_array.hpp
shared_container_iterator.hpp
shared_ptr.hpp
signal.hpp
signals
signals.hpp
signals2
signals2.hpp
smart_ptr
smart_ptr.hpp
sort
spirit
spirit.hpp
statechart
static_assert.hpp
swap.hpp
system
test
thread
thread.hpp
throw_exception.hpp
timer
timer.hpp
token_functions.hpp
token_iterator.hpp
tokenizer.hpp
tr1
tti
tuple
type.hpp
type_erasure
type_index
type_index.hpp
type_traits
type_traits.hpp
typeof
units
unordered
unordered_map.hpp
unordered_set.hpp
utility
utility.hpp
uuid
variant
variant.hpp
version.hpp
visit_each.hpp
vmd
wave
wave.hpp
weak_ptr.hpp
xpressive

Boost lib files: Boost lib文件:

ls ../libs

libboost_atomic-vc120-mt-1_60.lib
libboost_atomic-vc120-mt-gd-1_60.lib
libboost_chrono-vc120-mt-1_60.lib
libboost_chrono-vc120-mt-gd-1_60.lib
libboost_container-vc120-mt-1_60.lib
libboost_container-vc120-mt-gd-1_60.lib
libboost_context-vc120-mt-1_60.lib
libboost_context-vc120-mt-gd-1_60.lib
libboost_coroutine-vc120-mt-1_60.lib
libboost_coroutine-vc120-mt-gd-1_60.lib
libboost_date_time-vc120-mt-1_60.lib
libboost_date_time-vc120-mt-gd-1_60.lib
libboost_exception-vc120-mt-1_60.lib
libboost_exception-vc120-mt-gd-1_60.lib
libboost_filesystem-vc120-mt-1_60.lib
libboost_filesystem-vc120-mt-gd-1_60.lib
libboost_graph-vc120-mt-1_60.lib
libboost_graph-vc120-mt-gd-1_60.lib
libboost_iostreams-vc120-mt-1_60.lib
libboost_iostreams-vc120-mt-gd-1_60.lib
libboost_locale-vc120-mt-1_60.lib
libboost_locale-vc120-mt-gd-1_60.lib
libboost_log-vc120-mt-1_60.lib
libboost_log-vc120-mt-gd-1_60.lib
libboost_log_setup-vc120-mt-1_60.lib
libboost_log_setup-vc120-mt-gd-1_60.lib
libboost_math_c99-vc120-mt-1_60.lib
libboost_math_c99-vc120-mt-gd-1_60.lib
libboost_math_c99f-vc120-mt-1_60.lib
libboost_math_c99f-vc120-mt-gd-1_60.lib
libboost_math_c99l-vc120-mt-1_60.lib
libboost_math_c99l-vc120-mt-gd-1_60.lib
libboost_math_tr1-vc120-mt-1_60.lib
libboost_math_tr1-vc120-mt-gd-1_60.lib
libboost_math_tr1f-vc120-mt-1_60.lib
libboost_math_tr1f-vc120-mt-gd-1_60.lib
libboost_math_tr1l-vc120-mt-1_60.lib
libboost_math_tr1l-vc120-mt-gd-1_60.lib
libboost_prg_exec_monitor-vc120-mt-1_60.lib
libboost_prg_exec_monitor-vc120-mt-gd-1_60.lib
libboost_program_options-vc120-mt-1_60.lib
libboost_program_options-vc120-mt-gd-1_60.lib
libboost_python-vc120-mt-1_60.lib
libboost_python-vc120-mt-gd-1_60.lib
libboost_python3-vc120-mt-1_60.lib
libboost_python3-vc120-mt-gd-1_60.lib
libboost_random-vc120-mt-1_60.lib
libboost_random-vc120-mt-gd-1_60.lib
libboost_regex-vc120-mt-1_60.lib
libboost_regex-vc120-mt-gd-1_60.lib
libboost_serialization-vc120-mt-1_60.lib
libboost_serialization-vc120-mt-gd-1_60.lib
libboost_signals-vc120-mt-1_60.lib
libboost_signals-vc120-mt-gd-1_60.lib
libboost_system-vc120-mt-1_60.lib
libboost_system-vc120-mt-gd-1_60.lib
libboost_test_exec_monitor-vc120-mt-1_60.lib
libboost_test_exec_monitor-vc120-mt-gd-1_60.lib
libboost_thread-vc120-mt-1_60.lib
libboost_thread-vc120-mt-gd-1_60.lib
libboost_timer-vc120-mt-1_60.lib
libboost_timer-vc120-mt-gd-1_60.lib
libboost_type_erasure-vc120-mt-1_60.lib
libboost_type_erasure-vc120-mt-gd-1_60.lib
libboost_unit_test_framework-vc120-mt-1_60.lib
libboost_unit_test_framework-vc120-mt-gd-1_60.lib
libboost_wave-vc120-mt-1_60.lib
libboost_wave-vc120-mt-gd-1_60.lib
libboost_wserialization-vc120-mt-1_60.lib
libboost_wserialization-vc120-mt-gd-1_60.lib

Update 更新资料

I followed the comment from frymode and installed boost this way: 我遵循了frymode的评论,并以这种方式安装了boost:

./bootstrap.sh && ./b2 install

The folder C:\\cygwin\\usr\\local\\lib is empty. 文件夹C:\\cygwin\\usr\\local\\lib为空。 Boost is installed in C:\\Boost : Boost安装在C:\\Boost

...
common.copy C:\Boost\lib\libboost_wserialization-vc120-mt-gd-1_60.lib
bin.v2\libs\serialization\build\msvc-12.0\debug\link-static\threading-multi\libb
oost_wserialization-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:\Boost\lib\libboost_signals-vc120-mt-gd-1_60.lib
bin.v2\libs\signals\build\msvc-12.0\debug\link-static\threading-multi\libboost_s
ignals-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:\Boost\lib\libboost_timer-vc120-mt-gd-1_60.lib
bin.v2\libs\timer\build\msvc-12.0\debug\link-static\threading-multi\libboost_tim
er-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:\Boost\lib\libboost_prg_exec_monitor-vc120-mt-gd-1_60.lib
bin.v2\libs\test\build\msvc-12.0\debug\link-static\threading-multi\libboost_prg_
exec_monitor-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:\Boost\lib\libboost_test_exec_monitor-vc120-mt-gd-1_60.lib
bin.v2\libs\test\build\msvc-12.0\debug\link-static\threading-multi\libboost_test
_exec_monitor-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:\Boost\lib\libboost_unit_test_framework-vc120-mt-gd-1_60.lib
bin.v2\libs\test\build\msvc-12.0\debug\link-static\threading-multi\libboost_unit
_test_framework-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:\Boost\lib\libboost_type_erasure-vc120-mt-gd-1_60.lib
bin.v2\libs\type_erasure\build\msvc-12.0\debug\link-static\threading-multi\libbo
ost_type_erasure-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:\Boost\lib\libboost_wave-vc120-mt-gd-1_60.lib
bin.v2\libs\wave\build\msvc-12.0\debug\link-static\threading-multi\libboost_wave
-vc120-mt-gd-1_60.lib
        1 file(s) copied.
...updated 12016 targets...

Here is the output for linking: 这是链接的输出:

g++ -c main.cpp -std=c++11 -I'C:\Boost\include\boost-1_60\'
g++ main.o -L'C:\Boost\lib\libboost_filesystem-vc120-mt-1_60.lib'

main.o:main.cpp:(.text+0x36): undefined reference to `boost::system::generic_category()'
main.o:main.cpp:(.text+0x36): relocation truncated to fit: R_X86_64_PC32 against
 undefined symbol `boost::system::generic_category()'
main.o:main.cpp:(.text+0x42): undefined reference to `boost::system::generic_category()'
main.o:main.cpp:(.text+0x42): relocation truncated to fit: R_X86_64_PC32 against
 undefined symbol `boost::system::generic_category()'
main.o:main.cpp:(.text+0x4e): undefined reference to `boost::system::system_category()'
main.o:main.cpp:(.text+0x4e): relocation truncated to fit: R_X86_64_PC32 against
 undefined symbol `boost::system::system_category()'
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 1

As far as I know, Cygwin's gcc and g++ cannot link libraries that have been compiled with the Visual C++ toolchain, ie libraries ending with .lib and .dll . 据我所知,Cygwin的gccg++无法链接使用Visual C ++工具链编译的库,即以.lib.dll结尾的库。 You need to compile Boost with the Cygwin toolchain to get static .a or shared .so libraries. 您需要使用Cygwin工具链编译Boost以获取静态.a或共享的.so库。

Build static Boost libraries with Cygwin 使用Cygwin构建静态Boost库

  1. Download Boost 下载Boost
    • Any archive for Unix or Windows is ok, the content is the same. Unix或Windows的任何存档都可以,内容相同。
    • A prebuild Windows binary from SourceForge is also ok, it includes the full source code. 来自SourceForge的预构建Windows二进制文件也可以,它包含完整的源代码。
  2. Extract the archive or install the prebuild binary somewhere, eg C:\\boost_1_68_0 . 解压缩档案文件或将预构建二进制文件安装在某个位置,例如C:\\boost_1_68_0
  3. Open a Cygwin terminal and run the following commands: 打开一个Cygwin终端并运行以下命令:

     cd /cygdrive/c/boost_1_68_0/ ./bootstrap.sh # Builds the b2 tool known as Boost.Build. ./b2 --help # With b2 you can compile the Boost source code. ./b2 --build-dir=build --stagedir=lib64-gcc link=static stage # Watch the compilation and do not worry about the warnings. ls -1 lib64-gcc/lib/ libboost_atomic.a libboost_chrono.a libboost_container.a ... 

Compile and statically link your program 编译并静态链接程序

g++ -c main.cpp -I/cygdrive/c/boost_1_68_0
g++ main.o -L/cygdrive/c/boost_1_68_0/lib64-gcc/lib -lboost_filesystem -lboost_system

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM