LibTorch (PyTorch C++) LNK2001 errors - c++

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.

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

Unresolved external symbol _glfwInit due to 32-bit/64-bit mismatch

How do I link the library in a way that visual studio 2015 can actually see it?
Here's a few screenshot of my folders:
glfw3, glfw3/include/glfw/, glfw3/lib
I've attempted to link these folders into visual studio, under "vc++ directories => include/library directories as such: include, library
also linking the same to my project directly, under linker => input => additional dependencies
to me, that seems like everything is correct (this is how it was in the tutorial i am following, learnopengl.com)
note: I am also doing the same with the GLAD library, which that has a .c file that I put directly into my sources, along with the header files linked the same way as with GLFW.
but despite trying to run this code:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main() {
glfwInit();
return 0;
}
it will always return this error, or similar ones:
1>------ Build started: Project: opengl_test, Configuration: Debug Win32 ------
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol _glfwInit referenced in function _main
1>C:\Users\Honza\Desktop\C++ programs\lib\glfw3\lib\glfw3.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'X86'
1>c:\users\honza\documents\visual studio 2015\Projects\opengl_test\Debug\opengl_test.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'm guessing there is something wrong with how I linked the libraries. I am willing to literally re-install visual studio if I have to, just please give me some possible solutions. I want to start learning opengl but all this jazz with linking libraries is leaving me frustrated and confused.
EDIT: The problem here was that I was using 64-bit GLFW binaries instead of the 32-bit ones, and compiling in 32-bit. I've fixed that, and now I get even more errors:
1>------ Build started: Project: opengl_test, Configuration: Debug Win32 ------
1>libglfw3.a(init.c.obj) : error LNK2019: unresolved external symbol ___chkstk_ms referenced in function __glfwInputError
1>libglfw3.a(init.c.obj) : error LNK2019: unresolved external symbol _vsnprintf referenced in function __glfwInputError
1>MSVCRTD.lib(vsnprintf.obj) : error LNK2001: unresolved external symbol _vsnprintf
1>libglfw3.a(context.c.obj) : error LNK2019: unresolved external symbol _sscanf referenced in function __glfwRefreshContextAttribs
1>MSVCRTD.lib(vsnprintf.obj) : error LNK2001: unresolved external symbol __vsnprintf
1>C:\Users\Honza\Documents\Visual Studio 2015\Projects\opengl_test\Debug\opengl_test.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You write that you tried to link "these folders", while in fact, you need 2 separate things in order to use a function from a static library.
Use the proper header file so compilation succeeds. From the error you quoted, compilation went well. The program knows there is an external function named _glfwInit.
Link to the specific library (.lib file). Looks like you placed a path of a folder and not the full path of the .lib file.
Under Linker => input => additional dependencies, place the full path of the gfw3.lib file.
I faced the same problem while following learnopengl.com, The issue is that you need to rebuild glfw library with cmake again,
but this time when you choose visual studio 2015 or whatever for your generator for this project, you must also choose: OPTIONAL PLATFORM FOR GENERATOR -> x64
you can make sure that every thing is OK when you build glfw inside Visual Studio and the platform that appears is x64

MSVC 2015 only provides me with linker error

