简体   繁体   中英

Cannot find GLFW library on macOS (Bazel)

I'm working on a simple OpenGL application using C++, Bazel, and GLFW. When I attempt to build the rule for my app, I get the following error message:

ld: library not found for -lglfw3

My environment:

  • macOS Catalina 10.15.7
  • Apple clang version 12.0.0

Here is my BUILD rule for the target:

cc_binary(
    name = "hello-glfw",
    srcs = ["hello-glfw.cpp"],
    deps = [
        "@glfw//:glfw",
    ],
)

I'm trying to build GLFW as an external dependency. My WORKSPACE file has this:

http_archive(
    name = "glfw",
    build_file = "@//extern:glfw.BUILD",
    strip_prefix = "glfw-3.3.2",
    urls = ["https://github.com/glfw/glfw/archive/3.3.2.zip"],
)

The contents of my glfw.BUILD file is:

DARWIN_DEFINES = [
    "_GLFW_COCOA",
    "_GLFW_NSGL",
    "_GLFW_NO_DLOAD_WINMM",
    "_GLFW_USE_OPENGL",
]

DARWIN_HDRS = [
    "src/cocoa_joystick.h",
    "src/cocoa_platform.h",
    "src/glx_context.h",
    "src/nsgl_context.h",
    "src/null_joystick.h",
    "src/null_platform.h",
    "src/posix_thread.h",
    "src/wl_platform.h",
]


DARWIN_SRCS = [
    "src/cocoa_time.c",
    "src/posix_thread.c",
]

DARWIN_LINKOPTS = [
    "-lglfw3",
    "-framework OpenGL",
    "-framework Cocoa",
    "-framework IOKit",
    "-framework CoreFoundation"
]


cc_library(
    name = "glfw_src",
    hdrs = [
        "include/GLFW/glfw3.h",
        "include/GLFW/glfw3native.h",
        "src/egl_context.h",
        "src/internal.h",
        "src/osmesa_context.h",
        "src/mappings.h",
        "src/xkb_unicode.h",
    ] + select({
        "@bazel_tools//src/conditions:windows": WIN32_HDRS,
        "@bazel_tools//src/conditions:linux_x86_64": LINUX_HDRS,
        "@bazel_tools//src/conditions:darwin": DARWIN_HDRS,
    }),
    srcs = [
        "src/context.c",
        "src/egl_context.c",
        "src/init.c",
        "src/input.c",
        "src/osmesa_context.c",
        "src/monitor.c",
        "src/vulkan.c",
        "src/window.c",
        "src/xkb_unicode.c",
    ] + select({
        "@bazel_tools//src/conditions:windows": WIN32_SRCS,
        "@bazel_tools//src/conditions:linux_x86_64": LINUX_SRCS,
        "@bazel_tools//src/conditions:darwin": DARWIN_SRCS,
    }),
    defines = select({
        "@bazel_tools//src/conditions:windows": WIN32_DEFINES,
        "@bazel_tools//src/conditions:linux_x86_64": LINUX_DEFINES,
        "@bazel_tools//src/conditions:darwin": DARWIN_DEFINES,
    }),
)

cc_library(
    name = "glfw",
    hdrs = [
        "include/GLFW/glfw3.h",
        "include/GLFW/glfw3native.h",
    ],
    linkopts = select({
        "@bazel_tools//src/conditions:windows": WIN32_LINKOPTS,
        "@bazel_tools//src/conditions:linux_x86_64": LINUX_LINKOPTS,
        "@bazel_tools//src/conditions:darwin": DARWIN_LINKOPTS,
    }),
    deps = [":glfw_src"],
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
)

I think you just need to remove -lglfw3 from the block of code:

DARWIN_LINKOPTS = [
    "-lglfw3",
    "-framework OpenGL",
    "-framework Cocoa",
    "-framework IOKit",
    "-framework CoreFoundation"
]

so it reads:

DARWIN_LINKOPTS = [
    "-framework OpenGL",
    "-framework Cocoa",
    "-framework IOKit",
    "-framework CoreFoundation"
]

The glfw3 link request is probably a cut and paste error from instructions for building against glfw that is prebuilt.

I ran into the same issue on Mac. The Bazel rules I found for GLFW didn't work for me on Mac OS so I decided to rewrite them. After running into several linking errors I managed to get it fully working and I reported my findings here . I suppose you already managed to solve your problem by now, but I hope it's useful for anyone else running into this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM