I have following 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. Could some óne explain what’s wrong?
lib1.h
#pragma once
#include <vector>
struct Services
{
static std::vector<int> &vs();
static void printvs();
};
lib1.cc
#include "lib1.h"
#include <iostream>
std::vector<int>& Services::vs()
{
static std::vector<int> 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"],
)