1

If i use abseil and libtorch together, and use the function like StrAppend, the build will fail and give errors like

undefined reference to `absl::StrCat(absl::AlphaNum const&, absl::AlphaNum const&, absl::AlphaNum const&)'

My Cmakelist is like

cmake_minimum_required(VERSION 3.25)
project(simple_tests)
set(ABSL_PROPAGATE_CXX_STD ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_STANDARD 17)
set(_GLIBCXX_USE_CXX11_ABI 0)
set(CONDA_PREFIX "/home/qzz/.conda/envs/bridge_research/")
set(PYTHON_EXECUTABLE "${CONDA_PREFIX}/bin/python")
set(PYTHON_INCLUDE_DIR "${CONDA_PREFIX}/include/python3.8")
set(PYTHON_LIBRARIES "${CONDA_PREFIX}/lib/libpython3.so")
set(CMAKE_CUDA_COMPILER "/usr/local/cuda-11.7/bin/nvcc")
set(CMAKE_CUDA_ARCHITECTURES 86)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs REQUIRED)
set(TorchPath /home/qzz/.conda/envs/bridge_research/lib/python3.8/site-packages/torch)
message(STATUS TorchPath=${TorchPath})


list(APPEND CMAKE_PREFIX_PATH ${TorchPath})

find_package(Torch REQUIRED)
set(TORCH_PYTHON_LIBRARIES  "${TorchPath}/lib/libtorch_python.so")
add_subdirectory(third_party/abseil-cpp)
add_subdirectory(third_party/pybind11)

add_library(t SHARED absl_simple_tests.h)
target_link_libraries(t PUBLIC
        absl::base
        absl::algorithm
        absl::flags
        absl::optional
        absl::random_random
        absl::strings
        absl::str_format
        absl::time
        ${TORCH_LIBRARIES}
        )

target_include_directories(t PUBLIC
        ${TORCH_INCLUDE_DIRS}
        ${CMAKE_CURRENT_SOURCE_DIR}
        third_party/abseil-cpp
        third_party/)

add_executable(simple_tests main.cpp)
target_link_libraries(simple_tests PUBLIC t)

The file absl_simple_tests.h is

#include "absl/strings/str_cat.h"

#ifndef SIMPLE_TESTS_ABSL_SIMPLE_TESTS_H
#define SIMPLE_TESTS_ABSL_SIMPLE_TESTS_H

std::string TestStrCat() {
    std::string s1;
    s1 = absl::StrCat("A string ", " another string", "yet another string");
    return s1;
}

std::string TestStrAppend() {
    std::string s = "foo";
    absl::StrAppend(&s, "123");
    return s;
}



#endif //SIMPLE_TESTS_ABSL_SIMPLE_TESTS_H

The file main.cpp is like

#include <iostream>
#include "absl_simple_tests.h"
#include "torch/torch.h"


int main() {
    auto s = TestStrCat();
    std::cout << s << std::endl;
    s = TestStrAppend();
    std::cout << s << std::endl;
    std::cout << torch::eye(3) << std::endl;
    return 0;
}

If i build this, I will get errors:

/usr/bin/ld: CMakeFiles/simple_tests.dir/main.cpp.o: in function `TestStrCat()':
main.cpp:(.text+0xc3): undefined reference to `absl::StrCat(absl::AlphaNum const&, absl::AlphaNum const&, absl::AlphaNum const&)'
/usr/bin/ld: CMakeFiles/simple_tests.dir/main.cpp.o: in function `TestStrAppend()':
main.cpp:(.text+0x1a4): undefined reference to `absl::StrAppend(std::string*, absl::AlphaNum const&)'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

I have tested libtorch and abseil seperately, they all worked successfully. The libtorch is just torch2.0. I'm using ubuntu 22.04 with clion, cmake, ninja. I'm not sure if my cmakelist is wrongly written.

I tested on windows and this can work well. I expect it can run successfully.

hanyuu1
  • 11
  • 1

0 Answers0