C++ MNIST example code failed to compile on Linux WSL2

Tried to compile and run the C++ MNIST training example on WSL2, but I ran into the following error at runtime, thought the compilation went very smooth. The system is Core Ultra 7 + RTX2000 Ada + Windows 11 + WSL2(Ubuntu 22.04) + CUDA version 13.2 + PyTorchBuild -> Stable(2.12). The LibTorch library is placed at `$HOME/code/cpp/libtorch`


CUDA available! Training on GPU.
terminate called after throwing an instance of 'c10::Error'
  what():  Accessing empty ModuleHolder
Exception raised from ptr at $HOME/code/cpp/libtorch/include/torch/csrc/api/include/torch/nn/pimpl.h:103 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0x9d (0x7f482d2e6fad in $HOME/code/cpp/libtorch/lib/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, char const*) + 0x69 (0x7f482d275373 in $HOME/code/cpp/libtorch/lib/libc10.so)
frame #2: <unknown function> + 0x250fd (0x55579b29d0fd in ./mnist)
frame #3: <unknown function> + 0x1e7fe (0x55579b2967fe in ./mnist)
frame #4: <unknown function> + 0x176a0 (0x55579b28f6a0 in ./mnist)
frame #5: <unknown function> + 0xb498 (0x55579b283498 in ./mnist)
frame #6: <unknown function> + 0x29d90 (0x7f47ff069d90 in /lib/x86_64-linux-gnu/libc.so.6)
frame #7: __libc_start_main + 0x80 (0x7f47ff069e40 in /lib/x86_64-linux-gnu/libc.so.6)
frame #8: <unknown function> + 0xb305 (0x55579b283305 in ./mnist)

Aborted

The CMakeLists.txt is the following

cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(mnist)

find_package(CUDA REQUIRED)
find_package(Torch REQUIRED)
set(USE_KINETO 0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)

add_executable(mnist mnist.cpp)
target_link_libraries(mnist "${TORCH_LIBRARIES}")
set_property(TARGET mnist PROPERTY CXX_STANDARD 20)

I did notice that if I set add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0), the following errors occurred at linking

/usr/bin/ld: CMakeFiles/mnist.dir/mnist.cpp.o: in function `main':
mnist.cpp:(.text+0x135): undefined reference to `torch::data::datasets::MNIST::MNIST(std::string const&, torch::data::datasets::MNIST::Mode)'
/usr/bin/ld: mnist.cpp:(.text+0x37e): undefined reference to `torch::data::datasets::MNIST::MNIST(std::string const&, torch::data::datasets::MNIST::Mode)'
/usr/bin/ld: CMakeFiles/mnist.dir/mnist.cpp.o: in function `c10::Device::validate()':
mnist.cpp:(.text._ZN3c106Device8validateEv[_ZN3c106Device8validateEv]+0x88): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, std::string const&)'
/usr/bin/ld: mnist.cpp:(.text._ZN3c106Device8validateEv[_ZN3c106Device8validateEv]+0x125): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, std::string const&)'
/usr/bin/ld: CMakeFiles/mnist.dir/mnist.cpp.o: in function `c10::intrusive_ptr_target::~intrusive_ptr_target()':
mnist.cpp:(.text._ZN3c1020intrusive_ptr_targetD2Ev[_ZN3c1020intrusive_ptr_targetD5Ev]+0xde): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, std::string const&)'
...

After extensive Googling, I haven’t found a solution. Any ideas and suggestions?

Unsure how helpful but the example is not failing on Linux. I don’t have access to a WSL2 node at the moment.

@David_Warner What “fix” do I need to resolve this issue? Please advise. If the root cause, as you pointed out in your post, is the mismatching between the lib paths, what is the correct path setup? Thank you so much.