0

I have a problem with working with files in vscode opened folder. I can't specify the relative path of the file, because it is a relative path for the compiler, but not for my project folder. For example:

//I want to write in this way
std::ofstream out("out.txt"); //*compiler's folder*\out.txt

//But have to write in this way
std::ofstream out("*full path to file in working folder*\\out.txt")

The same problem with including. Are there any ways to resolve it?

majorro
  • 61
  • 1
  • 2
  • 8
  • Maybe it's a problem in your `launch.json` related: https://stackoverflow.com/questions/38623138/vscode-how-to-set-working-directory-for-debug – drescherjm Feb 24 '21 at 21:34
  • Also [https://code.visualstudio.com/docs/cpp/launch-json-reference](https://code.visualstudio.com/docs/cpp/launch-json-reference) – drescherjm Feb 24 '21 at 21:39
  • No, the problem not in vscode, but rather in the compiler itself, configs are ok as well as linting which "see" included file by relative path, but the compiler says that the file cannot be opened. With full path in include, everything compiles without errors. But possibly, there are some compiler flags which can be added to config... – majorro Feb 24 '21 at 21:43
  • ***But possibly, there are some compiler flags which can be added to config*** No, the compiler itself does not play a role in what folder is the current working directory when you run it. This is dependent on how you are running your code. If you click on your executable from the OS file manager the current working directory should be the same folder as the executable. – drescherjm Feb 24 '21 at 21:44
  • Btw, in linux relative paths is ok, but g++ there is a "global" command, maybe I should try to add it to PATH here – majorro Feb 24 '21 at 21:46
  • Relative paths should work in windows as well. But it's relative to what the current working directory is. And that is dependent on how you are running your application. – drescherjm Feb 24 '21 at 21:48

1 Answers1

0

Ok, I found the fix by myself. g++ bin folder wasn't added to my PATH, so I added it and changed "command": "*path to g++.exe*/g++.exe" to "command": "g++" and deleted "options": { "cwd": "*path to g++ bin folder*" }, in tasks.json. Now workspace I compile from is "main" and relative paths work correctly. Thanks drescherjm for helpful thoughts.

majorro
  • 61
  • 1
  • 2
  • 8