diff --git a/CMakeLists.txt b/CMakeLists.txt
index bcb01e4d6..be416bc84 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -521,7 +521,10 @@ if(NOT HAVE_ICONV)
 endif(NOT HAVE_ICONV)
 
 set(CMAKE_REQUIRED_LIBRARIES ${ICONV_LIBS})
-CHECK_C_SOURCE_RUNS("#include \"${CMAKE_SOURCE_DIR}/iconv-detect.c\"" _correct_iconv)
+file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/" _binary_dir_with_separator)
+CHECK_C_SOURCE_RUNS("#define ICONV_DETECT_BUILD_DIR \"${_binary_dir_with_separator}\"
+		     #include \"${CMAKE_SOURCE_DIR}/iconv-detect.c\"" _correct_iconv)
+unset(_binary_dir_with_separator)
 unset(CMAKE_REQUIRED_LIBRARIES)
 
 if(NOT _correct_iconv)
diff --git a/iconv-detect.c b/iconv-detect.c
index bc7d872a1..e44ad94e3 100644
--- a/iconv-detect.c
+++ b/iconv-detect.c
@@ -128,7 +128,7 @@ main (int argc,
 	if (!test_iconv ())
 		return 1;
 
-	fp = fopen ("iconv-detect.h", "w");
+	fp = fopen (ICONV_DETECT_BUILD_DIR "iconv-detect.h", "w");
 	if (fp == NULL)
 		return 255;
 
