0

I have a Bazel BUILD file that involves linking the SDL2 library. I was wondering if I there is a way to replace the file path for the -L flag to a relative file path instead of an absolute file path.

The BUILD file that uses the absolute file path /Users/desmondchi/dev/bullet_hell_sdl2_game/sdl2/2.28.1/lib works but when I change it to sdl2/2.28.1/lib it says that the file path cannot be found. Note: The folder sdl2 is in the same directory as this BUILD file. Thanks in advance for any help!

cc_library(
    name = "sdl2",
    hdrs = glob(["include/SDL2/*.h"]),
    linkopts = [
        # TODO: File path for -L flag should be relative path to sdl2/2.28.1/lib. (Unable to do so.)
        "-L /Users/desmondchi/dev/bullet_hell_sdl2_game/sdl2/2.28.1/lib -l SDL2-2.0.0",
        "-framework OpenGL"
    ],
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
)

Here is the error that I get from using -L sdl2/2.28.1/lib.

  • I actually wonder why do you want to use linkopts instead of listing the SDL2 libraries in the srcs attribute? It would be much easier and works well with relative paths. – Alireza Jul 25 '23 at 15:12
  • 1
    @Alireza I tried this and this works perfectly! Thanks! – Desmond Chi Aug 15 '23 at 05:49
  • Great :), I just posted my comment as an answer below, can you please mark it as the correct answer. Thanks! – Alireza Aug 25 '23 at 08:01

2 Answers2

0

When you do this (and btw. you may actually want to have a look at cc_import these days perhaps if pre-built), you've created a library sdl2 which your other code can depend on. It does provides hdrs and hence when you try to compile against this library, that will work as interfaces are known, however, you never included the implementation (srcs) in the target description (there is no sources to compile or pre-build solib/DLL or library archive) which means you won't be able to link against this library label. The fact it "works" with absolute path is sort of accidental, when you just added the search path with -L since it remains stable across builds (and is accessible from action sandbox; but this looks like Windows context, so the sandbox bit would be IIRC moot) and linker will find it with that information anyways. If you used path relative to the package containing sdl2 bits that for the action rooted elsewhere (or the path not being even included in the sandbox) will make no sense to the linker. TL;DR the minimal change would be to add the solib/DLL or library archive implementing desired functions under srcs.

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
0

Instead of using the linkopts attribute, you can list the SDL2 libraries in the srcs attribute. It's easier to use and works well with relative paths.

Alireza
  • 800
  • 1
  • 7
  • 21