Jamesbarford / holyc-lang

HolyC compiler
BSD 2-Clause "Simplified" License
273 stars 13 forks source link

MAlloc linking bug related to pthread #65

Open valerio-afk opened 3 weeks ago

valerio-afk commented 3 weeks ago

Hello,

First of all, thanks for your work. Finally we can all benefit from HolyC!

To get my hands dirty, I am trying to make my first program in HolyC and my typical test bench is to make a program to calculate the score in bowling games. I am using the MAlloc / Free functions and this causes the following linking error during compilation

$ hcc -run bowling.hc /usr/bin/ld: /usr/local/lib/libtos.a(all.o): in functionThreadPoolNew': (.text+0xf412): undefined reference to pthread_create' /usr/bin/ld: (.text+0xf423): undefined reference topthread_detach' /usr/bin/ld: /usr/local/lib/libtos.a(all.o): in function ThreadPoolStop': (.text+0xf54e): undefined reference topthread_join' collect2: error: ld returned 1 exit status sh: 1: ./a.out: not found`

The way I understood it was the use of MAlloc / Free to raise this error is that I commented them and also replaced them with std C malloc / free functions and in that case it seems working properly

This is the code so far

`#define FRAMES 10

class Frame { U8 first; U8 second; U16 score; };

class LastFrame : Frame { U8 third; };

U0 parse_string(U8 str,Framegame) {

}

U0 Main() { U8 *str = "625/x9/x7/8-61xx9/";

Frame game = MAlloc(sizeof(Frame)FRAMES);

Free(game);

"%s\n", str;

}`

My intuition may be wrong, but I think that the libtos.a is not linked with pthread, which I think I have it installed correctly:

$ ldconfig -p | grep pthread libpthread.so.0 (libc6,x86-64, OS ABI: Linux 3.2.0) => /lib/x86_64-linux-gnu/libpthread.so.0 libpthread.so (libc6,x86-64, OS ABI: Linux 3.2.0) => /lib/x86_64-linux-gnu/libpthread.so

I thought to act here in the CMakeFile

install(CODE "execute_process(COMMAND hcc -lib tos ${CMAKE_SOURCE_DIR}/holyc-lib/all.HC WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/holyc-lib)")

but I don't know whether hcc supports something like -lpthread

The only way I manged to get it work using MAlloc / Free function is the following (not sure why obj files are just .o and cannot give a name)

$ hcc -obj bowling.HC && mv .o bowling.o $ gcc -o bowling bowling.o -ltos -lm -lpthread

Another bug I found is that the file .hc must have an EOF at the end, meaning that the last token has to be an empty line. Otherwise, I get this error:

$ hcc -run bowling.hc ERROR: line 59: Ran out of tokens

To work it around, I just need to add a \n at EOF.

I am using WSL

Hope this issue helps your work ;)

EDIT:

Changing line 24 in main.c in #define CLIBS_BASE "-ltos -lpthread -lc -lm" seems making the trick (ie having -plthread after -ltos)

Jamesbarford commented 2 weeks ago

Thanks for raising this, I have merged in a patch with your suggestion for the ordering of the libs. If you could please pull down the latest code I'd really appreciate if you could give it a try on your end as I do not have access to a Windows machine or WSL.

With respect to .o not having the name I cannot recreate this.

but I don't know whether hcc supports something like -lpthread

Yes it does:

hcc ./program.HC -clibs=`-lpthread -lSDL2 -lcurl`

It's fairly trivial to write bindings to C libraries, though quite monotonous.