conndor
conndor

Reputation: 11

Can't use linkopts when building libpqxx using Bazel

I am trying to use Bazel with libpqxx, which does not have native Bazel support. Rather than trying to write my own BUILD file, I am using rules_foreign_cc cmake() to translate the provided CMakeLists.txt, and this builds a library named pqxx and links a static library libpqxx-7.7.a. When I try to add this as a dependency to a cc_binary, it is able to see all the headers, but is not able to run the program.

When installed locally, gcc requires the libraries -lpqxx -lpq (the C++ and C bindings). Locally, all the commands work fine: For example: g++ -L/usr/local/lib -lpqxx -lpq foo.cpp where /usr/local/lib is where the library was installed. I cannot duplicate this behavior in Bazel.

I can see that there is a libpqxx-7.7.a file in bazel-bin, but adding the path from "$(bazel info bazel-bin)/workspace/libpqxx-7.7.a" throws a error: ld: library not found for -lpqxx.

Leaving the link options out produces

Undefined symbols for architecture arm64:

"_ASN1_STRING_get0_data", referenced from: _pgtls_verify_peer_name_matches_certificate_guts in libpq.a(fe-secure-openssl.o) "_ASN1_STRING_length", referenced from: _pgtls_verify_peer_name_matches_certificate_guts in libpq.a(fe-secure-openssl.o) "_BIO_clear_flags", referenced from: _my_sock_write in libpq.a(fe-secure-openssl.o) _my_sock_read in libpq.a(fe-secure-openssl.o)

which I believe is a linking error. I am new to C++, and am trying to translate the CMakeLists to work with Bazel. Where am I building my cmake wrong?

WORKSPACE

http_archive(
    name = "libpqxx",
    url = "https://github.com/jtv/libpqxx/archive/refs/heads/master.zip",
    build_file_content = _ALL_CONTENT,
    strip_prefix = "libpqxx-master",
)

BUILD

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")

cmake(
    name = "pqxx",
    cache_entries = {
        "DPostgreSQL_ROOT=": "/Library/PostgreSQL/14", 
        "DSKIP_BUILD_TEST": "on"
    },
    lib_source = "@libpqxx//:all_srcs",
    visibility = ["//visibility:public"],
    out_static_libs = ["libpqxx-7.7.a"],
    targets = ["pqxx"]
)


cc_binary(
    name = "database_tester",
    srcs = ["database_access.cpp"],
    deps = [
          ":pqxx",
      ],
)

Upvotes: 1

Views: 353

Answers (1)

Kunshan
Kunshan

Reputation: 11

It seems that you tried to link the static libpq to your binary. In that case, you need to provide all static libs which libpq depends.

According to the error log, the linker can't solve openssl functions. So make sure you have installed openssl on your system and provide its static lib to cc_binary as deps.

Upvotes: 1

Related Questions