- Feedback Requested
- Symptoms
- The problem
- Situation: Visual Studio with Visual C++ compiler (cl.exe)
- Situation: Visual Studio with Clang compiler (clang-cl.exe)
This is living documentation. If you discover extra scenarios or better solutions, please contribute back via bug reports or pull requests. Thank you.
Running tests gives output such as the following:
************************************************************************************
* *
* Welcome to Approval Tests.
*
* There seems to be a problem with your build configuration.
* We cannot find the test source file at:
* ../../../tests/Catch1_Tests/ApprovalsTests.cpp
*
* For details on how to fix this, please visit:
* https://github.com/approvals/ApprovalTests.cpp/blob/master/doc/TroubleshootingMisconfiguredBuild.md
* *
************************************************************************************
Approval Tests depends on the test framework to provide access to the source file of the test being run.
In many cases, this is implementing using __FILE__
.
With some build configurations, we have found that the path contained in __FILE__
contains either just the file name, or contains an incorrect relative path to a non-existent directory, relative to the current working directory of the test program.
We think this may be associated with Visual Studio 2019's change to make Ninja the default generator.
Use /FC
to make Visual Studio emit the full path in diagnostics, and __FILE__
(documentation).
You need to add a line like the following to your CMakeLists.txt
file:
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(my_program_name PUBLIC /FC)
endif()
Or this:
target_compile_options(my_program_name PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/FC>)
We have not been able to find a compiler flag that makes clang-cl put full paths in __FILE__
.
The only solution we have found is to put your build outputs in a directory outside the source tree, so that the build will use absolute paths.
One way to do this is to edit your CMakeSettings.json
file, and change all pairs of lines like this:
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
To something like this (where you change MyProjectName
to the actual name of your project):
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\MyProjectName\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\MyProjectName\\install\\${name}",
This would put the build outputs in to:
C:\Users\YourUserName\CMakeBuilds\MyProjectName\build