. 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();
}
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.