terralang / terra

Terra is a low-level system programming language that is embedded in and meta-programmed by the Lua programming language.
terralang.org
Other
2.71k stars 197 forks source link

"CommandLine Error: Option 'help-list' registered more than once!" #629

Closed yohannd1 closed 1 year ago

yohannd1 commented 1 year ago

Hello! I'm experimenting Terra (Release 1.1.0) and trying to build an SDL app with it. So far, my code is the following:

local C = terralib.includecstring [[
    #include <stdio.h>
    #include <SDL2/SDL.h>
]]

terralib.linklibrary("/usr/lib/libSDL2-2.0.so.0")

local main = terra()
    C.SDL_Init(C.SDL_INIT_VIDEO)
    defer C.SDL_Quit()

    var win = C.SDL_CreateWindow(
        "Hello, World!", -1, -1,
        200, 200, C.SDL_WINDOW_SHOWN
    )
    defer C.SDL_DestroyWindow(win)
end

main()

Overall it seems fine but it's having an odd error which I can't really figure out (seemingly with LLVM):

: CommandLine Error: Option 'help-list' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options

Removing the var win and defer C.SDL_DestroyWindow statements solve the issue, so I suppose the problem is coming from there.

elliottslaughter commented 1 year ago

Did this only happen with SDL or does it also happen with a hello world program?

This error happens when you have multiple copies of LLVM linked into your program, so if SDL (or a dependency) was linked against LLVM, that could cause it.

It could also be something in the LLVM build itself and you might benefit from using our official LLVM builds (if you built from source): https://github.com/terralang/llvm-build/

yohannd1 commented 1 year ago

So far it's only happened with SDL. A program that just calls C.printf doesn't have that error.

I also tried using the custom LLVM build (downloaded latest release and added it to path) but it doesn't seem to be helping (I might have used it wrong, though - see screenshot):

image

Also: how could I go about solving the second potential issue you mentioned? Would I have to compile SDL without LLVM?

elliottslaughter commented 1 year ago

If it goes away when you don't use SDL, then SDL is probably the culprit, either directly or indirectly.

There are a few options for how to deal with this:

  1. Save a binary instead of attempting to JIT in-process. See https://terralang.org/api.html#saving-terra-code and the https://github.com/terralang/terra/blob/master/tests/hello.t example code. (Note: remove the call to terralib.linklibrary and make sure to pass the right link flags to terralib.saveobj.)
  2. Attempt to link Terra and SDL against the same LLVM. This can get tricky because some distros just don't package LLVM correctly. And possibly futile if LLVM is statically linked to Terra or SDL.
  3. Attempt to determine which feature or dependency of SDL is bringing in its own copy of LLVM. You could start with ldd on the SDL shared library, just to confirm that LLVM is there. But from there, you'll have to do some digging to figure out if it's a direct or indirect dependency, which can be a bit of a rabbit hole.
yohannd1 commented 1 year ago

Option 1 worked fine for me, so I think I'll stick with that. Thanks for the help!