简体   繁体   English

在 macOS (Bazel) 上找不到 GLFW 库

[英]Cannot find GLFW library on macOS (Bazel)

I'm working on a simple OpenGL application using C++, Bazel, and GLFW.我正在使用 C++、Bazel 和 GLFW 开发一个简单的 OpenGL 应用程序。 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 macOS Catalina 10.15.7
  • Apple clang version 12.0.0苹果叮当版 12.0.0

Here is my BUILD rule for the target:这是我针对目标的BUILD规则:

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

I'm trying to build GLFW as an external dependency.我正在尝试将 GLFW 构建为外部依赖项。 My WORKSPACE file has this:我的WORKSPACE文件有这个:

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:我的glfw.BUILD文件的内容是:

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:我认为您只需要从代码块中删除-lglfw3

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. glfw3 链接请求可能是针对预构建的 glfw 构建指令的剪切和粘贴错误。

I ran into the same issue on Mac.我在 Mac 上遇到了同样的问题。 The Bazel rules I found for GLFW didn't work for me on Mac OS so I decided to rewrite them.我为 GLFW 找到的 Bazel 规则在 Mac OS 上对我不起作用,所以我决定重写它们。 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.我想你现在已经设法解决了你的问题,但我希望它对遇到这个问题的其他人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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