bazel-contrib / bazel-contrib/rules_foreign_cc
Can't link libpqxx/libq via Bazel
- Dominant language
- Starlark
- Stars
- 737
- Forks
- 270
- PR merge metrics
- No merged PRs in 30d
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",
],
)
```
Contributor guide
Research direction
Read the WORKSPACE and BUILD definitions, then reproduce the database_tester link using the rules_foreign_cc cmake target and generated libpqxx-7.7.a. Compare the successful local gcc command with Bazel's link inputs; done means database_tester links and runs without missing libpqxx, libpq, or the reported OpenSSL symbols.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake, cpp, postgresql
- Domain
- build-system, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100