Change current working directory VS13? - c++

As stated in this post the working directory when I debug my SDL program is relative to the .vcproj instead of the .exe (which it should be IMO)
So I'm wondering if there's anyway I can change this, so when i press F5 the path will be relative to the .exe and not .vcproj?
The current (relatively easy) workaround I'm using, is simply opening up the folder and starting program from there, but I would much rather prefer simply pressing F5.

The naive answer to your question is that you can set the Working Directory option in the Debugging configuration properties to $(TargetDir). The default setting is $(ProjectDir) and by default the project directory is not where the executable file is output. However, I do not recommend you take this option, as I explain below.
You are attempting to solve this the wrong way. Your fundamental problem is that you are assuming that the working directory is the same as directory which contains the executable file. There's no reason for that to be so, and you should not rely one it. You know that the files are in the same directory as the executable and so you should look there, rather than the working directory which is only sometimes, coincidentally, the same as the executable directory.
So, instead of relying on the invoker of the process setting up the working directory to your liking, make your program independent of the working directory. You state that you wish to work with files whose location you know relative to the executable directory. So there is the answer. Construct full paths to your files, using the directory which contains the executable as the base.
If you need to find out the location of the executable, call GetModuleFileName(), and strip off the file name. What you have left is the directory which contains your executable. Combine that with the relative path of your files and your code is now independent from the working directory.

Related

.exe not opening input file

I'm coding on CLion and made this log in function. The tests are being made through the .exe because CLion's Terminal sometimes jacks up the I/O's. The problem is my .exe is not finding the files I'm specifying. It runs properly through the CLion terminal, but when shifting to the .exe it doesn't.
I've read that putting those files in the cmake-build-debug/ directory fixes the issue - and it does. Thing is, this is a group project, and by putting those files in that directory I'll constantly run into compatibility troubles when pulling from git - .cmake-build-debug would have to be pushed, thus i'd have to reload it every time. This doesn't seem very proper to me.
The other option is to put the .exe file and required .dll's in the main directory. Again, would have to update this file every single time i build the project, which also isn't a very practical solution.
So I'm asking for some help regarding what can I do to ensure my .exe searches for files in the main directory, not just on the cmake-build-debug directory. The directories are included in CMakeLists, and the .exe still doesn't find them. This is quite the issue. The project will also include some rudimental form of database, so file handling will be important. Would be nice to be able to code and build without having to manually change stuff around every single time.

Cmake: How to refer to files within an object file that is linked to multiple programs?

I am creating some software with C++ and Cmake that I want people to be able to effortlessly build and run. Cloning the GitHub repo will install the folder Project/, and the code in the file Project/src/navigation/camera/image.cpp compiled into and linked to multiple programs all over the Project repository. However, inside image.cpp there is a path to a file Project/Models/model.txt, and the file path is relative to Project/build/navigation/camera/image.o:
image.cpp:
int processImage() {
read_file("../../../Models/model.txt");
// Do something
}
But since the object file is linked to other programs all over the project, the path should be relative to many different locations. What is the standard "Software Engineering" technique to solve this? Do you tell Cmake the path of Project/, and somehow let it modify image.cpp before building? Or is there a way to still use relative paths?
If you are using CMake, the typical build model separates the source tree from the build tree, which means that your build folder could be anywhere relatively to the source folder. Therefore, any relative path wouldn't work reliably.
If I can't avoid having an hardcoded path in the source, my favourite solution is to pass your cpp file to the configure function of CMake to replace that relative path to an absolute path that CMake will calculate at generation time
I don't know how "standard" this approach is, but what I would expect is a requirement that Models be a subdirectory of whatever directory the executable is executed from. Usually this is wherever the executable ends up, but not necessarily. For released projects, this directory is usually (expected to be) the installation directory. There is a caveat that other functions are capable of changing the current working directory, and that would make it more difficult for your code to find model.txt. So I would also expect a requirement that the current working directory be restored before your code is run.
If you go this route, the relative path to your data file would be Models/model.txt. It would be up to each project to copy this data to the appropriate directory (or create a link from the directory to the data file). Note that each project would probably want this configuration for release anyway since you usually should not (sometimes cannot) access the parent of your install directory.

How does working directory work?

In Visual Studio, you are provided the option to change the working directory. I understand how to use it, but I do not know how it works.
How does the working directory setting work in relation to the complication of the application?
For example, if a program tries to open a file using relative directories (like e.g. "dir\\file.ext") then that relative directory is from the programs working directory. The working directory doesn't have to be the directory where the program is located, and the working directory can be changed from inside the program with e.g. the SetCurrentDirectory function.
Another example: Lets say you are starting a program from a command prompt window, and you are in the directory C:\some\folder\. The program is located in D:\other\folder\. Since the current directory when you start the program is C:\some\folder\, the programs working directory will be that as well.

How to specify the directory of a .config file in C++ code?

I have a C++ program that I built using Visual Stuidos. It has to read some variables from a .config file. I notice that when I run the program from VS (i.e. I open the VS solution and press the "Start Without Debugging" button), it can't find the .config file unless it's in the "release" or "debug" directory. However, I'd like to be able to run the program from the VS solution, while keeping the .config file in the same directory as the .sln file. Is there a way I can set the path of the .config file to this directory, perhaps somewhere in the source code?
Thanks.
I would take advantage of Visual Studios directory macros and set an additional include path in your solution directory, I answered a question previously on this site relating to include directories, so here is a diagram I created:
bigger link here
Don't pay attention to the text on the left side, but the black text on the right side is what you're looking for. $(SolutionDir) is a macro that finds the directory of your solution, and adding this will allow your program to have access to this directory during the program.
You can call GetModuleFileName to get the full path to your exe file. From there you can remove the lowest level (/debug or /release) to get the project path.
That should get you what you asked for, but what you asked for is not a good approach. Putting your config file in your exe directory will encounter problems later on with non-admin user rights. The recommended place for such files can be found by calling SHGetFolderPath, where you can create an accessible folder for your app.
However, I'd like to be able to run the program from the VS solution,
while keeping the .config file in the same directory as the .sln
file.
Just set Project Properties|Debugging|Working Directory to $(SolutionDir). Or any directory you want.

Where do data files go so the Microsoft Visual C++ 2008 debugger can find them?

I am writing code that opens an istream object on a file specified by the user. I want to be able to run the program in the debugger and just type the filename (eg data.txt) at the prompt, not the whole path. I haven't worked out how to do this inside the IDE so I have been saving my .txt file to the debug folder and running the .exe file, but that means I can't step through the program. How do I make it work inside the IDE instead? Thanks.
you can set the working path of the executable (project properties->Debugging->Working Directory), which leads the debugger to start the executable with that path as working directory. This has the advantage that if you set the same path for all your configurations (Debug/Release/...), you only need 1 data.txt on your entire system, which is especially nice if you want to change data.txt or it's name.
I am not sure I understand exactly the problem - is it that your data file is part of the project, but is not in the executable folder when you access it, or is it that the datafile is at another location? If the former, and the data file is part of the project, right-click on the file, and set the Build Action property to "Content". That way, it will get copied to the bin/debug folder where the executable runs when you debug.