Skip to content

On Windows: Linking against standard libraries #1945

Closed

Description

@Helcaraxan

For C++, linkopts allows one to specify linker options. However, it also states that:

Each element of this list that does not start with $ or - is assumed to be the label of a target in deps. The list of files generated by that target is appended to the linker options. An error is reported if the label is invalid, or is not declared in deps.

As far as I can see, this means that I cannot specify plain arguments to the linker, e.g. User32.lib, as required for some system libraries.

I got it to link to the system library by copying it locally, specifying it as a srcs in a cc_library, and referring to the library's label in both deps and linkopts. However, copying a system library seems like a bad idea. The C++ system header files are already picked up automatically by Bazel, so maybe these libraries should be, too. I noticed that CMake automatically links against some libraries on Windows, i.e. it links against user32.lib advapi32.lib gdi32.lib when compiling openssl on Windows without explicitly specifying these anywhere.

Activity

  1. hermione521 commented on Oct 14, 2016

    @hermione521
    Contributor
  2. laszlocsomor commented on Oct 24, 2016

    @laszlocsomor
    Contributor

    You can use linkopts = ["-Wl,user32.lib", "-Wl,advapi32.lib"].

    $ cat foo/BUILD
    cc_binary(
        name = 'foo',
        srcs = ['foo.cc'],
        linkopts = ['-Wl,user32.lib'],
    )
    
    $ cat foo/foo.cc
    #include <Windows.h>
    
    int main(int, char**) {
      MessageBoxA(0, "text", "caption", 0);
      return 0;
    }
    
    $ bazel run --cpu=x64_windows_msvc //foo
    ...
    INFO: Running command line: C:/tools/msys64/var/tmp/Bazel/MGAgSgPx/execroot/bazel2/bazel-out/vc_14_0_x64-fastbuild/bin/foo/foo.exe
    
    

    See the command line translation here:

    (r'-Wl,(.+)\.lib', ['$0.lib']),


    From Bazel 0.5.3, we no longer use wrapper script to invoke MSVC compiler anymore. So -Wl,<lib> won't be translated, please use -DEFAULTLIB:<lib> instead.

  3. Helcaraxan commented on Oct 24, 2016

    @Helcaraxan
    Author

    Thanks for the solution!

  4. grandseiken commented on Oct 24, 2016

    @grandseiken

    Thanks laszlo! You walked out that elevator like a boss!

  5. laszlocsomor commented on Oct 24, 2016

    @laszlocsomor
    Contributor

    @grandseiken : hey Stuart, small world :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions