Visual Studio 2022: error LNK2019: unresolved external symbol when trying to compile a simple code using ROOT's libraries

ROOT Version: 6.30.02
Platform: Visual Studio Community 2022 v17.8.1 (Windows 10)
Compiler: MSBuild (with the c++17 standard)


Hello,

I’m trying to run an extremely simple code (just an empty main() function that does nothing and returns a 0, while including a header file from ROOT’s libraries; the point was just to check if I configured everything correctly), but the linker gives me an issue of unresolved external symbol.

I downloaded the debug x64 .exe file (I made sure it was the correct version to download), which on execution set the correct environment variables, and the fact that the ROOT application from my Start bar works properly suggests me that this is the case.


Then, in the project where I’m writing this code, I added the “include” and “lib” directories to the project properties (I triple-checked to make sure both paths are correct, and in the “External Dependencies” folder in the project solution, I can see the ROOT header files, including the one I’m trying to include in my code.

Some of ROOT's header files correctly showing in the Solution Explorer
However, when I try compiling, the output window gives me the error message error LNK2019: unresolved external symbol "public: __cdecl TVersionCheck::TVersionCheck(int)" (??0TVersionCheck@@QEAA@H@Z) referenced in function "void __cdecl ROOT::Internal::dynamic initializer for 'gVersionCheck''(void)" (??__EgVersionCheck@Internal@ROOT@@YAXXZ) (there is a backtick between ROOT::Internal:: and dynamic initializer, but I removed it from here because it ruins the formatting). I looked up this error message online, and apparently it’s caused by something being declared but never defined, but this doesn’t really help me solving the issue.

Is there another step in the configuration process that I missed and I should know of? If possible, I’d appreciate a solution that doesn’t involve downgrading to an earlier version of Visual Studio, using a different compiler or doing things from the command prompt.

Thank you in advance.

Fede.

Welcome to the ROOT Forum!
You must use the same compiler flags than the ones used to build ROOT, and the same configuration. I would suggest to use CMake to generate your project, it will automatically use the correct flags. And I would suggest also to build in RelWithDebInfo mode, for historical and technical reasons, the Debug build of ROOT is not a full Debug, to avoid having to link against the Debug runtime libraries (on Windows all dependencies would have to be build in debug mode…)

Thank you for fast reply.

Unfortunately, I have absolutely no experience in using CMake to build my projects, so I’ll have to spend some time trying to learn how to use it (or at least it seems harder to use than what I’m using, from what I’ve seen). I’ll reply back once I’ve learned it to confirm whether it worked or new issues arised that I need help with. For now, I’ll just thank you for your help

OK, as you prefer. On another hand, did you specify the ROOT libraries to link with in your project (at least libCore.lib)?
And here is a simple example of CMakeLists.txt for a simple project:

# Check if cmake has the required version
CMAKE_MINIMUM_REQUIRED(VERSION 3.16.4 FATAL_ERROR)
project(mytest)

find_package(ROOT REQUIRED)
set(CMAKE_CXX_FLAGS "${ROOT_CXX_FLAGS}")
include(${ROOT_USE_FILE})
include_directories(${ROOT_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
link_directories(${ROOT_LIBRARY_DIR})

add_executable(mytest SHARED mytest.cxx)
target_link_libraries(mytest ${ROOT_LIBRARIES})

Ok, I tried linking libCore.lib to the additional dependencies and now it works. I’m not entirely sure why I adding the directory with all the libraries to the Library Directory property alone is not enough and I also need to singularly link one of those libraries in that folder in another configuration property, but it doesn’t matter. Thank you so much for your help, you saved me a lot of trouble

1 Like

It’s the way it works. You have to explicitly link against each libraries you want to use.

1 Like