# adding gdal as dependency on windows for msys2 compilers and msvc

**URL:** https://discourse.cmake.org/t/adding-gdal-as-dependency-on-windows-for-msys2-compilers-and-msvc/10372
**Category:** Usage
**Created:** [March 15, 2024, 1:47pm UTC](https://discourse.cmake.org/t/adding-gdal-as-dependency-on-windows-for-msys2-compilers-and-msvc/10372 "2024-03-15T13:47:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [March 15, 2024, 1:47pm UTC](https://discourse.cmake.org/t/adding-gdal-as-dependency-on-windows-for-msys2-compilers-and-msvc/10372/1 "2024-03-15T13:47:38Z")

</div>

Hi,

In order to use gdal library with msvc I used a windows installer from OSGeo4W. Though I couldn’t locate any cmake package files within the installation, it seemed to be correctly detected by `find_package`. I put it to the credit of [FindGDAL](https://cmake.org/cmake/help/latest/module/FindGDAL.html) though I don’t know how it works.

Now I want to use also gdal with a gcc or clang compiler, provided by msys2. I installed the ucrt64 version of these compiler and they are functioning as expected.

I’ve just installed gdal again with msys2 in ucrt64 version and now `find_package` is messed up. I can see within `cmake` variables:

> – GDAL\_DIR=C:/Users/\<userlogin\>/\<pathtomsys2\>/ucrt64/lib/cmake/gdal  
> – GDAL\_FOUND=1  
> – GDAL\_INCLUDE\_DIR=GDAL\_INCLUDE\_DIR-NOTFOUND

Here, again, I don’t know how the new gdal install could have been detected. (Yet my `Path` is containing `C:/Users/<userlogin>/<pathtomsys2>/ucrt64/lib`)

How can I switch between both versions, ie, if my compiler is msvc, rely on previous detection and when it is gcc or clang, detect, correctly, the msys2 version?

[EDIT] for msys2, cmake configuration files can be found within `C:/Users/<userlogin>/<pathtomsys2>/ucrt64/lib/cmake/gdal`

Regards,  
A.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [March 15, 2024, 3:50pm UTC](https://discourse.cmake.org/t/adding-gdal-as-dependency-on-windows-for-msys2-compilers-and-msvc/10372/2 "2024-03-15T15:50:17Z")

</div>

I tried that:

```cmake
if(NOT(DEFINED ENV{GDAL_DIR}))
	# On windows with 2 installs: an msvc-compiled and a gcc-compiled one.
	if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
		# MSVC
		if(DEFINED ENV{GDAL_DIR_msvc})
			set(ENV{GDAL_DIR} $ENV{GDAL_DIR_msvc})
		endif() 
	elseif(DEFINED ENV{GDAL_DIR_msys2})
		# gcc with msys2
		set(ENV{GDAL_DIR} $ENV{GDAL_DIR_msys2})
	else()
		message(WARNING "GDAL NOT FOUND OR NOT PROPERLY INSTALL")
	endif()
endif()

```

And I set two user environment variables:

```auto
GDAL_DIR_msvc=C:/OSGeo4W/include
GDAL_DIR_msys2=C:/Users/<userlogin>/<pathtomsys2>/ucrt64/lib/cmake/gdal

```

`GDAL_DIR_msvc` is pointing to the directory containing `gdal.h`.

Yet, when using msvc, `$ENV{GDAL_DIR}` is `C:/OSGeo4W/include` as expected but msys2 is still detected thus compilation fails due to ABI discrepancies.

I don’t understand how `find_package` could find again the msys2 version.

[EDIT] looking in debug mode, the issue is that `find_package` is looking for a `gdal-config.cmake`. In `C:/OSGeo4W/` and its sub-directories there are no such files but FindGDAL module should be able to pick `gdal.h` in the `includes` folder. Yet, I’ve got `C:/Users/<userlogin>/<pathtomsys2>/ucrt64/lib` in my user `Path`, and `find_package` starts by looking inside `Path` and finds a `gdal-config.cmake`.

How can I prevent, only for this library, when using MSVC `find_package` to look for gdal there. The solution must not change `find_package` behavior for other dependencies. For instance, I would have hope that having set `GDAL_ENV` or `GDAL_ROOT` would have give priority to this directory whatever the mode used (config or module). Is there a Package/CMake variable to tell wich mode to use?
