# Copy data files to build dir and avoid Debug/Release folder when using MSVC

**URL:** <https://discourse.cmake.org/t/copy-data-files-to-build-dir-and-avoid-debug-release-folder-when-using-msvc/2632>\
**Category:** Code\
**Tags:** comp:msvc\
**Created:** [January 26, 2021, 10:34am UTC](https://discourse.cmake.org/t/copy-data-files-to-build-dir-and-avoid-debug-release-folder-when-using-msvc/2632 "2021-01-26T10:34:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [January 26, 2021, 10:34am UTC](https://discourse.cmake.org/t/copy-data-files-to-build-dir-and-avoid-debug-release-folder-when-using-msvc/2632/1 "2021-01-26T10:34:58Z")

</div>

Hi,

In my tests (I use google test) I use some external **my\_file.txt**. Thus I need to put this **my\_file.txt** to the build dir.  
[It is known that MSVC creates additional config (Debug/Release) folder](https://stackoverflow.com/questions/7747857/in-cmake-how-do-i-work-around-the-debug-and-release-directories-visual-studio-2) and other compilers don’t.

I ve tried to remove these additional config folders in the same way as it was proposed in the link above:

```cmake
# First for the generic no-config case (e.g. with mingw)
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${youroutputdirectory} )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${youroutputdirectory} )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${youroutputdirectory} )
# Second, for multi-config builds (e.g. msvc)
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
    string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
    set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory} )
    set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory} )
    set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory} )
endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )

```

It works for my own project but google test (wich is a subproject) creates `Debug` folder and put there its **.moc** -files like in the picture:

 ![image](https://discourse.cmake.org/uploads/default/original/2X/8/890e0b0c8e5c806fbfd620f5d139c0e5baf0ac95.png)

If someone has idea how to make this approach crosscompile please share.  
For now I copy my file (folder actually) with command:

```cmake
file(COPY "data"
  DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

```

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [January 26, 2021, 2:21pm UTC](https://discourse.cmake.org/t/copy-data-files-to-build-dir-and-avoid-debug-release-folder-when-using-msvc/2632/2 "2021-01-26T14:21:40Z")

</div>

This has nothing to do with MSVC or the compiler in use. It is completely controlled by the generator in use. If you don’t want these directories, don’t use a multi-config generator (Visual Studio or Xcode). They are there to avoid clobbering each other when building in different configurations.

---

<div class="post-metadata">

**Author:** ![markdewit\_ies](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@markdewit\_ies](https://discourse.cmake.org/u/markdewit_ies)\
**Post date:** [February 2, 2021, 11:03am UTC](https://discourse.cmake.org/t/copy-data-files-to-build-dir-and-avoid-debug-release-folder-when-using-msvc/2632/3 "2021-02-02T11:03:20Z")

</div>

If you use a multi-configuration generator, and then try to flatten Debug/Release/etc into a single folder, I expect that trying to switch configurations would lead to all sorts of weird issues.

In our root cmake, we check if we’re multi- or single-configuration and set a (suitably escaped) variable:

```auto
get_property(MULTI_PROJ_GENERATOR GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if (NOT MULTI_PROJ_GENERATOR)
  # default to a release build if no configuraton was set
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Default build configuration")
  set(INSTALL_DIR "\${CMAKE_INSTALL_CONFIG_NAME}")
else()
  set(INSTALL_DIR ".")
endif()

```

now you can write your install statements using that variable, and they work for single- and multi-config generators, I think that even cpack is ok with this approach…

```auto
install(
  DIRECTORY "${CMAKE_SOURCE_DIR}/data/stuff"
  DESTINATION ${INSTALL_DIR}/data
  )

```

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [February 2, 2021, 12:50pm UTC](https://discourse.cmake.org/t/copy-data-files-to-build-dir-and-avoid-debug-release-folder-when-using-msvc/2632/4 "2021-02-02T12:50:42Z")

</div>

Thank you, I’m going to try
