0

We are working on a c++ project using CMake. In the project we are referencing some resource files like resources/file.txt (located directly inside the project folder) using the std::ifstream class. The problem now is that, while our other team members can reference these files as they use Linux, I cannot (I am using Visual Studio 2017).

How can I set up CMake or Visual Studio so I can reference these files without changing their paths? I am still a pretty big novice using CMake but I didn't find anything that worked.

EDIT:
Minimal code showing the issue:

#include <iostream>
#include <fstream>
#include <string>

std::string read_file(std::string filename)
{
    std::string result = "";

    std::ifstream file(filename);
    if (!file.is_open())
    {
        std::cout << "Could not open file " << filename.c_str() << "." << std::endl;
        return result;
    }

    std::string line;
    while (getline(file, line))
        result += line + '\n';
    return result;
}

int main(int argc, char *argv[])
{
    std::string fileContent = read_file("src/Test_File.txt");

    if (fileContent.compare(""))
    {
        std::cout << fileContent.c_str() << std::endl;
    }

    return 0;
}

This program should load the "Test_File.txt" located inside the src folder with the folder structure being:

  • src
    • main.cpp
    • CMakeList.txt
    • Test_File.txt
  • CMakeList.txt

The problem here is that the program cannot find the Text_File.txt while the other team members don't have such issue. Absolute paths of course work but they are obviously not the way to go.

I've already tried to set the working directory using the VS_DEBUGGER_WORKING_DIRECTORY parameter with set_target_properties(${PROJECT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") after add_executable(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp") but that does not seem to work either.

MrTheMake
  • 3
  • 1
  • 3
  • `The problem now is that, while our other team members can reference these files as they use Linux, I cannot (I am using Visual Studio 2017).` - **Why** you cannot access these files? Most likely the reason is **different location** of the executable, or **different working directory** (directory, from which you run executable). It depends on your executable's code, but you don't show it. – Tsyvarev Nov 11 '17 at 00:04
  • I already tried set a different working directory although I may have done it incorrectly. What code do you need exactly? Thank you. – MrTheMake Nov 11 '17 at 00:33
  • `What code do you need exactly?` - Code of your executable (C++ file), in form of [mcve]. `I already tried set a different working directory although I may have done it incorrectly.` - **How exactly** did you try to set it? You want us (Stack Overflow community) to help you, but provide so little information, so even the problem isn't clear. – Tsyvarev Nov 11 '17 at 12:09
  • I have now updated the question. Thank you. – MrTheMake Nov 11 '17 at 13:54
  • Well, the question becomes clearer. So you want to set working directory for an executable when run it in Visual Studio 2017. Have you seen [that question](https://stackoverflow.com/questions/41864259/how-to-set-working-directory-for-visual-studio-2017-rc-cmake-project), about very similar things? Which CMake version do you use? And which version do you pass to `cmake_minimum_required` command at the beginning of your `CMakeLists.txt`? – Tsyvarev Nov 11 '17 at 16:43
  • I have seen that question, yes. I tried setting the "currentDir" parameter in the `launch.vs.json` file to the folder the project is located in but that didn't work. As described, I also tried setting the `VS_DEBUGGER_WORKING_DIRECTORY` parameter without any success. I am using the integrated CMake of VS 15.4 which should be 3.9. `cmake_minimum_required` is set to `VERSION 3.8`. – MrTheMake Nov 11 '17 at 16:52

1 Answers1

0

After some more tries I have got it working thanks to @Tsyvarev. As states in this question they brought up, setting "currentDir" worked. For me it didn't work before because "name" and "projectTarget" were improperly set.

I now added "currentDir": "${workspaceRoot}" after fixing the "name" and "projectTarget" values and it worked.

MrTheMake
  • 3
  • 1
  • 3