kitlang / kit

Kit: a magical, high performance programming language, designed for game development.
https://www.kitlang.org
Other
1.02k stars 29 forks source link

Compiling error with SDL2 on windows #117

Closed MadIcecream closed 5 years ago

MadIcecream commented 5 years ago

Describe the problem. What did you see? What did you expect to see?

When I tried to compile a simple sdl2 hello-world, the compiler gave me an error.

kitc HelloWorld.kit --run -I 3rdparty\SDL2\include
[2019-03-18 12:02:02.2754] ===> parsing and building module graph
[2019-03-18 12:02:02.3255] ===> processing C includes
----------------------------------------
Error: Parsing C header "build\\include\\__clibs.h" failed: C:/Users/madic/scoop/apps/gcc/current/x86_64-w64-mingw32/include/psdk_inc/intrin-impl.h:835: (column 222) [ERROR]  >>> Syntax Error !
  Syntax error !
  The symbol `)' does not fit here.

[2019-03-18 12:02:03.0012] ERR: compilation failed (1 errors)

If this is a code issue, provide a minimal code example:

It's just a line of SDL_Init

include "SDL.h";

function main(){
    SDL_Init(${SDL_INIT_EVERYTHING:Uint32});
}

Environment

[2019-03-18 12:11:15.5615] ===> kitc version
[2019-03-18 12:11:15.5655] DBG: 0.1.0
[2019-03-18 12:11:15.5685] ===> OS
[2019-03-18 12:11:15.5715] DBG: mingw32
[2019-03-18 12:11:15.5755] ===> Source paths
[2019-03-18 12:11:15.5805] DBG: ["src","C:\\Users\\madic\\scoop\\apps\\kitlang-prerelease\\0.1.0-prerelease-0\\std"]
[2019-03-18 12:11:15.5855] ===> Standard prelude location
[2019-03-18 12:11:15.5905] DBG: C:\Users\madic\scoop\apps\kitlang-prerelease\0.1.0-prerelease-0\std\prelude.kit
[2019-03-18 12:11:15.5965] ===> ** COMPILER **
[2019-03-18 12:11:15.6005] ===> Toolchain
[2019-03-18 12:11:15.6035] DBG: C:\Users\madic\scoop\apps\kitlang-prerelease\0.1.0-prerelease-0\toolchains\windows-mingw[2019-03-18 12:11:15.6105] ===> Compiler
[2019-03-18 12:11:15.6135] DBG: gcc
gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[2019-03-18 12:11:15.6546] ===> Include paths
[2019-03-18 12:11:15.6585] DBG: []
[2019-03-18 12:11:15.6625] ===> Compiler flags
[2019-03-18 12:11:15.6655] DBG: ["-D__USE_MINGW_ANSI_STDIO","-std=c99","-pedantic","-O3","-Os","-Wno-missing-braces","-Wno-shift-op-parentheses"]
[2019-03-18 12:11:15.6735] ===> Linker flags
[2019-03-18 12:11:15.6765] DBG: ["-std=c99","-pedantic","-O3","-Os","-Wno-missing-braces","-Wno-shift-op-parentheses"]
bendmorris commented 5 years ago

Would you mind checking C:/Users/madic/scoop/apps/gcc/current/x86_64-w64-mingw32/include/psdk_inc/intrin-impl.h and posting the statement that includes line 835?

These parser failures happen occasionally and sometimes there's a workaround. The best fix is probably for me to migrate off of language-c and onto libclang for parsing C headers.

MadIcecream commented 5 years ago

Sure intrin-impl.h.txt

bendmorris commented 5 years ago

I'll try this out later, but I'm wondering if this is because __int64 isn't defined in mingw, which might require you to explicitly include "_mingw.h"; before including SDL.

omaraaa commented 5 years ago

https://github.com/haskell/c2hs/issues/199#issue-296630197

using the version of mingw specified in the issue linked above seems to fix it. However, now I seem to get this error, which I don't know how to solve:

F:\kit-test>kitc hello.kit -I . -o hello
[2019-04-08 12:59:03.9567] ===> parsing and building module graph
[2019-04-08 12:59:03.9768] ===> processing C includes
[2019-04-08 12:59:04.8488] ===> expanding macros
[2019-04-08 12:59:04.8508] ===> resolving module types
----------------------------------------
Error: :0: Duplicate declaration for `main` in hello;

First declaration:

Second declaration:

  @.\hello.kit:4:1-13
       4    function main() {
            ^^^^^^^^^^^^^

Function, variable, type and trait names must be unique within the same namespac
e.
[2019-04-08 12:59:04.8598] ERR: compilation failed (1 errors)
bendmorris commented 5 years ago

This can happen if you name your main module "main.kit". It's a known issue. Try renaming it to something else.

omaraaa commented 5 years ago

It was already named hello.kit when I got the error.

TheSpydog commented 5 years ago

Wonder if this due to a naming conflict with SDL2's main function?

omaraaa commented 5 years ago

Wonder if this due to a naming conflict with SDL2's main function?

Possible. Does the kit SDL example work in linux? If so, maybe it is an issue with SDL providing an WinMain.

You should be using main() instead of WinMain() even though you are creating a Windows application, because SDL provides a version of WinMain() which performs some SDL initialization before calling your main code. If for some reason you need to use WinMain(), take a look at the SDL source code in src/main/win32/SDL_main.c to see what kind of initialization you need to do in your WinMain() function so that SDL works properly.

from https://wiki.libsdl.org/FAQWindows

You can stop SDL2 from handling the main if you define SDL_MAIN_HANDLED .


#define SDL_MAIN_HANDLED
#include "SDL.h"

int main(int argc, char *argv[])
{
    SDL_SetMainReady();
    SDL_Init(SDL_INIT_VIDEO);
    ...
    SDL_Quit();

    return 0;
}

from https://wiki.libsdl.org/SDL_SetMainReady

Is it possible to do that in kit?

bendmorris commented 5 years ago

That makes sense. You can define flags in Kit via the command line: -DSDL_MAIN_HANDLED

omaraaa commented 5 years ago

yup, now it works with setting SDL_MAIN_HANDLED and calling SDL_SetMainReady before SDL_Init. just needed to link the sdl libs using -c-L[SDL2 libs dir] and put the .dll in the directory.

include "SDL2/SDL.h" => "SDL2";
include "stdio.h";

function main() {
    SDL_SetMainReady();
    var window: Ptr[SDL_Window] = null;
    var screenSurface: Ptr[SDL_Surface] = null;
    if SDL_Init(SDL_INIT_VIDEO) < 0 {
        panic("could not initialize sdl2: %s\n", SDL_GetError());
    }
    window = SDL_CreateWindow(
        "Hello from Kit and SDL2",
        ${SDL_WINDOWPOS_UNDEFINED: Int}, ${SDL_WINDOWPOS_UNDEFINED: Int},
        640, 480,
        SDL_WINDOW_SHOWN as Uint
    );
    if window == null {
        panic("could not create window: %s\n", SDL_GetError());
    }
    screenSurface = SDL_GetWindowSurface(window);
    SDL_FillRect(screenSurface, null, SDL_MapRGB(screenSurface.format, 0x80, 0xff, 0xe6));
    SDL_UpdateWindowSurface(window);
    SDL_Delay(2000);
    SDL_DestroyWindow(window);
    SDL_Quit();
}
F:\kit-test>kitc hello.kit -I. -DSDL_MAIN_HANDLED -c-L"F:\SDL2-2.0.9\x86_64-w64-mingw32\lib"
[2019-04-17 14:06:25.8484] ===> parsing and building module graph
[2019-04-17 14:06:25.8684] ===> processing C includes
[2019-04-17 14:06:26.7465] ===> expanding macros
[2019-04-17 14:06:26.7485] ===> resolving module types
[2019-04-17 14:06:26.7555] ===> flattening trait implementations
[2019-04-17 14:06:26.7575] ===> typing module content
[2019-04-17 14:06:26.7975] ===> generating intermediate representation
[2019-04-17 14:06:26.8075] ===> generating code
[2019-04-17 14:06:26.8395] ===> compiling
[2019-04-17 14:06:28.5606] ===> linking
[2019-04-17 14:06:28.6196] ===> finished; total time: 2.7711585s

Image

bendmorris commented 5 years ago

Glad it's working, I'll close this for now. I'm also planning to add a way to set the entry point function's name, since some platforms don't use main - that would be another way to resolve this.