Linking ROOT libs with cmake in Windows

I’ve been trying for a few days to get ROOT working on a Windows 10 machine. It’s been a challenge and there are a lot of pieces that I’m not yet familiar with but I’ve so far managed to:

  • Install pre-reqs, including Git, Python 3 (32-bit), CMake.
  • Clone ROOT.
  • Use cmake to generate a project and build root from the command line.
  • Run ROOT from the command line.
  • Go through much of the ROOTPrimer.

Now, I want to reference ROOT libraries in my custom application. I’m getting stuck when trying to link. This is the error:

C:\root\vs\tutorials\Sandbox\ROOT_1\Bin>cmake --build . --config Debug
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

ROOT_HelloWorld.obj : error LNK2019: unresolved external symbol "public: __thiscall TVersionCheck::TVersionCheck(int)" (??0TVersionCheck@@QAE@H@Z) referenced in function "void __cdecl ROOT::Internal::`dynamic initializer for 'gVersionCheck''(void)" (??__EgVersionCheck@Internal@ROOT@@YAXXZ) [C:\root\vs\tutorials\Sandbox\ROOT_1\Bin\ROOT_HelloWorld.vcxproj]
C:\root\vs\tutorials\Sandbox\ROOT_1\Bin\Debug\ROOT_HelloWorld.exe : fatal error LNK1120: 1 unresolved externals [C:\root\vs\tutorials\Sandbox\ROOT_1\Bin\ROOT_HelloWorld.vcxproj]

My CMakeList.txt is just this:

cmake_minimum_required(VERSION 3.10)

project(ROOT_HelloWorld)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

$ROOTSYS/etc/cmake/FindROOT.cmake
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})

set(ROOT_DIR "c:/root/vs")
find_package(ROOT REQUIRED)

include(${ROOT_USE_FILE})

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

add_executable(ROOT_HelloWorld ROOT_HelloWorld.cpp)

target_link_libraries(ROOT_HelloWorld PUBLIC)

And my ROOT_HellowWorld.cpp is just this:

// A simple program to test root.
#include <cmath>
#include <iostream>
#include <string>
#include "TROOT.h"

int main(int argc, char* argv[])
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}

CMakeLists.txt (960 Bytes)
ROOT_HelloWorld.cpp (211 Bytes)

I’ve spent a couple of hours trying to find an example that I can get to work, but so far have come up empty. Please let me know if you know how to fix the problem or where to look.


Please read tips for efficient and successful posting and posting code

_ROOT Version:6.21/01
Platform: Windows 10
Compiler: Microsoft ® C/C++ Optimizing Compiler Version 19.24.28315 for x86


Here is a simple example (I didn’t try):

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

set(PROJECT_NAME ROOT_HellowWorld)
project(${PROJECT_NAME})

find_package(ROOT REQUIRED)

set(CMAKE_CXX_FLAGS "${ROOT_CXX_FLAGS}")

include(${ROOT_USE_FILE})
include_directories(${ROOT_INCLUDE_DIRS})
link_directories(${ROOT_LIBRARY_DIR})

add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp)
target_link_libraries(${PROJECT_NAME} ${ROOT_LIBRARIES} )

OK, so I just tested it, and it works:

C:\Users\bellenot\rootdev\blah\build>cmake -G"Visual Studio 16 2019" -A Win32 -Thost=x64 -DCMAKE_VERBOSE_MAKEFILE=ON ..\
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17763.
-- The C compiler identification is MSVC 19.24.28319.0
-- The CXX compiler identification is MSVC 19.24.28319.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/bellenot/rootdev/blah/build

C:\Users\bellenot\rootdev\blah\build>cmake --build . --config Release
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 03/04/2020 23:22:14.
Project "C:\Users\bellenot\rootdev\blah\build\ALL_BUILD.vcxproj" on node 1 (default targets).
Project "C:\Users\bellenot\rootdev\blah\build\ALL_BUILD.vcxproj" (1) is building "C:\Users\bellenot\rootdev\b
lah\build\ZERO_CHECK.vcxproj" (2) on node 1 (default targets).
PrepareForBuild:
  Creating directory "Win32\Release\ZERO_CHECK\".
  Creating directory "C:\Users\bellenot\rootdev\blah\build\Release\".
  Creating directory "Win32\Release\ZERO_CHECK\ZERO_CHECK.tlog\".
InitializeBuildStatus:
  Creating "Win32\Release\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
CustomBuild:
  Checking Build System
FinalizeBuildStatus:
  Deleting file "Win32\Release\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild".
  Touching "Win32\Release\ZERO_CHECK\ZERO_CHECK.tlog\ZERO_CHECK.lastbuildstate".
Done Building Project "C:\Users\bellenot\rootdev\blah\build\ZERO_CHECK.vcxproj" (default targets).

Project "C:\Users\bellenot\rootdev\blah\build\ALL_BUILD.vcxproj" (1) is building "C:\Users\bellenot\rootdev\b
lah\build\ROOT_HellowWorld.vcxproj" (3) on node 1 (default targets).
PrepareForBuild:
  Creating directory "ROOT_HellowWorld.dir\Release\".
  Creating directory "ROOT_HellowWorld.dir\Release\ROOT_HellowWorld.tlog\".
