bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Wrong linking between binaries

Open steffenroeber opened this issue 9 months ago • 0 comments

Description of the bug:

Hi, after recent update to bazel 7.1 I got follwing issue.. I have two shared libs (using cc_binary) and one executable. If I run main, I'd expect to show same values in printvs independednfrom where I call it. But I get, a 0 when calling from main. I don't understand why, because the vs() symbols should be located in lib1.so, wich sould be shared to every consumer. Has this changed in bazel and how to do correctly again?

Which category does this issue belong to?

C++ Rules

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

lib1.h

#pragma once #include

struct Services { static std::vector &vs(); static void printvs(); }; lib1.cc

#include "lib1.h"

#include

std::vector& Services::vs() { static std::vector v; return v; }

void Services::printvs() { std::cout << Services::vs().size() << std::endl; } lib2.cc

#include "lib1.h"

static bool init() { Services::vs().push_back(1); Services::vs().push_back(2); Services::printvs(); return true; }

[[maybe_unused]] bool i = init(); main.cc #include "lib1.h"

int main() { Services::printvs(); } BUILD.bazel

cc_library( name = "lib1_lib", srcs = ["lib1.cc"], hdrs = ["lib1.h"], visibility = ["//visibility:public"], )

cc_binary( name = "lib1", srcs = ["lib1_lib"], visibility = ["//visibility:public"], linkshared = 1, )

cc_library( name = "lib2_lib", srcs = ["lib2.cc"], deps = ["lib1_lib"], visibility = ["//visibility:public"], )

cc_binary( name = "lib2", srcs = ["lib2_lib"], visibility = ["//visibility:public"], linkshared = 1, )

cc_binary( name = "main", srcs = ["main.cc", ":lib2", ":lib1"], deps = [":lib1_lib"], )

Which operating system are you running Bazel on?

linux

What is the output of bazel info release?

Invocation ID: 434bb820-3b50-468f-97f7-237571c04586 release 7.1.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

steffenroeber avatar Apr 27 '24 20:04 steffenroeber