Home > Net >  Working Directory changes when program starts / How to get program files dir
Working Directory changes when program starts / How to get program files dir

Time:03-17

In my c program (I'm following the Vulkan helloTriangle guide), when I run the executable, the working directory auto-defaults to /Users/foo, even though my file is in /Users/foo/Desktop/Game/src. I've tried deleting all of the code in the main file (just in case it was a glfw problem), but that didn't fix the issue.

Edit: It doesn't default to /Users/foo, when I run the program It shows Game as the folder in Terminal, but then it changes to foo.

I'm currently using very absolute paths, such as /Users/foo/Desktop/Game/src/bar, however, I don't want the code to break through standard movement of files.

I also don't run directly in VSCode, I run my Makefile in terminal.

Is this a VSCode launch.json problem or something else? I have not created or edited a settings or launch file.

Makefile

CC = g  
# -g                            debug
# -Wall                         compiler warnings
# -mmacosx-version-min=12.0     deal with it macos error
# -fdiagnostics-color           colored debug
CFLAGS = -fdiagnostics-color=always -mmacosx-version-min=12.0 -g -Wall
CFLAGS  = -std=c  17 -lglfw -framework Cocoa -framework OpenGL -framework IOKit -lvulkan
S = shaders/

TARGET = Game

.PHONY: all clean shades

.SILENT:
# other files go before -o
all: main.cpp
    $(CC) $(CFLAGS) main.cpp -o $(TARGET)

clean:
    $(RM) $(TARGET)
    $(RM) -r $(TARGET).dSYM

shades:
    glslc $(S)shader.vert -o $(S)vert.spv
    glslc $(S)shader.frag -o $(S)frag.spv

Side question: When I finish this project and I want good absolute paths, how do I locate the program files directory in c ? On windows it's C:\ ... \Program Files, and on macos it's ... / Aplication Support (At least according to Minecraft). Is there a library to handle default writable file locations in c ?

CodePudding user response:

when I run the executable, the working directory auto-defaults to /Users/foo, even though my file is in /Users/foo/Desktop/Game/src

You can't rely on the working directory being any particular value at runtime. If you want to access files that are relative to your program's installation folder, then you need to retrieve your program's current location at runtime (for instance, on Windows, you can use GetModuleFileName(NULL)), and then manipulate that path as needed (drop/change the file name, append subfolders, etc).

how do I locate the program files directory in c ?

Ask the OS. For instance, on Windows, use SHGetFolderPath(CSIDL_PROGRAM_FILES) or SHGetKnownFolderPath(FOLDERID_ProgramFiles).

Is there a library to handle default writable file locations in c ?

Not one that is built-in to the C standard library, no.

CodePudding user response:

Two options, one while debugging, one "good" solution.

While debugging (if you don't want the "good" solution) you can get the path to your source file by including __FILENAME__ and the chdir to whatever relative directory you want to go from there. Note: This is not suitable for releasing your program to others.

   std::string mysourcefile(__FILENAME__);
   _chdir(mysourcefile "/../testdir");

This is a useful little trick during debugging.


But for the good option you can use boost::dll::program_location();

   boost::filesystem::path path_to_exe=boost::dll::program_location();

This will give you the full name and path of the executable in a portable way. From there you can use boost::filesystem::path::parent and similar methods to get the path (without the name of the executable) and change directories.

  • Related