fccm / OCamlSDL2

OCaml interface to SDL 2.0 (for Linux, Windows, MacOS, and ChromeBook)
Other
51 stars 10 forks source link

Opam install and Dune: linking error #19

Closed tekbar11 closed 3 years ago

tekbar11 commented 3 years ago

Hey I'm interested in using this library for a school project, and I'm having trouble compiling some basic code with dune.

I've installed the opam package using opam install ocamlsdl2

Here's my dune file (executable (name test)(libraries sdl2))

When I'm running dune build, I'm getting ocamlopt test.exe (exit 2) (cd _build/default && /usr/bin/ocamlopt.opt -w @1..3@5..28@30..39@43@46..47@49..57@61..62-40 -strict-sequence -strict-formats -short-paths -keep-locs -g -o test.exe /home/wisesalmon/.opam/default/lib/sdl2/sdl2.cmxa -I /home/wisesalmon/.opam/default/lib/sdl2 .test.eobjs/native/dune__exe__Test.cmx) /usr/bin/ld: /home/wisesalmon/.opam/default/lib/sdl2/libsdl2_stubs.a(sdlinit_stub.o): in function caml_SDL_Init': /home/wisesalmon/.opam/default/.opam-switch/build/ocamlsdl2.0.03/src/sdlinit_stub.c:55: undefined reference to SDL_Init' /usr/bin/ld: /home/wisesalmon/.opam/default/lib/sdl2/libsdl2_stubs.a(sdlinit_stub.o): in function caml_SDL_InitSubSystem': /home/wisesalmon/.opam/default/.opam-switch/build/ocamlsdl2.0.03/src/sdlinit_stub.c:63: undefined reference to SDL_InitSubSystem' ... collect2: error: ld returned 1 exit status File "caml_startup", line 1: Error: Error during linking (exit code 1) Any idea what the problem is?

Thanks

fccm commented 3 years ago

Does it work when you use the command line? ocamlopt -I $(ocamlfind query sdl2) sdl2.cmxa test.ml -o test.opt

fccm commented 3 years ago

I never used Dune before. Let's try a tutorial about it. I copied the example file examples/ex_fill_rand_col.ml from ocamlsdl2 sources into /tmp/testsdl2/test.ml and used your dune file. Dune seems to compile in a very strict mode, turning warnings into errors (I even have no warnings before with this example). Dune doesn't accept omitted labels, so I have to add it in my test.ml file. But after that, your dune file does work on my computer, and I can run the ./_build/default/test.exe program created.

Maybe you could try to link with the result of the command: sdl2-config --libs For example:

(executable
  (name test)
  (libraries sdl2)
  (link_flags -cclib -lSDL2)
)
tekbar11 commented 3 years ago

I can confirm that adding the (link_flags -cclib -lSDL2) did it for me. Thank you very much.

fccm commented 3 years ago

It seems like you're using Linux, but could you tell me which distribution you're using? I'm wondering why we have this difference. Do you also have to add the parameter -cclib -lSDL2 when you use the plain command line?

ocamlopt -I $(ocamlfind query sdl2) sdl2.cmxa test.ml -o test.opt

or

ocamlopt -I $(ocamlfind query sdl2) sdl2.cmxa -cclib -lSDL2 test.ml -o test.opt
tekbar11 commented 3 years ago

It seems like you're using Linux, but could you tell me which distribution you're using?

I'm on Arch Linux.

Do you also have to add the parameter -cclib -lSDL2 when you use the plain command line?

ocamlopt -I $(ocamlfind query sdl2) sdl2.cmxa test.ml -o test.opt

or

ocamlopt -I $(ocamlfind query sdl2) sdl2.cmxa -cclib -lSDL2 test.ml -o test.opt

Yes, I need to add -cclib -lSDL2 in the command line, otherwise I get the same linking error.