C++ cannot link to boost library using cygwin

2019-09-07 05:21发布

问题:

I am wondering why I cannot link boost library.

I use gcc from Cygwin on windows 64 bit platform.

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;
}

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:

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:

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:

./bootstrap.sh && ./b2 install

The folder C:\cygwin\usr\local\lib is empty. Boost is installed in 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

回答1:

As far as I know, Cygwin's gcc and g++ cannot link libraries that have been compiled with the Visual C++ toolchain, i.e. libraries ending with .lib and .dll. You need to compile Boost with the Cygwin toolchain to get static .a or shared .so libraries.

Build static Boost libraries with Cygwin

  1. Download Boost
    • Any archive for Unix or Windows is ok, the content is the same.
    • A prebuild Windows binary from SourceForge is also ok, it includes the full source code.
  2. Extract the archive or install the prebuild binary somewhere, e.g. C:\boost_1_68_0.
  3. Open a Cygwin terminal and run the following commands:

    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