How to debug "LoadLibraryA() failed"?

Hi,

I'm using c++ to develop (and by extension, C), and I am attempting to add FLECS library to the game. Well, FLECS isn't exactly designed for embedded but I somehow was able to compile it after changing/removing some modules, however the resulting dll simply causes "LoadLibraryA() failed", and I have no clue what would be the cause... Can anyone provide some insight?

FLECS does attempt to include some system headers that might not be available on playdate, but that's just my guess.

What is the error code that is being logged? Searching for that is a good place to start.

Ah so the number was an error code then! Didn't realise that. It's 126 so the library is expecting some system/os dll I suppose. Whelp, it was FLECS having both STATIC and SHARED flags turned on for some reason and end up requiring to link against flecs.dll while that dll was never produced.

Hmm, despite being able to run in the simulator now, the game would instantly crash on the real device... This time I really don't have any debug info.

Here are the possibly problematic includes in the library, any of them I should remove?

  • stdio.h
  • stddef.h
  • ctype.h
  • time.h
  • assert.h
  • stdarg.h
  • string.h
  • stdint.h
  • stdbool.h
  • stdlib.h
  • math.h
  • errno.h

Or perhaps its printf and stdout? (literally dumping my suspicion)

It sounds like you still have some linking issues; standard lib doesn't exist on device while it exists on the host computer so it will look like code is building correctly but in reality it isn't. There are posts around here that talk about how to resolve these types of issues.

You can't use stdio.h, time.h, or errno.h on the Playdate device since it doesn't have a libc to link against. You'll have to use the PlaydateAPI struct pointer to write replacements for the functions you need.

Oh! Also, pthread doesn't exist on device since all C code (except callbacks) runs in the same FreeRTOS task.

Example: playdate->system->logToConsole is basically a drop-in replacement for printf.

Update: after fiddling with the code for days I still can't make it run on the device. I've removed every time.h, errno.h, stdio.h and most of the stdlib.h as suggested, and replaced them with corresponding playdate API... No luck. Dependency checker doesn't help either.

I didn't removed all the stdio functions because... some of them does work? like atoll, memcpy, memset. So I'm really in the dark now. Oh well, now I have removed all of the #include <stdlib.h> from the library and it still crashes. I'm out of clue

Do you have a crashlog.txt file in the device data disk? If so, you might want to consider posting it here for someone to take a look at. It'll give a bit of info about where the crash occurred.

here it is...

--- crash at 2025/02/26 15:59:43---
build:57176cb0-2.6.2-release.177516-buildbot
   r0:00000000    r1:00000000     r2:00000000    r3: 00000000
  r12:00000000    lr:00000000     pc:00000000   psr: 00000000
 cfsr:00000092  hfsr:00000000  mmfar:1ffffb2c  bfar: 1ffffb2c
rcccsr:00000000
heap allocated: 424992
Lua totalbytes=0 GCdebt=0 GCestimate=0 stacksize=0

Update: I was able to somewhat isolate the problem. The flecs_bootstrap(world) call below will crash the system, comment it out then it won't crash.

    //... lots of other stuff
    ecs_set_stage_count(world, 1);
    ecs_default_lookup_path[0] = EcsFlecsCore;
    ecs_set_lookup_path(world, ecs_default_lookup_path);
    flecs_init_store(world);
    
    flecs_bootstrap(world);

    world->flags &= ~EcsWorldInit;

    ecs_trace("world ready!");
    ecs_log_pop();

    return world;

However, I encountered a peculiar issue when trying to identify which part of the flecs_bootstrap caused an issue. I tried to use goto to skip parts of the code but eventually it turned out... that it didn't work.

The following will still crash the system despite it should do nothing except for a jump:

void flecs_bootstrap(ecs_world_t* world){
    return;
    //... rest of the function body
}

In contrast, the following will not crash the system (the whole function body deleted and replaced with just a return;) :

void flecs_bootstrap(ecs_world_t* world){
    return;
}

Which... I suspect is stack corruption?

Trimming the code line by line and I arrived at a spot where it actually outputs a message:

Run loop stalled for more than 10 seconds pc=0x240323A2 lr=0x240323A1