Anyway the Clion can set the working directory as the current project directory?
Answered
Thought the empty working directory means by default the current project directory. But, seems it is NOT. Try to access ..\TestData\..., not successful. Visual studio has $(ProjectDir) and I am wondering if Clion has such as well. Thanks!
Please sign in to leave a comment.
Hi! Do you want to change the project root directory? You can do it using Tools | CMake | Change Project Root. Here is the related article in our web-help: https://www.jetbrains.com/help/clion/changing-project-root-directory.html.
Not sure if the root directory is equivalent to the active directory or the working directory. it is said by default the root directory is the directory where the CMake file is at. But when I reference a file by ..\TestData\.., it couldn't find the file though the file is located at the relative path "..\TestData\.." fromo the so called project root directory.
Patrick, please send the simple sample project illustrating the issue to clion-support at jetbrains.com.
This question applies universally to all projects. In Visual studio, I can set the $(ProjectDir) as the working directory. Anything similar in CLion?
As I see on MSDN, $(ProjectDir) means the directory of the project. So it looks like a synonym to the project root directory in CLion.
Actually, I assume that the reason of the issue you've faced is connected not to the project directory, but to the fact that CLion successfully understands Unix-style paths:
And doesn't understand Windows paths:
Here is the related issue in our tracker: https://youtrack.jetbrains.com/issue/CPP-9820. Feel free to comment or upvote, follow it to get updates.
Here is my observation.
I tried using
The fact is that I have to add one more ..\\ than it shall be from project root directory. My suspect is that the .exe file is built in the cmake-build-debug directory. So the current active directory is from root_directory\cmake-build-debug.
#include may not be the same as this std::ifstream usage which is opening a file at execution time NOT compile time.
I have the same problem. Trying to be clearer about what's wanted,
Consider the popup you get if you click on the current configuration and select "Edit Config". This popup is titled "Run/Debug Configurations". On this popup is a field labeled "Working Directory". The question is, what do I type in that field such that my executable will be run with the project root directory as the working directory? Precisely and literally?
As Patrick noted, for MSVC I would enter "$(ProjectDir)". I tried "${CMAKE_HOME_DIRECTORY}" but it was not expanded. For this particular project, the absolute path of the project directory is "/home/amc/git/libswoc". If I put that in the "Working Directory" field, it works as expected. But I do not want to use absolute paths, I want to use CLion's knowledge of where the project is. Currently I use ".." which works, but seems a bit fragile.
As an example of where this matters, I have some code that opens a file. In the test logic I need a file to open, so I open the test source file using a relative path (which is part of the test). I want to use the project relative path, "src/unit_tests/test_file.cc". This is affected by the working directory in the debug configuration but not the project root directory and setting the latter will not fix the problem.
Also, here's the project on github if you want a look at an example. The actual path to the test code is "src/unit_tests/test_swoc_file.cc".
> The question is, what do I type in that field such that my executable will be run with the project root directory as the working directory? Precisely and literally?
In this case you can leave the "Working Directory" field empty.
Note that it's possible to use the $PROJECT_DIR$ macro in this field ($PROJECT_DIR $ is your project directory where .idea project configuration folder is located).
Also note that working directory is actually saved relative to the project root directory, like WORKING_DIR="file://$PROJECT_DIR$/smth" though it looks as absolute path in UI. You can check that in workspace.xml located in the .idea subfolder of the project folder.
Looks like in remote development project things are a bit different. Let's say I have file `foo/bar.xml` in my project (it's actually checked into git). I now need to run my executable with that file as argument.
Here is what I would do to run it manually: run from root of the project, specify relative paths to the executable and to teh file:
cmake-build-debug/bar-consumer foo/bar.xml
When I simply specify foo/bar.xml as command line arg in run configuration, the run fails - it cannot find the file. But when I also put the $PROJECT_DIR$ into Working Directory, things work.
I am not sure if this is a bug or something else.
@Abelousov8, thanks for the notes!
I wasn't completely right in my previous post. By default the working directory is the directory of the executable.
And the $PROJECT_DIR$ variable can be used in the "Working Directory" field since CLion 2018.3 - https://youtrack.jetbrains.net/issue/IDEA-74031.