I am a non-expert with many things PureBasic.PureBasic 6.00 LTS (Windows - x64)
Windows 10 [Version 10.0.19044.3086]
I would like to integrate the Tiny C (https://bellard.org/tcc/)
libtcc.lib (https://bellard.org/tcc/tcc-doc.html#Libtcc)
into PureBasic.
A search of the PureBasic forums returned one match for libtcc.
idle said on 2022/01/13 that he was looking into libtcc to see if it was usable.
My goal is to create a simple console program that will take int main(){printf("Hello World.\n"); return 0;}
compile it in memory,
run it,
and have "Hello World" displayed on the console screen.
Yes,
I could use RunProgram (https://www.purebasic.com/documentation ... gram.html) to run TCC.EXE,
and compile the C-code using that method,
but I would like to learn how to integrate the libtcc.lib into PureBasic,
and run the code that way instead.
The following (incomplete) code is what I have cobbled together,
but it does not work,
as pointers are not my friends.
Code: Select all
EnableExplicit
Global rc.l, s.s
#TCC_OUTPUT_MEMORY = 1
#TCC_OUTPUT_EXE = 2
#TCC_OUTPUT_DLL = 3
#TCC_OUTPUT_OBJ = 4
#TCC_OUTPUT_PREPROCESS = 5
ImportC "libtcc.lib"
; /* compile a string containing a C source. Return -1 If error. */
; LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf);
tcc_compile_string(*s, *buf) As "tcc_compile_string"
; /* create a new TCC compilation context */
; LIBTCCAPI TCCState *tcc_new(void);
tcc_new() As "tcc_new"
; /* set output type. MUST BE CALLED before any compilation */
; LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type);
; #define TCC_OUTPUT_MEMORY 1 /* output will be run in memory (Default) */
; #define TCC_OUTPUT_EXE 2 /* executable file */
; #define TCC_OUTPUT_DLL 3 /* dynamic library */
; #define TCC_OUTPUT_OBJ 4 /* object file */
; #define TCC_OUTPUT_PREPROCESS 5 /* only preprocess (used internally) */ tcc_set_output_type(*s, int) As "tcc_output_type"
tcc_set_output_type(s,i) As "tcc_set_output_type"
; /* link And run main() function And Return its value. DO Not call
; tcc_relocate() before. */
; LIBTCCAPI int tcc_run(TCCState *s, int argc, char **argv);
tcc_run() As "tcc_run"
EndImport
rc.l = tcc_new()
rc.l = tcc_set_output_type(s, #TCC_OUTPUT_MEMORY)
rc.l = tcc_compile_string("int main(){printf("""Hello World.\n"""); return 0;}")
rc.l = tcc_run(s,argc,argv)
I have been reading,
referencing other code I have found in the forum,
but I still cannot grasp what goes where.
I'm coming up on my 61st orbit around the sun,
so any constructive assistance would be appreciated.
Regards and Thanks,
Joe