Mac Executable Difficulty

I have been working on a game using SDL2 and the Soloud audio library using VSCode with clang as my compiler for mac.

I have included the source code of the soloud library in my program rather than linking to a .dylib file which is how I linked for SDL2.

when I run the application from the terminal using ./main everything runs as expected.

however, when I try to run the game by double-clicking the executable, the program works but with no sound. This leads me to believe that I am not linking the source code of the soloud library correctly. please advise, my tasks.json file with my compiler args is as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-DWITH_SDL2",
                "-stdlib=libc++",
                "-g",
                "${workspaceFolder}/*.cpp",
                "${workspaceFolder}/soloud/*.cpp",
                "${workspaceFolder}/soloud/*.c",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-lSDL2",
                "-lSDL2main",
                "-lSDL2_image",
                "-lSDL2_ttf",
                "-I /usr/local/Cellar/sdl2/2.0.12/lib",
                "-I /usr/local/Cellar/sdl2_image/2.0.5/lib",
                "-I /usr/local/Cellar/sdl2_ttf/2.0.15/lib"
            ],
            "options": {
                "cwd": "${workspaceFolder}",
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}


I have all the source code files for soloud in its own /soloud directory within my working directory.
Last edited on
When your program starts, having it print its wording directory.
This would be system("pwd"); on *nix, and system("cd"); on Windows.

Is it different when launching it via ./main and launching it by double-clicking?

If some audio is not working, I assume that means it can't load an audio file correctly. I suggest some print statements to say whether or not file X is successfully loaded.
Last edited on
ah interesting, yes when I run the program from terminal, system("pwd"); returns
"/Users/myname/CppProjects/Game"

but when double-clicked it returns
"/Users/myname"

so when it's double-clicked the working directory is wrong, how can I fix this? Ideally I want the working directory to be whatever directory the executable is in, is that possible?
Last edited on
That I'm not sure of. Maybe someone else can answer.

But this StackExchange post might help you: https://apple.stackexchange.com/questions/179063/wrong-working-directory-if-bash-script-is-opened-via-double-click
That's Eclipse though, so it might not apply here.
Last edited on
Thanks a lot for your help, I have managed to find a way to make this work.

using:

 
chdir( "/Users/myname/CppProjects/Game" );


before any media loading functions changes the working directory correctly. I'm not sure exactly how portable that function is but it at least works for osx.

I have actually implemented it like this
1
2
3
4
if (chdir("/Users/myname/CppProjects/Game") != 0)
{
    perror("ERROR - chdir() to /Users/myname/CppProjects/Game failed"); 
}

so that an error is returned if the function fails.

I'm not sure if it's possible to change it based on the executable's location but If I work out how to do this I will add it in another reply.
Last edited on
Topic archived. No new replies allowed.