microsoft / vcpkg

C++ Library Manager for Windows, Linux, and MacOS
MIT License
22.74k stars 6.29k forks source link

build failure when linking to libarchive on x64 linux (ubuntu 18.04). Same build works on windows 10 and macos. #8839

Closed abhishekmishra closed 4 years ago

abhishekmishra commented 4 years ago

Describe the bug I have a cmake build, which uses vcpkg toolchain and links an executable to the libarchive library as one of it's dependencies. The build works fine on Windows x64 and Macos. However when I try the same build on an x64 linux (namely Ubuntu 18.04), I get linker errors. This is my first time using vcpkg so I might be doing something incorrect here.

Here's the relevant section of my cmake build:

# To find and use libarchive, as the vcpkg build does not have cmake config
find_path(LIBARCHIVE_INCLUDE_DIR archive.h)
find_library(LIBARCHIVE_LIBRARY archive)
include_directories(${LIBARCHIVE_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC ${LIBARCHIVE_LIBRARY})

Environment

libarchive[bzip2]:x64-linux BZip2 support

libarchive[libxml2]:x64-linux Libxml2 support

libarchive[lz4]:x64-linux LZ4 support

libarchive[lzma]:x64-linux LZMA support

libarchive[lzo]:x64-linux LZO support

libarchive[openssl]:x64-linux OpenSSL support

liblzma:x64-linux 5.2.4-2 Compression library with an API similar to that ...

libxml2:x64-linux 2.9.9-4 Libxml2 is the XML C parser and toolkit develope...

lz4:x64-linux 1.9.2 Lossless compression algorithm, providing compre...

lzo:x64-linux 2.10-4 Lossless data compression library

zlib:x64-linux 1.2.11-5 A compression library

To Reproduce Steps to reproduce the behavior:

  1. Create executable linking to libarchive from vcpkg
  2. Here is the build error

/root/vcpkg/installed/x64-linux/debug/lib/libarchive.a(archive_write_add_filter_gzip.c.o): In function archive_compressor_gzip_open': /root/vcpkg/buildtrees/libarchive/src/fa3b23efe3-1e0b785ee8/libarchive/archive_write_add_filter_gzip.c:242: undefined reference todeflateInit2_' /root/vcpkg/installed/x64-linux/debug/lib/libarchive.a(archive_write_add_filter_gzip.c.o): In function archive_compressor_gzip_close': /root/vcpkg/buildtrees/libarchive/src/fa3b23efe3-1e0b785ee8/libarchive/archive_write_add_filter_gzip.c:333: undefined reference todeflateEnd' /root/vcpkg/installed/x64-linux/debug/lib/libarchive.a(archive_write_add_filter_gzip.c.o): In function drive_compressor': /root/vcpkg/buildtrees/libarchive/src/fa3b23efe3-1e0b785ee8/libarchive/archive_write_add_filter_gzip.c:374: undefined reference todeflate' collect2: error: ld returned 1 exit status

Expected behavior The build should succeed.

abhishekmishra commented 4 years ago

I have created a repo with test code to help reproduce this error -> https://github.com/abhishekmishra/vcpkg_libarchive_issue_8839. It has a cmake build which works on windows+vcpkg and linux+pkgconfig but fails with linux+vcpkg. Instructions to reproduce are in the README.

LilyWangL commented 4 years ago

Hi @abhishekmishra, function crc32 and deflateInit2 need link to zlib. You need add find_package(ZLIB REQUIRED) to CMakeLists. Due to libarchive depends on zlib, zlib should be placed behind libarchive in target_link_libraries. Like this:

find_package(ZLIB REQUIRED)
find_path(LIBARCHIVE_INCLUDE_DIR archive.h)
find_library(LIBARCHIVE_LIBRARY archive)
include_directories(${LIBARCHIVE_INCLUDE_DIR})
target_link_libraries(vcpkg_libarchive_issue_8839 PRIVATE ${LIBARCHIVE_LIBRARY} ZLIB::ZLIB)
abhishekmishra commented 4 years ago

Hi @abhishekmishra, function crc32 and deflateInit2 need link to zlib. You need add find_package(ZLIB REQUIRED) to CMakeLists. Due to libarchive depends on zlib, zlib should be placed behind libarchive in target_link_libraries. Like this:

find_package(ZLIB REQUIRED)
find_path(LIBARCHIVE_INCLUDE_DIR archive.h)
find_library(LIBARCHIVE_LIBRARY archive)
include_directories(${LIBARCHIVE_INCLUDE_DIR})
target_link_libraries(vcpkg_libarchive_issue_8839 PRIVATE ${LIBARCHIVE_LIBRARY} ZLIB::ZLIB)

Thanks a lot, this worked for me.