Need Help with Strange Library Linking Issues

117 views
Skip to first unread message

lkimuk

unread,
Apr 20, 2024, 11:58:41 AM4/20/24
to ns-3-users
Hello,

My project depends on ns3 and libtorch. When I link only one of these libraries in CMake, there are no issues. However, when I try to link both libraries simultaneously, I encounter problems. Here is a simplified example:

// include/lib.h
void greet();
void print();

// src/lib.cpp
#include "lib.h"
#include <iostream>
#include <ns3/core-module.h>
#include <torch/torch.h>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("MyFirstLogExample");

void greet() {
std::cout << "hello torch\n";
torch::Tensor tensor = torch::rand({2, 3});
std::cout << tensor << std::endl;
}

void print()
{
std::cout << "print()\n";
LogComponentEnable ("MyFirstLogExample", LOG_LEVEL_INFO);
NS_LOG_INFO ("Hello, NS-3!");
}

// src/main.cpp
#include "lib.h"

int main()
{
greet();
print();
}


Here is the CMake file for this example:

cmake_minimum_required(VERSION 3.12)
project(main VERSION 1.0)

list(APPEND CMAKE_PREFIX_PATH "/home/xxx/software/libtorch")
find_package(Torch REQUIRED)
find_package(ns3 3.41 COMPONENTS libcore)

add_executable(main src/lib.cc src/main.cc)
target_compile_features(main PRIVATE cxx_std_20)

target_include_directories(main PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
target_link_libraries(main PRIVATE ns3::libcore)
target_link_libraries(main PRIVATE ${TORCH_LIBRARIES})


When I only link ns3 or libtorch individually (i.e., when only either the greet() or print() function exists), there are no issues with compiling and running. However, when I link both libraries together, I encounter the following errors:

```
/usr/bin/ld: CMakeFiles/main.dir/src/lib.cc.o: in function `print()':
lib.cc:(.text+0x404): undefined reference to `ns3::LogComponentEnable(std::string const&, ns3::LogLevel)'
/usr/bin/ld: lib.cc:(.text+0x4fd): undefined reference to `ns3::LogComponent::Name() const'
/usr/bin/ld: lib.cc:(.text+0x5a3): undefined reference to `ns3::LogComponent::GetLevelLabel(ns3::LogLevel)'
/usr/bin/ld: CMakeFiles/main.dir/src/lib.cc.o: in function `__static_initialization_and_destruction_0()':
lib.cc:(.text+0x1178): undefined reference to `ns3::LogComponent::LogComponent(std::string const&, std::string const&, ns3::LogLevel)'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/main.dir/build.make:120: main] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/main.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
```

I've checked several potential causes, and it doesn't seem to be related to the linking order. Also, the issue only arises with the ns3::libcore log module; for example, using ns3::Node in print() does not cause issues. Additionally, when I place the project under scratch/ and link libtorch in ns3's CMakeLists.txt, there are no issues, so it doesn't seem to be a version mismatch in linking either.

What could be causing this issue? I'd appreciate any insights or suggestions. Thank you very much for your help.

lkimuk

unread,
Apr 21, 2024, 1:03:25 AM4/21/24
to ns-3-users
After hours of debugging of the code I finally found the culprit. The libtorch is shipped with two types of build Pre-cxx11 and cxx11 ABIs. I was trying to link together object files that were compiled with different values for the
_GLIBCXX_USE_CXX11_ABI macro. Consequently, there are distinct types: std::__cxx11::basic_string and std::string, whcih casued undefiend references in the ns3 log module.

To resolve this, I simply directly used the libtorch cxx11 version and that just did it.

Gabriel Ferreira

unread,
Apr 21, 2024, 8:04:11 AM4/21/24
to ns-3-users

You have beaten me to it. But that is correct. Very good work in debugging this that quickly. I spent many days to figure this out.
Reply all
Reply to author
Forward
0 new messages