'linking existed c++ static libaray in Bazel failed

I am writing a OpenGL programe on windows.

I want to link precompiled GLEW32.lib, but failed and failed for a couple of hours.

In WORKSPACE, a http_archive uses to download GLEW32.lib

http_archive(
  name = "glew",
  build_file = "@//:thirdparty/glew.BUILD",
  sha256 = "ea6b14a1c6c968d0034e61ff6cb242cff2ce0ede79267a0f2b47b1b0b652c164",
  strip_prefix = "glew-2.2.0",
  urls = ["https://github.com/nigels-com/glew/releases/download/glew-2.2.0/glew-2.2.0-win32.zip"],
)

GLEW.BUILD as follow

package(default_visibility=["//visibility:public"])
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_import(
  name = "glew_lib",
  static_library = "lib/Release/x64/glew32s.lib",
  hdrs = glob([
    "include/GL/*.h"
  ]),
)

cc_library(
  name = "glew"
  includes = ["include"],
  deps = ["glew_lib"]
)

Depending binary as follow

cc_binary(
  name = "window",
  srcs = [
    "window.cc",
  ],
  deps = [
    "//engine:debug",
    "//engine:gl",
    "//playground:playground",
    "@fmt",
    "@glew",
    "@glfw",
    "@glm",
    "@imgui",
  ],
  linkstatic = 1
)

The link param file C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX64\x64\link.exe @bazel-out/x64_windows-fastbuild/bin/playground/window.exe-2.params 's GLEW lib is odd. I don't know how external/glew/lib/Release/x64/glew32s.lib is generated. But GLEW.lib hadn't join linking.

/nologo
/OUT:bazel-out/x64_windows-fastbuild/bin/playground/window.exe
bazel-out/x64_windows-fastbuild/bin/playground/_objs/window/window.obj
bazel-out/x64_windows-fastbuild/bin/playground/playground.lib
bazel-out/x64_windows-fastbuild/bin/playground/scene/scenes.lib
bazel-out/x64_windows-fastbuild/bin/playground/pass.lib
bazel-out/x64_windows-fastbuild/bin/playground/object/object.lib
bazel-out/x64_windows-fastbuild/bin/engine/framebuffer/framebuffer.lib
bazel-out/x64_windows-fastbuild/bin/playground/context.lib
bazel-out/x64_windows-fastbuild/bin/engine/repo/repo.lib
bazel-out/x64_windows-fastbuild/bin/engine/engine.lib
bazel-out/x64_windows-fastbuild/bin/engine/primitive/primitive.lib
bazel-out/x64_windows-fastbuild/bin/engine/mesh.lib
bazel-out/x64_windows-fastbuild/bin/engine/gl.lib
bazel-out/x64_windows-fastbuild/bin/external/assimp/assimp.lib
bazel-out/x64_windows-fastbuild/bin/engine/util.lib
bazel-out/x64_windows-fastbuild/bin/engine/proto/config_proto.lib
bazel-out/x64_windows-fastbuild/bin/external/com_github_protocolbuffers_protobuf/protobuf.lib
bazel-out/x64_windows-fastbuild/bin/external/com_github_protocolbuffers_protobuf/protobuf_lite.lib
bazel-out/x64_windows-fastbuild/bin/external/com_github_google_glog/glog.lib
bazel-out/x64_windows-fastbuild/bin/external/com_github_gflags_gflags/gflags.lib
bazel-out/x64_windows-fastbuild/bin/external/fmt/fmt.lib
external/glew/lib/Release/x64/glew32s.lib
bazel-out/x64_windows-fastbuild/bin/external/imgui/imgui.lib
bazel-out/x64_windows-fastbuild/bin/external/glfw/glfw_src.lib
/SUBSYSTEM:CONSOLE
-ignore:4221
-ignore:4221
-DEFAULTLIB:user32.lib
-DEFAULTLIB:gdi32.lib
-DEFAULTLIB:shell32.lib
/MACHINE:X64
/DEFAULTLIB:msvcrt.lib
/DEBUG:FASTLINK
/INCREMENTAL:NO

Could someone help me to link existed GLEW32.lib success? Thanks a lot.



Solution 1:[1]