I am working on a solution with 100+ files. I am using MS Visual Studio 2015 Pro. Compiling it is fine, problems come at link stage.
LINK complains on one of the project's library but only gives me the error number.
The command line (simplified) used is the following:
LINK.exe /NOLOGO /MANIFEST /IMPLIB:C:\\MySolution\\myownlib.lib -LARGEADDRESSAWARE -subsystem:console -MACHINE:X86 -DEBUG -INCREMENTAL /DLL File1.cpp.1.o File2.cpp.1.o File3.cpp.1.o File4.cpp.1.o /OUT:C:\\MySolution\\myownlib.dll
The only output I get is the following:
File1.cpp.1.o : error LNK2005:
File2.cpp.1.o : error LNK2005:
File3.cpp.1.o : error LNK2005:
File4.cpp.1.o : error LNK2005:
File1.cpp.1.o : error LNK2001:
File2.cpp.1.o : error LNK2001:
File3.cpp.1.o : error LNK2001:
File4.cpp.1.o : error LNK2001:
I cannot find any clue as to why the linker does not complains with the duplicated symbols nor the unresolved ones. Since the lib owns more than 80 objects, and nearly all of them is listed, finding the erroneous symbol(s) is like looking for a needle in a haystack!
EDIT : I do not compile using the IDE, I use the developer command prompt to build. Migrating the project in Visual Studio is not frankly possible as the program I am working on has never been imported under Visual Studio, we only use the compiler/linker. Moreover, a colleague of mine working on the project does not experience such troubles. It seems that something is going on under my environment...
EDIT 2 : Full installation repair fixed the problem.

CUDA 6.5: error MSB3191 Unable to create directory and LNK2001 Unresolved External symbol

I am running CUDA 6.5 on Visual Studio 2013 x64.
I have a VC++ static library project named MyLib which is linked with MyClient x64 console executable project. I added CUDA 6.5 build customization to both the projects. In addition, I goto Configuration Properties -> CUDA C/C++ and have set “Target Machine Platform” as 64-bit for these two projects as I want to compile them for x64 platform
I added MyCUDACode.cpp and MyCUDACode.h files to MyLib project and marked their item type as CUDA C/C++ instead oc C/C++ compiler. MyCUDACode.cpp has number of functions which call CUDA API functions. Also, MyCUDACode.h references the cuda.h and cuda_runtime.h files.
MyClient is linked with MyLib. I now get two compilation errors:
First error in compiling MyLib which says a Unable to create directory
Second, I get unresolved external symbol errors while linking
MyClient with MyLib. Here is the log output:
------ Build started: Project: MyLib, Configuration: Release x64 ------
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\BuildCustomizations\CUDA 6.5.targets(412,9): error MSB3191: Unable to create directory "x64\Release\D:\github\src\helpers\". The given path's format is not supported.
2>------ Build started: Project: MyClient, Configuration: Release x64 ------
2>MyLib.lib(MyCUDACode.obj) : error LNK2001: unresolved external symbol cudaGetDeviceCount
2>MyLib.lib(MyCUDACode.obj) : error LNK2001: unresolved external symbol cudaDeviceCanAccessPeer
2>MyLib.lib(MyCUDACode.obj) : error LNK2001: unresolved external symbol cudaGetDeviceProperties
2>MyLib.lib(MyCUDACode.obj) : error LNK2001: unresolved external symbol cudaSetDevice
2>MyLib.lib(MyCUDACode.obj) : error LNK2001: unresolved external symbol cudaDeviceDisablePeerAccess
2>MyLib.lib(MyCUDACode.obj) : error LNK2001: unresolved external symbol cudaDeviceReset
2>MyLib.lib(MyCUDACode.obj) : error LNK2001: unresolved external symbol cudaDeviceEnablePeerAccess
2>C:...\visual studio 2013\Projects\Prj\x64\Release\MyClient.exe : fatal error LNK1120: 7 unresolved externals
I have tried both release and debug modes but I get same error.
I am just reproducing the steps to resolve the errors:
To resolve the error(temporary workaround): MSB3191: Unable to create directory The given path's format is not supported., simply copy Cuda cu and cuh files to Visual studio project folder.
[UPDATE]: I recently found a better solution for the issue MSB3191: Unable to create directory The given path's format is not supported.. This fix works without need to move around the cu files to the VS project folder. For this, open the file "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\BuildCustomizations\CUDA 6.5.targets", and move to the offending line(412 in this case). This line contains following xml section:
<MakeDir
Condition="'%(CudaCompile.ExcludedFromBuild)' != 'true'"
Directories="%(CudaCompile.DepsOutputDir)" />
As you can see, this is a sort of directive which tells the build system to make a folder for the cuda files being compiled. The property used for folder name is CudaCompile.DepsOutputDir. Now open the file: "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\BuildCustomizations\CUDA 6.5.props". This props file defines values of the properties used in targets file. Now search for word DepsOutputDirin this props file. There are only XML section which defines and refers to this keyword is:
<!-- Miscellaneous -->
<DepsOutputFile>%(Filename)%(Extension).deps</DepsOutputFile>
<DepsOutputDir>$(IntDir)%(RelativeDir)</DepsOutputDir>
<DepsOutputPath>%(DepsOutputDir)%(DepsOutputFile)</DepsOutputPath>
Now, to finally fix the MSB3191 issue, simply remove the variable %(RelativeDir) from above xml section and save the props file (this would require administrative privileges)
To resolve the error: LNK2001: unresolved external symbol, follow the Settings for Linker section in this post

