Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top