Commit 54214d618390292f96bd9ab055e4bbe5d54e1fbd

Patrick Steinhardt 2017-09-15T10:28:32

cmake: fix linker error with dbghelper library When the MSVC_CRTDBG option is set by the developer, we will link in the dbghelper library to enable memory lead detection in MSVC projects. We are doing so by adding it to the variable `CMAKE_C_STANDARD_LIBRARIES`, so that it is linked for every library and executable built by CMake. But this causes our builds to fail with a linker error: ``` LINK: fatal error LNK1104: cannot open file 'advapi32.lib;Dbghelp.lib' ``` The issue here is that we are treating the variable as if it were an array of libraries by setting it via the following command: ``` SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES}" "Dbghelp.lib") ``` The generated build commands will then simply stringify the variable, concatenating all the contained libraries with a ";". This causes the observed linking failure. To fix the issue, we should just treat the variabable as a simple string. So instead of adding multiple members, we just add the "Dbghelp.lib" library to the existing string, separated by a space character.

1
2
3
4
5
6
7
8
9
10
11
12
13
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4c03aa0..713b2b7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -173,7 +173,7 @@ IF (MSVC)
 	IF (MSVC_CRTDBG)
 		SET(GIT_MSVC_CRTDBG 1)
 		SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
-		SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES}" "Dbghelp.lib")
+		SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
 	ENDIF()
 
 	# /Zi - Create debugging information