0
root/
├─ bin/
│  ├─ Release/
│  │  ├─ executable
│  ├─ Debug/
│  │  ├─ executable
├─ Database/
│  ├─ Generated_Databases/
│  │  ├─ XXX

This is how my project looks like (CMakeLists.txt is in /root). I want to access XXX in my project. When I do "./Database/Generated_Databases/XXX" on Linux (Qt Creator is the IDE I'm using) it works, but when I do the same on Windows (Visual Studio) it doesnt.

I did std::cout << std::filesystem::current_path() << '\n'; on my windows machine and the output was the current path of the executable and not the /root, which means the working directory on Windows & VS is the executable path. How can I ensure that the working directory will always be /root instead?

Here's how CMakeFiles.txt looks:

cmake_minimum_required(VERSION 3.5)

project(RubiksCubeSolver)

set(SRC "${CMAKE_SOURCE_DIR}/main.cpp"
      .....
      .....

add_executable(RubiksCubeSolver ${SRC})

# The relative path of the output will be /root *** DOESN'T WORK ***
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR})

# I realise now that specifying the binary output location is actually irrelevant to the problem (?)
set_target_properties(RubiksCubeSolver PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/Debug/")
set_target_properties(RubiksCubeSolver PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/Release/")

# C++20 required for rotl/rotr
set_property(TARGET RubiksCubeSolver PROPERTY CXX_STANDARD 20)
set_property(TARGET RubiksCubeSolver PROPERTY CXX_STANDARD_REQUIRED ON)

# SDL2
#    Linux
if(UNIX)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
target_link_libraries(RubiksCubeSolver ${SDL2_LIBRARIES})
endif()
#    Windows
if(WIN32)
# Change to your local SDL2 folder path
set(SDL2_DIR "D:/.................") #its like this because there are issues with SDL2 and windows
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
target_link_libraries(RubiksCubeSolver ${SDL2_LIBRARIES})
endif()
itaysadeh
  • 11
  • 1
  • If I understood the question correctly you're running the executable from the Visual Studio debugger? Then setting the [VS_DEBUGGER_WORKING_DIRECTORY](https://cmake.org/cmake/help/latest/prop_tgt/VS_DEBUGGER_WORKING_DIRECTORY.html) to `${CMAKE_SOURCE_DIR}` should work if you use the Visual Studio generator. If you use the Ninja generator and the CMake integration of Visual Studio 2019 then you probably will need to create a [launch.vs.json](https://learn.microsoft.com/en-us/cpp/build/launch-vs-schema-reference-cpp?view=msvc-160) file with the proper working directory. – Corristo Mar 02 '21 at 21:26
  • @Corristo I added `set(VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})` and built the project through CMake GUI with the Visual Studio 2019 generator and it didn't work - I still get the executable path instead of /root. Is that what you meant? – itaysadeh Mar 02 '21 at 21:53
  • `VS_DEBUGGER_WORKING_DIRECTORY` is not a CMake variable, but a target property, so you'll need to use `set_target_properties( PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")`. If you have multiple executable targets then you can also list all of them before the `PROPERTIES` "keyword" in `set_target_properties`. – Corristo Mar 02 '21 at 21:56
  • @Corristo Thank you! I've been stuck on this for 2 days, I really appreciate your help – itaysadeh Mar 02 '21 at 22:09

0 Answers0