Skip to main content
GameDev.net gamedev.net
🔒 Locked

a little help with tinycc, tinycc compiler.

Started by becoolnike Dec 15, 2010 at 10:36 PM 6 replies 3.3k views
Original Post
becoolnike
becoolnike
I am using the tinycc mini compiler for generating dynamic code for a small project. The problem is that for a reason i can't pass FLOATS to functions, which are inside the main program. But if I change it to double it works. For a reason the literal 3.1416 is not interpreted as a float literal. I am using visual studio 2008 for compiling this code.
This is my code.

#include <iostream>#include "libtcc.h"using namespace std;void local( float f ){printf ( "%f \n", f );}void main(){TCCState *s;void (*main)();void *mem;int size=0;// This is our code. For a reason the literal 3.1416 is not intrepeted// as a float.char code[] = " void main () { local( 3.1416 ); }" ;s = tcc_new();if (!s)cout << "Error" << endl;/* MUST BE CALLED before any compilation */tcc_set_output_type(s, TCC_OUTPUT_MEMORY);if ( tcc_compile_string(s, code) == -1)cout << "Error" << endl;tcc_add_symbol(s, "printf", printf);tcc_add_symbol(s, "local", local);size = tcc_relocate(s, NULL);if (size == -1)cout << "Error" << endl;mem = malloc(size);tcc_relocate(s, mem);main =( void (*) (void) ) tcc_get_symbol(s, "main");if (!main)cout << "Error" << endl;// call the main functionmain();system ( "pause" );}


[Edited by - becoolnike on December 15, 2010 11:25:38 PM]
KulSeran
KulSeran
Did you try it with a trailing 'f' to specify it as a float? ie "3.14159f"
becoolnike
becoolnike
Sure, I did it! I passed any other type of data including doubles, long doubles, float pointers. And i don't have that problem. It appears to me this is a BUG.
Or maybe visual studio 2008 handle differently FLOATS, that's my only guess.

But i can't contact the author, no e-mail address. I only knows his website.



becoolnike
becoolnike
No, it doesn't work, but if I use double or long double, integer or any other type of data works. There is sure a bug in tinycc or Visual studio c++ handles differently FLOATS.
implicit
implicit
The local function doesn't seem to be declared anywhere in the code sent to TCC, so by default the compiler assumes it takes a double and promotes the float (e.g. by the pre-ANSI K&R rules, much as for variable argument lists like in printf.).

Try adding a "void local(float f);" declaration to the code string.
becoolnike
becoolnike
Adding a forward function prototype fixes this problem.
KulSeran you were right.
I feel like a C rookie! :)

I don't understand why I did not think of that before.


Thanks KulSeran!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.