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

Multiple definition errors (unguarded generated main.h?) #148

Open nardi opened 4 years ago

nardi commented 4 years ago

I made a simple file hello.kit file containing:

function main() {
    printf("%s\n", "Hello from Kit!");
}

and tried to compile it using kitc hello.kit. At the linking stage I get a bunch of "multiple definition" errors for all kinds of symbols (e.g. kit_mem_Allocator__...). This seems logical, as the generated build/include/main.h file contains these definitions, but it is included in every .c-file and does not contain a header guard or similar. I assume this is not intended behavior, so what could be going wrong on my side?

Environment

[2020-08-01 16:26:57.1947] ===> kitc version
[2020-08-01 16:26:57.2686] DBG: 0.1.0
[2020-08-01 16:26:57.3465] ===> OS
[2020-08-01 16:26:57.4413] DBG: mingw32
[2020-08-01 16:26:57.4813] ===> Source paths
[2020-08-01 16:26:57.5721] ===> Standard prelude location
[2020-08-01 16:26:57.6480] DBG: C:\tools\kit\std\prelude.kit
[2020-08-01 16:26:57.6900] ===> ** COMPILER **
[2020-08-01 16:26:57.7339] ===> Toolchain
[2020-08-01 16:26:57.7898] DBG: C:\tools\kit\toolchains\windows-mingw
[2020-08-01 16:26:57.8433] ===> Compiler
[2020-08-01 16:26:58.6820] DBG: gcc
gcc (Rev1, Built by MSYS2 project) 10.2.0
Copyright (C) 2020 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.

[2020-08-01 16:26:59.4545] ===> Include paths
[2020-08-01 16:26:59.4745] DBG: []
[2020-08-01 16:26:59.5274] ===> Compiler flags
[2020-08-01 16:26:59.5723] DBG: ["-D__USE_MINGW_ANSI_STDIO","-std=c99","-pedantic","-O3","-Os","-Wno-missing-braces","-Wno-shift-op-parentheses"]
[2020-08-01 16:26:59.7491] ===> Linker flags
[2020-08-01 16:26:59.7810] DBG: ["-std=c99","-pedantic","-O3","-Os","-Wno-missing-braces","-Wno-shift-op-parentheses"]

I see mingw32 here, not mingw64, is that a problem? I got it from the scoop package, but am using my existing install of gcc-mingw64 via msys2.

nardi commented 4 years ago

FIX:

I've figured it out, from GCC 10 (May 2020) the default behavior has changed. Multiple (consistent) declarations of a global variable used to be okay, but now they give an error if not prefixed with extern. A quick fix would be to add the gcc option -fcommon but it should probably still be fixed in the header generator by adding extern in front of variable declarations :)

Source: https://gcc.gnu.org/gcc-10/porting_to.html

AlectronikForge commented 4 years ago

I get the same problem. Thanks for pointing out about GCC 10, as unfortunately clang doesn't buid either, it bails out with an error about some system header file :/

Could anyone point me to where in the source code the invocation of GCC is located to add said -fcommon switch?

Edit: Indeed it is the solution, with a wrapper script the problem is gone!

/usr/bin/gcc-fcommon:

!/usr/bin/bash

gcc -fcommon $*

export CC=gcc-fcommon

PtrMan commented 3 years ago

better is to add -fcommon to the command line in toolchain/linux-gcc in the kit source directory (not build which is created in the folder from where the compiler is called)