My fault. This is not bazel's issue or glew's issue. GLEW means The OpenGL Extension Wrangler Library. It doesn't contain the origin OpenGL function implements.

After adding linkopts = ["-DEFAULTLIB:opengl32.lib"] to glew cc_library. Every is OK.

The link error as follow. They are all original OpenGL functions.

gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glBindTexture referenced in function "void __cdecl glBindTexture_(unsigned int,unsigned int)" (?glBindTexture_@@YAXII@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glClear referenced in function "void __cdecl glClear_(unsigned int)" (?glClear_@@YAXI@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glClearColor referenced in function "void __cdecl
glClearColor_(float,float,float,float)" (?glClearColor_@@YAXMMMM@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glCullFace referenced in function "void __cdecl glCullFace_(unsigned int)" (?glCullFace_@@YAXI@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glDeleteTextures referenced in function "void __cdecl glDeleteTextures_(int,unsigned int const *)" (?glDeleteTextures_@@YAXHPEBI@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glDisable referenced in function "void __cdecl glDisable_(unsigned int)" (?glDisable_@@YAXI@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glDrawArrays referenced in function "void __cdecl
glDrawArrays_(unsigned int,int,int)" (?glDrawArrays_@@YAXIHH@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glDrawBuffer referenced in function "void __cdecl
glDrawBuffer_(unsigned int)" (?glDrawBuffer_@@YAXI@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glDrawElements referenced in function "void __cdecl glDrawElements_(unsigned int,int,unsigned int,void const *)" (?glDrawElements_@@YAXIHIPEBX@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glEnable referenced in function "void __cdecl glEnable_(unsigned int)" (?glEnable_@@YAXI@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glFrontFace referenced in function "void __cdecl glFrontFace_(unsigned int)" (?glFrontFace_@@YAXI@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glGenTextures referenced in function "void __cdecl glGenTextures_(int,unsigned int *)" (?glGenTextures_@@YAXHPEAI@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glGetBooleanv referenced in function "void __cdecl glGetBooleanv_(unsigned int,unsigned char *)" (?glGetBooleanv_@@YAXIPEAE@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glGetDoublev referenced in function "void __cdecl
glGetDoublev_(unsigned int,double *)" (?glGetDoublev_@@YAXIPEAN@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glGetError referenced in function "unsigned int __cdecl glewInit_(void)" (?glewInit_@@YAIXZ)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glGetFloatv referenced in function "void __cdecl glGetFloatv_(unsigned int,float *)" (?glGetFloatv_@@YAXIPEAM@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glGetIntegerv referenced in function "void __cdecl glGetIntegerv_(unsigned int,int *)" (?glGetIntegerv_@@YAXIPEAH@Z)
glew.lib(glew.obj) : error LNK2001: unresolved external symbol __imp_glGetIntegerv
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glGetTexImage referenced in function "void __cdecl glGetTexImage_(unsigned int,int,unsigned int,unsigned int,void *)" (?glGetTexImage_@@YAXIHIIPEAX@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glGetTexLevelParameteriv referenced in function "void __cdecl glGetTexLevelParameteriv_(unsigned int,int,unsigned int,int *)" (?glGetTexLevelParameteriv_@@YAXIHIPEAH@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glReadBuffer referenced in function "void __cdecl
glReadBuffer_(unsigned int)" (?glReadBuffer_@@YAXI@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glTexImage2D referenced in function "void __cdecl
glTexImage2D_(unsigned int,int,int,int,int,int,unsigned int,unsigned int,void const *)" (?glTexImage2D_@@YAXIHHHHHIIPEBX@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glTexParameteri referenced in function "void __cdecl glTexParameteri_(unsigned int,unsigned int,int)" (?glTexParameteri_@@YAXIIH@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glTexSubImage2D referenced in function "void __cdecl glTexSubImage2D_(unsigned int,int,int,int,int,int,unsigned int,unsigned int,void const *)" (?glTexSubImage2D_@@YAXIHHHHHIIPEBX@Z)
gl.lib(gl.obj) : error LNK2019: unresolved external symbol __imp_glViewport referenced in function "void __cdecl glViewport_(int,int,int,int)" (?glViewport_@@YAXHHHH@Z)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zichao liu