Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Windows: Linking against standard libraries #1945

Closed
Helcaraxan opened this issue Oct 14, 2016 · 5 comments
Closed

On Windows: Linking against standard libraries #1945

Helcaraxan opened this issue Oct 14, 2016 · 5 comments

Comments

@Helcaraxan
Copy link

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.

@hermione521
Copy link
Contributor

@meteorcloudy

@laszlocsomor
Copy link
Contributor

laszlocsomor commented Oct 24, 2016

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.

@Helcaraxan
Copy link
Author

Thanks for the solution!

@grandseiken
Copy link

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

@laszlocsomor
Copy link
Contributor

@grandseiken : hey Stuart, small world :)

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

No branches or pull requests

4 participants