Hi Yury,
I had the same error when building the shared library (a dll) in TensorFlow with GPU support, the library I needed to link is cudart.lib.
The reason why it's failing is basically a bug in MSVC linker.
When building a shared library, Bazel enables the whole archive feature, which means telling the linker to force link every library the target depends on.
On windows, if your VS is or newer than VS 2015 update 2, it means adding /WHOLEARCHIVE option(a new feature in MSVC) in the linking command. And the linker fails with this error when trying to force link system lib files.
pcloudy@PCLOUDY1-W MSYS ~/workspace/buzel
$ cat ./bazel-out/vc_14_0_x64-opt/bin/link_crasher.exe-2.params.msvc
/nologo
/MACHINE:X64
/SUBSYSTEM:CONSOLE
/OUT:bazel-out/vc_14_0_x64-opt/bin/link_crasher.exe
/WHOLEARCHIVE
/DLL
ws2_32.lib
/MT
C:\temp\_bazel_pcloudy\k-8MzLG1\execroot\buzel\bazel-out\vc_14_0_x64-opt\bin\_objs\link_crasher\one.o
(The actual link command would be link @bazel-out/vc_14_0_x64-opt/bin/link_crasher.exe-2.params.msvc)
For VS older than 2015 update 2, /WHOLEARCHIVE doesn't exist, the way Bazel does force linking is to find and pass all the object files directly to linker, the relevant code is
here. You can also tell Bazel not to use /WHOLEARCHIVE option by
export NO_WHOLE_ARCHIVE_OPTION=1
pcloudy@PCLOUDY1-W MSYS ~/workspace/buzel
$ bazel shutdown # This will cause Bazel to re-configure cc toolchain the next you run bazel build
pcloudy@PCLOUDY1-W MSYS ~/workspace/buzel
$ export NO_WHOLE_ARCHIVE_OPTION=1
pcloudy@PCLOUDY1-W MSYS ~/workspace/buzel
$ bazel build :link_crasher
.........................
INFO: Analysed target //:link_crasher.
INFO: Found 1 target...
INFO: From Linking link_crasher.exe:
Creating library bazel-out/vc_14_0_x64-opt/bin/link_crasher.lib and object bazel-out/vc_14_0_x64-opt/bin/link_crasher.exp
Target //:link_crasher up-to-date:
C:/temp/_bazel_pcloudy/k-8MzLG1/execroot/buzel/bazel-out/vc_14_0_x64-opt/bin/link_crasher.exe
INFO: Elapsed time: 39.739s, Critical Path: 2.44s
INFO: Build completed successfully, 4 total actions
pcloudy@PCLOUDY1-W MSYS ~/workspace/buzel
$ cat ./bazel-out/vc_14_0_x64-opt/bin/link_crasher.exe-2.params.msvc
/nologo
/MACHINE:X64
/SUBSYSTEM:CONSOLE
/OUT:bazel-out/vc_14_0_x64-opt/bin/link_crasher.exe
/DLL
ws2_32.lib
/MT
C:\temp\_bazel_pcloudy\k-8MzLG1\execroot\buzel\bazel-out\vc_14_0_x64-opt\bin\_objs\link_crasher\one.o
As you can see, when not using /WHOLEARCHIVE option, the linking action succeed.
And if you want build a shared library (a dll) on Windows, I would recommend not to add `-shared` into linkopts, instead, define the target like this:
cc_binary(
name = "libok.dll",
linkshared = 1,
srcs = [
"one.cc",
],
linkopts = [
"-Wl,ws2_32.lib",
],
)
Thanks,
Yun