tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] accessing tinycc from C program


From: fsw . fb
Subject: Re: [Tinycc-devel] accessing tinycc from C program
Date: Thu, 20 Aug 2009 17:06:47 +0000 (UTC)

Hi,
played a little bit with your code and works with a few warnings... thanks.

There seems to be an issue with libtcc in general, because if I exchange the following code:

tcc_compile_string(s, "int bar(){ return(7-5);}\n" );

with:

tcc_compile_string(s, "int main(){printf("Hello World!");return 0;}\n");

tcc complains: "  ',' expected "

Had the impression libtcc was just another way to access tcc's functionality, and that libtcc would be able to compile real world programs (like compiling itself), but at this point in time it doesn't seem so.

Thank for reading
fsw




----- Original Message -----
From: "Rüdiger Plantiko" <address@hidden>
To: address@hidden
Sent: Wednesday, August 19, 2009 2:42:23 PM GMT -08:00 US/Canada Pacific
Subject: Re: [Tinycc-devel] accessing tinycc from C program

Thank you, Felix, for the example code! I could not directly use it on
my Win 32 laptop,
but I combined it with the recipe for delayed loading of TCC as
described by Benjamin Maggi
in http://maggidev.com - and now I have my first runnable example of TCC
used as
interpreter. At runtime it needs to access libtcc1.a. Here is the
combination of Benjamin's
with your example.

Regards,
Rüdiger

*
------------------------------------------------------------------------------------
#include <windows.h>

struct TCCState;
typedef struct TCCState TCCState;

// Windows Handle for TCC library
HINSTANCE _tcc_lib = NULL;

// Macro for loading a pointer to a libtcc function
#define load_TCC_function(name)                 \
name = GetProcAddress(_tcc_lib, #name );        \
if ( NULL == name ) {                           \
  printf("Error: could not load " #name );      \
  FreeLibrary(_tcc_lib);                        \
  return FALSE;                                 \
  }    

/* Declarations of pointers to libtcc functions */
TCCState *(__stdcall *tcc_new)(void);
int       (__stdcall *tcc_compile_string)(TCCState *s, const char *buf);
int       (__stdcall *tcc_relocate)(TCCState *s1, void *ptr);
void *    (__stdcall *tcc_get_symbol)(TCCState *s, const char *name);
 
 
/*  
* Call this function at the start of your program to load the interpreter
* It will make it's functions available
*/
BOOL loadTcc(void)
{
  
_tcc_lib = LoadLibrary("libtcc.dll");    
if(!_tcc_lib) {
  printf("Could not load library libtcc.dll");
  return FALSE;    
  }
  
load_TCC_function( tcc_new )        
load_TCC_function( tcc_compile_string )
load_TCC_function( tcc_relocate )
load_TCC_function( tcc_get_symbol )
  
// Add more functions if needed      
  
return TRUE;
}

// Call this function when your program exits
// or when you no longer need to use the libtcc

BOOL UnLoadTCC(void) {    
  if(_tcc_lib) FreeLibrary(_tcc_lib);
  }

int main( char* argv[] ) {
 
  int testint;
  int (*testfct)(void);
  void* ptr = malloc(100000); // too much.
  TCCState* s;

  loadTcc( );
        
  s = tcc_new( );          
 
  tcc_compile_string(s, "int bar(){ return(7-5);}\n" );

  tcc_relocate(s, ptr);
  testfct = tcc_get_symbol( s, "bar" );
  if ( ! testfct ) {
    printf( "no symbol from string\n" );
    return (2);
    }

  testint = testfct();

  printf( "integer from string: %i\n", testint );
        
 
  UnLoadTCC();
        
  }        


Felix Salfelder schrieb:
> Hi tinycc developers.
>
> Thanks for your work!
> I am using libtcc to generate object code from C at run time. An example
> code for something like this is still missing.
> Furthermore it would be great to be able to hand a file descriptor to
> the compiler (instead of only a file name or a string).
>
> In the attachment you find a libtcc patch and an example application
> addressing this. Add it to the repo if you like.
>
> regards
> felix
>  
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tinycc-devel mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/tinycc-devel



_______________________________________________
Tinycc-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/tinycc-devel

reply via email to

[Prev in Thread] Current Thread [Next in Thread]