Handmade Hero » Forums » Code » Day 21 Dynamic Linking on Linux
Jvac
Josh Ayres
6 posts

High School student who likes programming

#13011 Day 21 Dynamic Linking on Linux
2 weeks, 5 days ago

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!
mmozeiko
Mārtiņš Možeiko
1477 posts
1 project
#13012 Day 21 Dynamic Linking on Linux
2 weeks, 5 days ago Edited by Mārtiņš Možeiko on Sept. 4, 2017, 2:20 a.m.

How did you create handmade.dll file?
Jvac
Josh Ayres
6 posts

High School student who likes programming

#13013 Day 21 Dynamic Linking on Linux
2 weeks, 5 days ago

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

mmozeiko
Mārtiņš Možeiko
1477 posts
1 project
#13014 Day 21 Dynamic Linking on Linux
2 weeks, 5 days ago Edited by Mārtiņš Možeiko on Sept. 4, 2017, 3:15 a.m.

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.
Jvac
Josh Ayres
6 posts

High School student who likes programming

#13015 Day 21 Dynamic Linking on Linux
2 weeks, 5 days ago

So it is saying that the -shared is not being used during compilation.
Jvac
Josh Ayres
6 posts

High School student who likes programming

#13016 Day 21 Dynamic Linking on Linux
2 weeks, 5 days ago

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
mmozeiko
Mārtiņš Možeiko
1477 posts
1 project
#13017 Day 21 Dynamic Linking on Linux
2 weeks, 5 days ago Edited by Mārtiņš Možeiko on Sept. 4, 2017, 4:15 a.m.

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
Jvac
Josh Ayres
6 posts

High School student who likes programming

#13025 Day 21 Dynamic Linking on Linux
2 weeks, 5 days ago

Thanks for all of your help!