0

I think I'm missing something very simple but this is the setup I have:

I'm using conan to install zstandard:

[requires]
...
zstd/1.5.1

with following config:

[settings]
os=Linux
arch=x86_64
build_type=Release
compiler=clang
compiler.version=12
compiler.libcxx=libstdc++

[env]
CC=/usr/bin/clang-12
CXX=/usr/bin/clang++-12

My CMakeLists.txt is as follows:

set(LIBRARY_ZSTDSTREAM_SOURCES
    zstdstream.cpp
)

set(LIBRARY_ZSTDSTREAM_HEADERS
    circularbuffer.h
    zstdstream.h
)

add_library(${LIBRARY_ZSTDSTREAM_NAME} STATIC
    ${LIBRARY_ZSTDSTREAM_SOURCES}
    ${LIBRARY_ZSTDSTREAM_HEADERS})

target_link_libraries(${LIBRARY_ZSTDSTREAM_NAME} PUBLIC
    ${CONAN_ZSTD}
    ${LIBRARY_FORMAT_NAME}
)

target_include_directories(${LIBRARY_ZSTDSTREAM_NAME} PUBLIC
    "./"
    "${CMAKE_BINARY_DIR}/configured_files/include"
)

The error message I get is the following:

zstdstream.cpp:(.text+0x3b): undefined reference to `ZSTD_getErrorName'
/usr/bin/ld: ../../lib/libzstdstream.a(zstdstream.cpp.o): in function `common::zstdstream::cstream::cstream()':
zstdstream.cpp:(.text+0xc5): undefined reference to `ZSTD_createCStream'
/usr/bin/ld: ../../lib/libzstdstream.a(zstdstream.cpp.o): in function `common::zstdstream::cstream::~cstream()':
zstdstream.cpp:(.text+0xd8): undefined reference to `ZSTD_freeCStream'
/usr/bin/ld: zstdstream.cpp:(.text+0xe3): undefined reference to `ZSTD_isError'
/usr/bin/ld: ../../lib/libzstdstream.a(zstdstream.cpp.o): in function `common::zstdstream::cstream::init(int)':
zstdstream.cpp:(.text+0x148): undefined reference to `ZSTD_initCStream'
/usr/bin/ld: zstdstream.cpp:(.text+0x153): undefined reference to `ZSTD_isError'

I checked the linker by setting VERBOSE=1 running makefile:

-Wl,-rpath,/home/worker/.conan/data/zstd/1.5.1/_/_/package/4d1e52cb9a38d07b5e682edec92bb71d7afcd534/lib:
-L/home/worker/.conan/data/zstd/1.5.1/_/_/package/4d1e52cb9a38d07b5e682edec92bb71d7afcd534/lib

so zstd library is there and linked properly.

I am so confused to what is going on..

MoneyBall
  • 2,343
  • 5
  • 26
  • 59
  • There are two command and major options when it comets to linking: First it's `-L` which adds a path that the linker will use to search for libraries. And then there's `-l` (lower-case L) which tells the linker to actually link with a library. I'll guess you're missing the `-l` option. – Some programmer dude Oct 26 '22 at 09:18
  • which Conan generator are you using? Are you using which build system? Which OS? please, add more details, it's hard to follow your problem with few information. – uilianries Oct 26 '22 at 09:59
  • Both the `-Wl,-rpath` and the `-L` options are needed, but neither of them are actually linking with the library. All those two options are doing is set paths for the linker (`-L` sets the build-time path, and `-Wl,-rpath` sets the run-time path). To actually link with a library you need the `-l` option, as in `-lzstd`. – Some programmer dude Oct 27 '22 at 06:19
  • I second the request to specify which generator is being used here. In the meantime, try [`zstd::libzstd_shared`](https://conan.io/center/zstd?tab=recipe) (or _static) instead of `${CONAN_ZSTD}`. – skink Oct 28 '22 at 06:47

2 Answers2

1

Basic example with CMakeToolchain & CMakeDeps generators:

conanfile.txt:

[requires]
zstd/1.5.1
[generators]
CMakeToolchain
CMakeDeps

CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(foo)

find_package(zstd REQUIRED)

add_library(bar ...)
target_link_libraries(bar PRIVATE $<IF:$<TARGET_EXISTS:zstd::libzstd_shared>,zstd::libzstd_shared,zstd::libzstd_static>)

install dependencies & build:

mkdir build && cd build
conan install .. -s build_type=Release -b missing
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build . --config Release
SpacePotatoes
  • 659
  • 5
  • 11
0

I could not get conan to work. It is possible that ${CONAN_ZSTD} is not the right variable name, and I could not find the correct variable name.

Instead, I used CMake's fetchContent, and it worked for me:

include(FetchContent)

FetchContent_Declare(zstd
  GIT_REPOSITORY    "https://github.com/facebook/zstd"
  GIT_TAG           "dev"
)
FetchContent_MakeAvailable(
    zstd
)

MoneyBall
  • 2,343
  • 5
  • 26
  • 59