Home > database >  LibTorch (PyTorch C ) LNK2001 errors
LibTorch (PyTorch C ) LNK2001 errors

Time:11-20

I was following the tutorial in LibTorch here.

With the following changes:

  • example-app => Ceres
  • example-app.cpp => main.cxx

Everything worked until the CMake command cmake --build . --config Release.

It produced the following errors:

main.obj : error LNK2001: unresolved external symbol __imp___tls_index_?init@?1??lazy_init_num_threads@internal@at@@YAXXZ@4_NA [D:\Silverous Black\CPE42S2-CPE42S2\CPE 406\ProjectDumagan\src\Ceres\build\Ceres.vcxproj]
main.obj : error LNK2001: unresolved external symbol __imp___tls_offset_?init@?1??lazy_init_num_threads@internal@at@@YAXXZ@4_NA [D:\Silverous Black\CPE42S2-CPE42S2\CPE 406\ProjectDumagan\src\Ceres\build\Ceres.vcxproj]
D:\Silverous Black\CPE42S2-CPE42S2\CPE 406\ProjectDumagan\src\Ceres\build\Release\Ceres.exe : fatal error LNK1120: 2 unresolved externals [D:\Silverous Black\CPE42S2-CPE42S2\CPE 406\ProjectDumagan\src\Ceres\build\Ceres.vcxproj]

I don't believe these are from the changes I placed, since the problem is with the linking.

I also am trying to replicate this directly into Visual Studio. I am using Visual Studio 17 2022 which the LibTorch extension is not yet compatible with (Visual Studio 16 2019 is no longer available for install from the website).

The replication is through a blank C template (no starting files). And I have set the following macros:

  • LibTorchTarget = CPU specifies libtorch for CPU is to be used (useful for other macros
  • LibTorchDir = C:/libtorch/ directory where the libtorch installation(s) can be found (for multiple installations)
  • LibTorchInstall = $(LibTorchDir)libtorch_$(LibTorchTarget)/ expresses to C:/libtorch/libtorch_CPU/
  • LibTorchInclude = $(LibTorchInstall)include/ expresses to C:/libtorch/libtorch_CPU/include/
  • LibTorchLib = $(LibTorchInstall)lib/ expresses to C:/libtorch/libtorch_CPU/lib/

And have put the Include and Lib macros at their respective VC Directories positions. As well as $(LibTorchLib)*.lib (C:/libtorch/libtorch_CPU/lib/*.lib) in the Linker > Input > Additional Dependencies to specify all the .libs for linking (prevents a lot of LNK2009 errors).

And lastly, I have put start xcopy /s "$(LibTorchLib)*.dll" "$(OutDir)" /Y /E /D /R command at the Build Events > Pre-Link Event > Command Line to replicate the if clause in the CMakeLists.txt in the tutorial (apparently to avoid memory errors).

Result is same exact error with a final LNK1120 error:

Error   LNK2001 unresolved external symbol __imp___tls_index_?init@?1??lazy_init_num_threads@internal@at@@YAXXZ@4_NA    Ceres   D:\Silverous Black\CPE42S2-CPE42S2\CPE 406\ProjectDumagan\src\Ceres\main.obj    1   
Error   LNK2001 unresolved external symbol __imp___tls_offset_?init@?1??lazy_init_num_threads@internal@at@@YAXXZ@4_NA   Ceres   D:\Silverous Black\CPE42S2-CPE42S2\CPE 406\ProjectDumagan\src\Ceres\main.obj    1   
Error   LNK1120 2 unresolved externals  Ceres   D:\Silverous Black\CPE42S2-CPE42S2\CPE 406\ProjectDumagan\out\Debug_64\Ceres\Ceres.exe  1   

I don't exactly understand the reason for the LNK errors, so if anyone could help that'll be really nice. Thank you in advance.

CodePudding user response:

Look to: Updating to Visual Studio 17.4.0 Yields linker errors related to TLS

You most likely need to rebuild PyTorch after MSVC update.

  • Related