VS: Using $<CONFIG> for generated sources overwrites VCXPROJ property ObjectFileName and adds the "Debug"/"Release" folder to it twice. WINDOWS_EXPORT_ALL_SYMBOLS then fails.
[Checked with CMake 3.24.1 and 3.26.4]
When adding source file whose path contains the $<CONFIG> genex, the VS (2022) generator for some reason adds an explicit <ObjectFileName> property to the sources. Moreover that path reads like "$(IntDir)/Debug/...". The VS $(IntDir) macro, however, already contains the the configuration specific path prefix. Hence the resulting object file path expands to something like ".../Debug/Debug/xyz.obj".
If the "Auto build dll exports" (WINDOWS_EXPORT_ALL_SYMBOLS property) custom build step runs afterwards, it will expect the object file at ".../Debug/xyz.obj" (output: "Couldn't open file '...build/Play_Ground.dir/Debug/ToCopy.obj' with CreateFile()"). I guess the problem is not the export symbol step, but the double configuration folder in the first place. Actually I don't see a reason to overwrite the ObjectFileName property at all, since VS will put the object file into the cfg-specific "intermediate dir" by default.
Example CMakeLists.txt:
cmake_minimum_required(VERSION 3.24.1)
set(CMAKE_CONFIGURATION_TYPES Debug Release)
project(Play CXX)
add_library(Play_Ground SHARED)
target_sources(Play_Ground PRIVATE bar.cpp ToCopy.cpp.in)
set(copy_input_file ${CMAKE_CURRENT_SOURCE_DIR}/ToCopy.cpp.in)
set(copy_output_file $<CONFIG>/ToCopy.cpp)
add_custom_command(OUTPUT ${copy_output_file}
COMMAND ${CMAKE_COMMAND} -E copy ${copy_input_file} ${copy_output_file}
MAIN_DEPENDENCY ${copy_input_file}
)
target_sources(Play_Ground PRIVATE ${copy_output_file})
set_target_properties(Play_Ground PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
Generated vcxproj excerpt:
<ItemGroup>
<ClCompile Include="C:\tmp\play\src\bar.cpp" />
<ClCompile Include="C:\tmp\play\build\Debug\ToCopy.cpp">
<ObjectFileName>$(IntDir)/Debug/ToCopy.cpp.obj</ObjectFileName>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="C:\tmp\play\build\Release\ToCopy.cpp">
<ObjectFileName>$(IntDir)/Release/ToCopy.cpp.obj</ObjectFileName>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
</ItemGroup>
Edit: Remove escaping backslashes from title.