AlastairG
AlastairG

Reputation: 4314

CMake find_path works on Linux but not on MingW. How do I fix this?

On a Linux system:

Execute the following commands:

mkdir test
cd test
mkdir mingw linux files
touch files/blah

Add the following CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
PROJECT(TEST LANGUAGES CXX)

find_path(BLAH_DIR
    blah
    PATHS ${CMAKE_CURRENT_SOURCE_DIR}/files
)
message(STATUS "BLAH_DIR=${BLAH_DIR}")

In the "mingw" folder run cmake with a mingw toolchain file like the following:

# the name of the target operating system
SET(CMAKE_SYSTEM_NAME Windows)

# which compilers to use for C and C++
SET(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
SET(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
SET(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)

# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32 )

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search 
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

In the "linux" folder, simply run cmake.

For MingW we get:

BLAH_DIR=BLAH_DIR-NOTFOUND

For Linux we get:

BLAH_DIR=/Development/test/files

as expected.

Why doesn't find_path work for MingW? Should I submit a bug report on CMake?

Upvotes: 1

Views: 961

Answers (2)

AlastairG
AlastairG

Reputation: 4314

Thanks to Tsyvarev for the helpful info.

The answer to the part of the title which says "How do I fix this?" is to specify NO_CMAKE_FIND_ROOT_PATH and NO_DEFAULT_PATH in the find_path command.

Upvotes: 2

Tsyvarev
Tsyvarev

Reputation: 65938

It is written in your toolchain file:

search headers and libraries in the target environment

Command find_path searches headers, so it looks under /usr/x86_64-w64-mingw32 only.

Even PATH option is prepended with that root path. So it cannot find a file located in the source tree.

See documentation for find_path command, and my answer to related question.

Upvotes: 1

Related Questions