Q:

TCC invoke a function returning a double

Has anyone successfully invoked a function returning a double with TCC's libtcc?
I defined a function to return a double in my code and through tcc_add_symbol added it to libtcc. When I invoked this function in tcc scripts and got the returned value, the value was 0.000, which was not what I was expecting.
The code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libtcc.h"

double get_double()
{
return 80.333;
}

int get_int()
{
return 333;
}

char my_program[] =
"int foo()\n"
"{\n"
"    printf(\"Hello World!\\n\");\n"
"    printf(\"double: %.4f\\n\", get_double()); \n"
"    printf(\"int: %d\\n\", get_int()); \n"
"    return 0;\n"
"}\n";

int main(int argc, char **argv)
{
TCCState *s;
typedef int (*func_type)();
func_type func;

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

tcc_set_lib_path(s, "TCC");

tcc_set_output_type(s, TCC_OUTPUT_MEMORY);

if (tcc_compile_string(s, my_program) == -1)
    return 1;
tcc_add_symbol(s, "get_double", get_double);
tcc_add_symbol(s, "get_int", get_int);

if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
    return 1;

func = (func_type)tcc_get_symbol(s, "foo");
if (!func)
    return 1;

func();
tcc_delete(s);
getchar();
return 0;
}

The result of running the code:
Hello World!

double: 0.0000

int: 333

Why does the get_double() function return 0.0000, but get_int() is successful? 

A:

Take a look at your int foo() snippet. You have to remember, this string is the whole compilation unit, just as if you saved it into a C file. Within this compilation unit, get_int() and get_double() are actually undefined. The int version works due to luck, since all undeclared variables and functions have type of int. And this is also the reason why get_double does not work, because the same rule presumes it is in int function.
Solution is simple. Just declare your functions in your script. Use a header file or something like:
char my_program[] =
"double get_double();\n"
"int get_int();\n"
"int foo()\n"
"{\n"
"    printf(\"Hello World!\\n\");\n"
"    printf(\"double: %.4f\\n\", get_double()); \n"
"    printf(\"int: %d\\n\", get_int()); \n"
"    return 0;\n"
"}\n";

I strongly suggest you use tcc_set_error_func() to capture any warnings and errors.

