Skip to content

Can't link libpqxx/libq via Bazel #927

Open
@conniemzhang

Description

Hello, I am trying to use libpqxx by using cmake to build a static library using Bazel but I am stuck. I am using rules_foreign_cc cmake() which 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). When install it 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.

No matter what, I always get the error:

ld: library not found for -lpqxx

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 the same "library not found" error. 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. Am I building my cmake wrong? I am new to C++, am I missing important debugging steps?

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",
      ],
)

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions