Search code examples
c++buildbazelsdl-2bazel-rules

Bazel cc_library linkopts relative file path


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.


Solution

  • 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.