簡體   English   中英

C ++無法使用cygwin鏈接到Boost庫

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

我想知道為什么我不能鏈接Boost庫。

我在Windows 64位平台上使用來自Cygwin的gcc。

生成文件

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:\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庫文件:

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文件:

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:\\cygwin\\usr\\local\\lib為空。 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...

這是鏈接的輸出:

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

據我所知,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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM