Flatbuffers C++ cmake linking library not found

Viewed 167

I am linking against the flatbuffers library in my c++ project with cmake. My CMakeLists.txt looks like:

set(FLATBUFFERS_SRC_DIR /root/src/git/flatbuffers)
set(FLATBUFFERS_BUILD_TESTS "Off")

add_subdirectory(${FLATBUFFERS_SRC_DIR}
        ${CMAKE_CURRENT_BINARY_DIR}/flatbuffers-build
        EXCLUDE_FROM_ALL)

  target_link_libraries(MyApp
          flatbuffers
          )

I am doing it by the book. https://google.github.io/flatbuffers/flatbuffers_guide_building.html

This worked fine on centos7 but on 8 I get an error:

Linking CXX static library libflatbuffers.a
Error running link command: No such file or directory

Im using this in a podman container:

FROM centos:8

USER root

RUN dnf -y --disablerepo '*' --enablerepo=extras swap centos-linux-repos centos-stream-repos
RUN dnf -y distro-sync

RUN yum update -y

# git and python3 for cmake
RUN yum install -y \
    git  \
    python3 \
    wget

RUN yum groupinstall -y "Development Tools"

# Install
RUN yum install -y gcc-toolset-11
RUN echo "source /opt/rh/gcc-toolset-11/enable" >> /etc/bashrc

## Install cmake
RUN yum install -y openssl-devel
RUN wget https://cmake.org/files/v3.24/cmake-3.24.0.tar.gz
RUN tar zxvf cmake-3.* && \
    cd cmake-3.24.0 && \
    ./bootstrap --prefix=/usr/local && \
    make -j$(nproc) && \
    make install

WORKDIR /app

With build command:

cd ~/src
podman run -it  \
 -e CC=/opt/rh/gcc-toolset-11/root/usr/bin/cc \
 -e CXX=/opt/rh/gcc-toolset-11/root/usr/bin/g++ \
 -v $(pwd)/myapp/MyApp:/app \
 -v $(pwd)/git/flatbuffers:/root/src/git/flatbuffers \
  myapp /bin/bash -c "cmake /app; cmake --build /app"

After running make VERBOSE=1 I see some output that indicates cmake is using devtoolset-7 when it should be using 11. Will try to debug that further.

[ 14%] Linking CXX static library libflatbuffers.a

cd /app/MyApp/flatbuffers-build && /usr/local/bin/cmake -P CMakeFiles/flatbuffers.dir/cmake_clean_target.cmake

cd /app/MyApp/flatbuffers-build && /usr/local/bin/cmake -E cmake_link_script CMakeFiles/flatbuffers.dir/link.txt --verbose=1

/opt/rh/devtoolset-7/root/usr/bin/ar qc libflatbuffers.a CMakeFiles/flatbuffers.dir/src/idl_parser.cpp.o CMakeFiles/flatbuffers.dir/src/idl_gen_text.cpp.o CMakeFiles/flatbuffers.dir/src/reflection.cpp.o CMakeFiles/flatbuffers.dir/src/util.cpp.o
Error running link command: No such file or directory

make[2]: *** [MyApp/flatbuffers-build/CMakeFiles/flatbuffers.dir/build.make:146: MyApp/flatbuffers-build/libflatbuffers.a] Error 2

Strange that the directory doesn't exist: ls: cannot access '/opt/rh/devtoolset-7': No such file or directory

Im not sure if this is related to the main problem or not.

1 Answers

From the description you mention in question and error snippet provided, it seems issue is, you are using dirty build directory. In other word you are using same build directory for centos7 and centos8, and that is on your host os. Changing your build command as below will definitely helps. But before that make sure your should code directory on host os is clean(i.e. no file generated by cmake should be present)

For centos 7

/bin/bash -c "cmake -S /app -B /app/centos7build/; cmake --build /app/centos7build/"

For centos 8

/bin/bash -c "cmake -S /app -B /app/centos8build/; cmake --build /app/centos8build/"

Related