Handmade Hero»Forums»Code
Josh Ayres
8 posts
High School student who likes programming
Day 21 Dynamic Linking on Linux
Hello, I have started to follow Handmade Hero on linux.

I have been trying to do Dynamic Loading but an getting an error:
./handmade.dll: only ET_DYN and ET_EXEC can be loaded

Here is my loading code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct sdl_game_code
{
    void *GameCodeDLL;
    game_update_and_render *UpdateAndRender;
    
    bool32 IsValid;
};

internal sdl_game_code SDLLoadGameCode(){
    sdl_game_code Result = {};
    
    Result.UpdateAndRender = GameUpdateAndRenderStub;
    
    Result.GameCodeDLL = dlopen("./handmade.dll", RTLD_LAZY);
    
    if (!Result.GameCodeDLL) {
        fputs (dlerror(), stderr);
        
    }
    
    Result.UpdateAndRender = (game_update_and_render *)dlsym(Result.GameCodeDLL, "GameUpdateAndRender");
    return Result;
}


Thanks!
Mārtiņš Možeiko
2562 posts / 2 projects
Day 21 Dynamic Linking on Linux
Edited by Mārtiņš Možeiko on
How did you create handmade.dll file?
Josh Ayres
8 posts
High School student who likes programming
Day 21 Dynamic Linking on Linux
Here is my build file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/bin/sh
cd ../bin

CommonCompilerFlags="-std=c++14 -Wextra -Wno-writable-strings -Wno-missing-field-initializers -Wno-sign-compare -Wno-unused-parameter -Wno-null-dereference -Wno-c++11-compat-deprecated-writable-strings -DHANDMADE_INTERNAL -DHANDMADE_SLOW"

LinkerFlags="-ldl `sdl2-config --cflags --libs`"

clang++ $CommonCompilerFlags -c ../src/handmade.cpp -o handmadehero.dll -g $LinkerFlags
clang++ $CommonCompilerFlags ../src/main.cpp -o handmadehero -g $LinkerFlags 

cd ../src

Mārtiņš Možeiko
2562 posts / 2 projects
Day 21 Dynamic Linking on Linux
Edited by Mārtiņš Možeiko on
You need to pass "-shared" to clang/gcc arguments when creating shared library.
So line 8 should look like this:
1
clang++ -shared $CommonCompilerFlags -c ../src/handmade.cpp -o handmadehero.dll -g

You probably shouldn't need $LinkerFlags there, because game layer shouldn't depend on SDL library.
Josh Ayres
8 posts
High School student who likes programming
Day 21 Dynamic Linking on Linux
So it is saying that the -shared is not being used during compilation.
Josh Ayres
8 posts
High School student who likes programming
Day 21 Dynamic Linking on Linux
I figured it out. I have to compile an object file before the dll
1
2
clang++ $CommonCompilerFlags -fPIC -c ../src/handmade.cpp
clang++ -shared -o handmade.dll handmade.o
Mārtiņš Možeiko
2562 posts / 2 projects
Day 21 Dynamic Linking on Linux
Edited by Mārtiņš Možeiko on
Oh, there's one more mistake. The "-c" argument. "-c" means compile only. So you were actually producing object file, not shared library. Just remove -c and you're fine. Don't use it when you want to produce final executable (main program or shared library). Having "-fPIC" (or better "-fpic") is also a good idea:

1
clang++ -shared -fpic $CommonCompilerFlags ../src/handmade.cpp -o handmadehero.dll -g
Josh Ayres
8 posts
High School student who likes programming
Day 21 Dynamic Linking on Linux
Thanks for all of your help!