0

I have a main function, but when tcc goes to link everything together, it says its undefined. Why is this?

I'm using a python build script, and arguments are passed as lists:

['v:/exe/tcc/tcc.exe', '-odist/***.exe', '-Llib/', '-llua5.1', '-lsoil', '-lSDL', '-lopengl32', 'build/luainterface\\msL.o', 'build/luainterface\\vector.o', 'build/rendering\\renderutil.o', 'build/structures\\crtree.o', 'build/structures\\uniqueid.o', 'build/structures\\vector.o', 'build/world\\blocklib.o', 'build/world\\chunk.o', 'build/world\\world.o', 'build/main.o']

The output is simply:

tcc: undefined symbol 'main'

My main function is defined in the file main.c (and doesn't have a header), with a couple of static functions. Main function:

int main(int argc, char *argv[])
{
    SDL_Surface* screen = render_initialize(1024,768);

    lua_State* L = luaL_newstate();
    //lua_atpanic(L,&Lpanic);

    msL_registerState(L);
    msL_openAllLibs(L);

    int fail = luaL_dofile(L,"lua/main.luac");
    if(fail)
    {
        fprintf(stderr, "Lua error: %s\n", lua_tostring(L,-1));
        doexit(1);
    }

    lua_getfield(L,LUA_GLOBALSINDEX,"draw");
    if(!lua_isfunction(L,-1))
    {
        lua_pop(L,1);
        fprintf(stderr, "No draw function defined in global scope\n");
        doexit(1);
    }
    lua_setfield(L,LUA_REGISTRYINDEX,"msL_drawfunction");

    while(1)
    {
        lua_getfield(L,LUA_REGISTRYINDEX,"msL_drawfunction");
        assert(lua_isfunction(L,-1));
        int err = lua_pcall(L,0,0,0);
        if(err)
        {
            fprintf(stderr,"Lua error: ");
            switch(err)
            {
                case LUA_ERRRUN:
                    fprintf(stderr,"%s",lua_tostring(L,-1));
                    break;
                case LUA_ERRMEM:
                    fprintf(stderr,"out of memory");
                    break;
                default:
                    fprintf(stderr,"unknown error");
            }
            fprintf(stderr,"\n");
            doexit(1);
        }

        render_flipbuffers(screen);
    }
    doexit(0);
}

EDIT: I ran the code through tcc with preprocessing only. Apparently the main function is being renamed to SDL_main, through some macro in SDL.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • Please see http://sscce.org/ for advice on asking a better question. You should ditch python for now and make a very simple shell script that compiles and links your program so we can see exactly what arguments were passed to tcc. You neglected to show us the arguments to tcc used to compile main.o. Your main.c MUST be incomplete, because the compiler probably will not recognize the "SDL_Surface" type unless you include the proper header file. You should simplify your code as much as possible. – David Grayson Oct 30 '11 at 22:38
  • @DavidGrayson I can add the compile options if you want... I didn't paste the whole main file, including the #include's (as I said, the code compiles). – Colonel Thirty Two Oct 30 '11 at 22:54

1 Answers1

1

Add -lSDLmain to linker flags.

http://wiki.libsdl.org/moin.cgi/FAQWindows#I_get_.22Undefined_reference_to_.27WinMain.4016.27.22

Although things may be a little different in tcc.

Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65
  • Unfortunately, it didn't seem to work. It fails with the same error. Though it's definitely a step up, and may be tcc's fault. – Colonel Thirty Two Oct 31 '11 at 00:00
  • Ok, after following the instructions on what to do to define my own WinMain function (basically just copying the win32 main file into my project), it links correctly. Thanks a lot for the link! – Colonel Thirty Two Oct 31 '11 at 00:24