Beispiel #1
0
/*time_t lastmodtime;*/
int recompile()
{
	/*
	struct stat file_stat;
	int err = stat("update.c", &file_stat);

	if( !g_tcc )
	{
	lastmodtime = file_stat.st_mtime;		
	}
	else
	{
	if( lastmodtime == file_stat.st_mtime )
	return 0;
	lastmodtime = file_stat.st_mtime;
	}
	*/
	printf("recompile\n");
	FILE* f = fopen( "update.c", "rb" );	// MUST be "rb" on windows - othersize CRLF will be converted to \n, and file size will be wrong (CRLF:2bytes, \n:1bytes)
	fseek(f, 0L, SEEK_END);
	long sz = ftell(f);
	fseek(f, 0L, SEEK_SET);
	char* program = (char*)malloc( sz+1 );
	fread( program, sz, 1, f ); 
	program[sz] = '\0';
	fclose(f);

	TCCState* s = tcc_new();
	tcc_set_output_type(s, TCC_OUTPUT_MEMORY);

	int n;
	tcc_set_error_func(s, &n, compile_err);

	addSymbols(s);

	int compileResult = tcc_compile_string(s, program);
	free( program );

	if ( compileResult == -1)
	{
		return 1;
	}
	
	tcc_add_library_path(s,".");
	
	/* relocate the code */
	if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
		return 1;

	/* get entry symbol */
	update = (updatefp_t)tcc_get_symbol(s, "update");
	if (!update)
		return 1;

	if( g_tcc ) tcc_delete(g_tcc);
	g_tcc = s;

	printf("recompile ok\n");
	return 0;
}
Beispiel #2
0
int main(int argc, char **argv)
{
    TCCState *s;
    int i;
    int (*func)(int);

    s = tcc_new();
    if (!s) {
        fprintf(stderr, "Could not create tcc state\n");
        exit(1);
    }

    /* if tcclib.h and libtcc1.a are not installed, where can we find them */
    for (i = 1; i < argc; ++i) {
        char *a = argv[i];
        if (a[0] == '-') {
            if (a[1] == 'B')
                tcc_set_lib_path(s, a+2);
            else if (a[1] == 'I')
                tcc_add_include_path(s, a+2);
            else if (a[1] == 'L')
                tcc_add_library_path(s, a+2);
        }
    }

    /* MUST BE CALLED before any compilation */
    tcc_set_output_type(s, TCC_OUTPUT_MEMORY);

    if (tcc_compile_string(s, my_program) == -1)
        return 1;

    /* as a test, we add a symbol that the compiled program can use.
       You may also open a dll with tcc_add_dll() and use symbols from that */
    tcc_add_symbol(s, "add", add);

    /* relocate the code */
    if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
        return 1;

    /* get entry symbol */
    func = tcc_get_symbol(s, "foo");
    if (!func)
        return 1;

    /* run the code */
    func(32);

    /* delete the state */
    tcc_delete(s);

    return 0;
}