InitializeBuildStatus:
  Creating "ROOT_HellowWorld.dir\Release\ROOT_HellowWorld.tlog\unsuccessfulbuild" because "AlwaysCreate" was
  specified.
CustomBuild:
  Building Custom Rule C:/Users/bellenot/rootdev/blah/CMakeLists.txt
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x86\CL.
  exe /c /IC:\Users\bellenot\git\master\build\win /IC:\Users\bellenot\git\master\build\win /IC:\Users\belleno
  t\build\release\include /nologo /W1 /WX- /diagnostics:column /O2 /Ob2 /Oy- /D _WIN32 /D _XKEYCHECK_H /D NOM
  INMAX /D _CRT_SECURE_NO_WARNINGS /D NDEBUG /D "CMAKE_INTDIR=\"Release\"" /D _MBCS /Gm- /MD /GS /fp:precise
  /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"ROOT_HellowWorld.dir\Release\\" /Fd"ROOT_HellowWorld.dir\Releas
  e\vc142.pdb" /Gd /TP /wd4141 /wd4291 /wd4244 /wd4049 /wd4141 /wd4291 /wd4244 /wd4049 /FIw32pragma.h /FIsehm
  ap.h /FIw32pragma.h /FIsehmap.h /analyze- /errorReport:queue  -Zc:__cplusplus -EHsc- -Zc:__cplusplus -EHsc-
   C:\Users\bellenot\rootdev\blah\ROOT_HellowWorld.cpp
  ROOT_HellowWorld.cpp
Link:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x86\lin
  k.exe /ERRORREPORT:QUEUE /OUT:"C:\Users\bellenot\rootdev\blah\build\Release\ROOT_HellowWorld.exe" /INCREMEN
  TAL:NO /NOLOGO /LIBPATH:C:/Users/bellenot/build/release/lib /LIBPATH:C:/Users/bellenot/build/release/lib/Re
  lease C:\Users\bellenot\build\release\lib\libCore.lib C:\Users\bellenot\build\release\lib\libImt.lib C:\Use
  rs\bellenot\build\release\lib\libRIO.lib C:\Users\bellenot\build\release\lib\libNet.lib C:\Users\bellenot\b
  uild\release\lib\libHist.lib C:\Users\bellenot\build\release\lib\libGraf.lib C:\Users\bellenot\build\releas
  e\lib\libGraf3d.lib C:\Users\bellenot\build\release\lib\libGpad.lib C:\Users\bellenot\build\release\lib\lib
  ROOTDataFrame.lib C:\Users\bellenot\build\release\lib\libTree.lib C:\Users\bellenot\build\release\lib\libTr
  eePlayer.lib C:\Users\bellenot\build\release\lib\libRint.lib C:\Users\bellenot\build\release\lib\libPostscr
  ipt.lib C:\Users\bellenot\build\release\lib\libMatrix.lib C:\Users\bellenot\build\release\lib\libPhysics.li
  b C:\Users\bellenot\build\release\lib\libMathCore.lib C:\Users\bellenot\build\release\lib\libThread.lib ker
  nel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi
  32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /PDB:"C:/Users/bellenot/
  rootdev/blah/build/Release/ROOT_HellowWorld.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB
  :"C:/Users/bellenot/rootdev/blah/build/Release/ROOT_HellowWorld.lib" /MACHINE:X86 /SAFESEH  /machine:X86 RO
  OT_HellowWorld.dir\Release\ROOT_HellowWorld.obj
  ROOT_HellowWorld.vcxproj -> C:\Users\bellenot\rootdev\blah\build\Release\ROOT_HellowWorld.exe
FinalizeBuildStatus:
  Deleting file "ROOT_HellowWorld.dir\Release\ROOT_HellowWorld.tlog\unsuccessfulbuild".
  Touching "ROOT_HellowWorld.dir\Release\ROOT_HellowWorld.tlog\ROOT_HellowWorld.lastbuildstate".
Done Building Project "C:\Users\bellenot\rootdev\blah\build\ROOT_HellowWorld.vcxproj" (default targets).

PrepareForBuild:
  Creating directory "Win32\Release\ALL_BUILD\".
  Creating directory "Win32\Release\ALL_BUILD\ALL_BUILD.tlog\".
InitializeBuildStatus:
  Creating "Win32\Release\ALL_BUILD\ALL_BUILD.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
CustomBuild:
  Building Custom Rule C:/Users/bellenot/rootdev/blah/CMakeLists.txt
FinalizeBuildStatus:
  Deleting file "Win32\Release\ALL_BUILD\ALL_BUILD.tlog\unsuccessfulbuild".
  Touching "Win32\Release\ALL_BUILD\ALL_BUILD.tlog\ALL_BUILD.lastbuildstate".
Done Building Project "C:\Users\bellenot\rootdev\blah\build\ALL_BUILD.vcxproj" (default targets).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.51

C:\Users\bellenot\rootdev\blah\build>
C:\Users\bellenot\rootdev\blah\build>Release\ROOT_HellowWorld.exe
Hello World!

C:\Users\bellenot\rootdev\blah\build>

Yes!! Thank you for the timely help.

I was able to compile and run. Thanks for posting your commands and output as well. I have been guessing my way through a lot of this and it’s nice to see how your commands compare to what I was doing.

1 Like