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

C++ cannot link to boost library using cygwin

本文关键字:链接 boost cygwin C++      更新时间:2023-10-16

我想知道为什么我不能链接boost库。

我在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

#include <boost/filesystem.hpp>

int main()
{
    return 0;
}

错误:

D:tmptestexample>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:tmptestexample>

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

提升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

更新

我遵循了frymode的评论,并以这种方式安装了boost:

./bootstrap.sh && ./b2 install

文件夹C:cygwinusrlocallib为空。Boost安装在C:Boost:中

...
common.copy C:Boostliblibboost_wserialization-vc120-mt-gd-1_60.lib
bin.v2libsserializationbuildmsvc-12.0debuglink-staticthreading-multilibb
oost_wserialization-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:Boostliblibboost_signals-vc120-mt-gd-1_60.lib
bin.v2libssignalsbuildmsvc-12.0debuglink-staticthreading-multilibboost_s
ignals-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:Boostliblibboost_timer-vc120-mt-gd-1_60.lib
bin.v2libstimerbuildmsvc-12.0debuglink-staticthreading-multilibboost_tim
er-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:Boostliblibboost_prg_exec_monitor-vc120-mt-gd-1_60.lib
bin.v2libstestbuildmsvc-12.0debuglink-staticthreading-multilibboost_prg_
exec_monitor-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:Boostliblibboost_test_exec_monitor-vc120-mt-gd-1_60.lib
bin.v2libstestbuildmsvc-12.0debuglink-staticthreading-multilibboost_test
_exec_monitor-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:Boostliblibboost_unit_test_framework-vc120-mt-gd-1_60.lib
bin.v2libstestbuildmsvc-12.0debuglink-staticthreading-multilibboost_unit
_test_framework-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:Boostliblibboost_type_erasure-vc120-mt-gd-1_60.lib
bin.v2libstype_erasurebuildmsvc-12.0debuglink-staticthreading-multilibbo
ost_type_erasure-vc120-mt-gd-1_60.lib
        1 file(s) copied.
common.copy C:Boostliblibboost_wave-vc120-mt-gd-1_60.lib
bin.v2libswavebuildmsvc-12.0debuglink-staticthreading-multilibboost_wave
-vc120-mt-gd-1_60.lib
        1 file(s) copied.
...updated 12016 targets...

以下是链接的输出:

g++ -c main.cpp -std=c++11 -I'C:Boostincludeboost-1_60'
g++ main.o -L'C:Boostliblibboost_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

据我所知,Cygwin的gccg++无法链接已使用Visual C++工具链编译的库,即以.lib.dll结尾的库。您需要使用Cygwin工具链编译Boost以获得静态.a或共享.so库。

使用Cygwin构建静态Boost库

  1. 下载Boost
    • Unix或Windows的任何存档都可以,内容都是一样的
    • 从SourceForge预构建Windows二进制文件也可以,它包括完整的源代码
  2. 提取归档文件或将预构建二进制文件安装到某个位置,例如C:boost_1_68_0
  3. 打开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
    ...
    

编译并静态链接程序

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