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.
Related
I created a C++ project in Xcode which successfully builds and runs a program in the Xcode environment. This program is dependent on existing in its current working directory to access several files. Outside of the Xcode environment, when I double-click on the executable file, I get a message that notifies me that the terminal exits the current working directory.
The current working directory exiting twice
[1]: https://i.stack.imgur.com/EZ5kc.png
After the exit messages, my current working directory results in /Users/chefjeff/
Judging by this other post: How to change default working directory on Mac OS X?
There might not be an easy solution to double-clicking an executable so that it opens in its current directory, but I still need a solution to using the GUI on Mac in some sort of way to open the file in its working directory.
EDIT:
The project folder, CultGame, must be able to exist at any location in the user's filesystem and still have its packaged files and executable files work.
So here's how I solved this:
I couldn't change the default directory that double-clicking on the program to run it would start up on.
I can use the chdir() function to move around directories. I moved to the root directory using chdir() after the program began and started me in Users/Username. More on chdir here: https://www.geeksforgeeks.org/chdir-in-c-language-with-examples/
I used _NSGetExecutablePath() to get the directory that the currently executing file is located in. More on that here: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html
From root, I used chdir() to navigate to the path of the currently executing file which I obtained with _NSGetExecutablePath().
I will update my answer in a bit with a code example. Hopefully this helps!
I've got an a project (native C++ compiled to .exe) that runs fine outside of VS 2010, but inside of VS (with or without the debugger) it gets stuck trying to find a text file located in the same bin folder as it.
Any ideas as to why this would happen? My hunch is that VS messes with where the code looks when trying to open a file, but I don't know enough details to correct this.
Some details:
My .exe calls a function from a .dll I wrote earlier, which in turn tries to find a text file specifying that function's parameters. For convenience I've placed all of these files in the same folder, so finding the text file wouldn't be a problem.
Before opening the file, the function checks that it exists using:
PersistentAssert(Utility::FileExists(Filename), "Parameter file not found");
In VS 2010 this line causes "Parameter file not found" to display, but outside of VS the program manages to find the file.
When launching an executable from visual studio by default it uses the project file directory as the current working directory. You can change it in Configuration Properties -> Debugging -> Working Directory.
However I don't think it's correct behavior to search your programs datafiles etc from the current working directory. Instead your program should find out the directory of the executable and find the files in that directory.
Processes have the concept of a "current directory", which may or may not be the same directory as where the .exe file is located. It sounds like when you run your program inside VS, the current directory is something other than where your .exe is.
Somewhere in the project settings, there should be a place where you can choose what directory will be the current directory when your program starts inside the debugger. Set that to the same location as your .exe and you should be good.
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.
I just installed MinGW using the automatic installer MinGW-get-inst that I found on their website. I am using eclipse to write my C++ programs. My code compiles fine, and I get a .exe file. However, when I try to open this executable, I get the error that libgcc_s_dw2-1.dll is missing from my computer. I have located this file under MinGW\bin so I know it exists.
This is for all C/C++ programs, I am testing with a simple hello-world program. Any fixes?
Thanks
You need to ensure that the MinGW\bin directory is on the path from where you are running application.
If you were running the resulting executable, say a.exe for example, from a CMD window do the following to check that MinGW\bin is on the path.
set PATH
At this point you will see the current value for the command path. Make sure the MinGW\bin directory is in it somewhere.
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.