WMI Linker Error on x64

I'm trying to use the WMI example from msdn:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa384724%28v=vs.85%29.aspx
I've copied the last set of code there verbatim into a console application in VS2008. If I have the application in release or debug for the win32 platform, it compiles (and runs) fine. If I have it in release or debug for the x64 platform, I get the following linker errors:
CppConsole.obj : error LNK2001: unresolved external symbol IID_IWbemConfigureRefresher
CppConsole.obj : error LNK2001: unresolved external symbol CLSID_WbemRefresher
CppConsole.obj : error LNK2001: unresolved external symbol IID_IWbemRefresher
CppConsole.obj : error LNK2001: unresolved external symbol CLSID_WbemLocator
CppConsole.obj : error LNK2001: unresolved external symbol IID_IWbemLocator
fatal error LNK1120: 5 unresolved externals
I've already tried putting the wbemuuid.lib into the linker input directly in the project properties, but that didn't make a difference from the pragma.
Has anybody made this work with x64? Or is there something else I'm doing wrong?
I give credit to RRUZ for this, as he/she at least put me on the right track, but as they haven't posted an answer-answer (just a comment) I can't click them for credit.
Basically, because of the work environment I'm in, the project directories are set up in a "non-standard" way. There was already a copy of wbemuuid.lib in another directory that was not the correct version (not x64), and that directory was higher on the library include list, thus never getting to the right Windows Platform library directory.
So if you ever have problems with x86 vs x64 and library includes, check your directories and check the ORDER of them as well.

Why am I getting error LNK2001 when linking to zlib.lib?

I'm working on a project that already contains the gzip library as follows:
zlib\zlib.h
zlib\zlib.lib
zlib\zconf.h
I would like to use the gzip functions from this .lib but am getting the following errors:
Compress.cpp
Linking...
Compress.obj : error LNK2001: unresolved external symbol _gzclose
Compress.obj : error LNK2001: unresolved external symbol _gzerror
Compress.obj : error LNK2001: unresolved external symbol _gzwrite
Compress.obj : error LNK2001: unresolved external symbol _gzopen
.\Debug/files.exe : fatal error LNK1120: 4 unresolved externals
Error executing link.exe.
The link settings include:
Object/library modules: zlib.lib
Project Options:
zlib.lib
In the file using the gzX() functions, it
#include "zlib/zlib.h"
What else needs to be done here to use these functions?
Thank You.
EDIT: Using Visual Studio 6.0 C++
EDIT2: It turned out the static library I was using had the gz() functions taken out of them. The header file still had them which was misleading.
I grabbed the one off here to get zlib to build in windows. If you did the same, you may have forgotten to #define ZLIB_WINAPI before including zlib.h
you also need to add zlib.lib to your project's libraries:
Project properties->Linker->Input->Additional Dependencies.
When the build stops, ctrl-click on the URL to see the more verbose form of the log and check the actual command line passed to the linker. That at least will tell you whether the option to link against zlib is being respected. You may get other useful diagnostic output. One possibility could be that the architecture is different (eg you're building x64 but the lib is x86)
It turned out the static library I was using had the gz() functions taken out of them. The header file still had them which was misleading.