ROOT + CMake + VisualStudio - macro builds but doesn't work

Hello,
I am using Visual Studio 2019 (16.3.4).
I have installed ROOT 6.18.04 using the installer.

I tried to compile a simple program, but it doesn’t work.

This is my CMakeLists:

project(CMakeRootTest CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

cmake_minimum_required (VERSION 3.9)

# Add ROOT to path
list(APPEND CMAKE_PREFIX_PATH ${ROOTSYS}) # $ROOTSYS points to C:\root_v6.18.04\

find_package(ROOT 6.18 CONFIG REQUIRED)

# Add source to this project's executable.
add_executable (CMakeRootTest "CMakeRootTest.cpp")
target_link_libraries(CMakeRootTest PUBLIC ROOT::Core)

CMakeRootTest.cpp:

#include <TString.h>
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
	std::cout << "Program start\n";
	TString t = "TEST TString";
	std::cout << t << std::endl;

	return EXIT_SUCCESS;
}

The project builds. I have copied all of the *.dll, *.rdict.pcm and *.rootmap files from the ROOT’s bin folder to ensure that the executable has all of the files it needs.

Unfortunately, it doesn’t even reach the main() function, it crashes somewhere in the initialization with

Unhandled exception at 0x77A20E43 (ntdll.dll) in CMakeRootTest.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77A5E930).

I’ll try, but in the meanwhile, can you tell me how you build your application?

I have created the CMake project in the IDE using the built-in wizard, added the “-DROOTSYS=C:\root_v6.18.04” to CMakeSettings.json (“CMake command arguments”), changed “Configuration type” to “Release”, “Toolset” to “msvc_x86” and replaced the content of the default files with what I’ve shown.

I just built your app with ROOT (master), Visual Studio 2019 v16.3.4 and this CMakeLists.txt:

project(CMakeRootTest CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

cmake_minimum_required (VERSION 3.9)

# Add ROOT to path
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})

find_package(ROOT 6.18 CONFIG REQUIRED)
include_directories($ENV{ROOTSYS}/include)

# Add source to this project's executable.
add_executable (CMakeRootTest "CMakeRootTest.cpp")
target_link_libraries(CMakeRootTest PUBLIC ROOT::Core)

Configuring this way:

C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build>cmake -A Win32 -Thost=x64 -G"Visual Studio 16 2019" ..\
-- The CXX compiler identification is MSVC 19.23.28106.4
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/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/forum/piotr-nowakowski/build

Here is the result when building with the cmake --build command:

C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build>cmake --build . --config Debug
Microsoft (R) Build Engine version 16.3.1+1def00d3d for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Checking Build System
  CMake does not need to re-run because C:/Users/bellenot/rootdev/forum/piotr-nowakowski/build/CMakeFiles/generate.stam
  p is up-to-date.
  Building Custom Rule C:/Users/bellenot/rootdev/forum/piotr-nowakowski/CMakeLists.txt
  CMake does not need to re-run because C:/Users/bellenot/rootdev/forum/piotr-nowakowski/build/CMakeFiles/generate.stam
  p is up-to-date.
  CMakeRootTest.cpp
C:\Users\bellenot\build\debug\include\ROOT\libcpp_string_view.h(193,9): warning C4068: unknown pragma [C:\Users\belleno
t\rootdev\forum\piotr-nowakowski\build\CMakeRootTest.vcxproj]
  CMakeRootTest.vcxproj -> C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build\Debug\CMakeRootTest.exe
  Building Custom Rule C:/Users/bellenot/rootdev/forum/piotr-nowakowski/CMakeLists.txt
  CMake does not need to re-run because C:/Users/bellenot/rootdev/forum/piotr-nowakowski/build/CMakeFiles/generate.stam
  p is up-to-date.

And then, running it it:

C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build>Debug\CMakeRootTest.exe
Program start
TEST TString

C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build>

N.B. I called root\bin\thisroot.bat before configuring/building the app. And I build in Debug mode because I use a Debug build of ROOT (one cannot mix debug/release modes on Windows.

I also tried with ROOT v6.18.04 and it works the same.

The crash was caused by the ROOTSYS variable not being present when the program is executed.

1 Like