Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building with bazel error: "ar: libdeps specified more than once" or "undefined reference to PQ" #804

Closed
manoelmenezes opened this issue Mar 6, 2024 · 8 comments

Comments

@manoelmenezes
Copy link

manoelmenezes commented Mar 6, 2024

Hello,
Could you help me solving the following issue? Thank you!
I am trying to build it with bazel.

Machine:
Linux 6.1.0-18-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.76-1 (2024-02-01) x86_64 GNU/Linux

I already installed:

sudo apt install postgresql postgresql-contrib
sudo apt-get install -y libpq-dev

My WORKSPACE file:

http_archive(
    name = "rules_foreign_cc",
    patch_args = ["-p1"],
    sha256 = "62e364a05370059f07313ec46ae4205af23deb00e41f786f3233a98098c7e636",
    strip_prefix = "rules_foreign_cc-ae4ff42901354e2da8285dac4be8329eea2ea96a",
    url = "https://github.com/bazelbuild/rules_foreign_cc/archive/ae4ff42901354e2da8285dac4be8329eea2ea96a.tar.gz",  # v 0.7.1
)

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies(
    register_built_tools = True,
)

new_git_repository(
    name = "pqxx",
    build_file = "//:libpqxx.BUILD",
    commit = "9d2a459f76f52ea0df7b9b306b27fba84bb82e5f",
    remote = "https://github.com/jtv/libpqxx",
)

libpqxx.BUILD:

load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")

package(
    default_visibility = ["//visibility:public"],
)

filegroup(
    name = "all_srcs",
    srcs = glob(["**"]),
    visibility = ["//:__pkg__"],
)

cmake(
    name = "pqxx",
    build_args = [
        "--verbose",
    ],
    cache_entries = {
        "CMAKE_C_FLAGS": "-fPIC",
        "CMAKE_STATIC_LINKER_FLAGS": "-lpqxx -lpq",
        "BUILD_SHARED_LIBS": "off",
         "PostgreSQL_TYPE_INCLUDE_DIR": "/usr/include/postgresql",
        "PostgreSQL_INCLUDE_DIR": "/usr/include/postgresql",
         "PostgreSQL_LIBRARY": "/usr/lib/x86_64-linux-gnu/libpq.so",
    },
    lib_source = "@pqxx//:all_srcs",
    visibility = ["//visibility:public"],
    out_static_libs = ["libpqxx.a"],
    targets = ["pqxx"],
)

The error is:

...
[ 95%] Building CXX object src/CMakeFiles/pqxx.dir/wait.cxx.o
cd /home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/bazel-out/k8-fastbuild/bin/external/pqxx/pqxx.build_tmpdir/src && /home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/gcc_toolchain_x86_64/bin/gcc --sysroot=/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/sysroot_x86_64/  -I/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/bazel-out/k8-fastbuild/bin/external/pqxx/pqxx.build_tmpdir/include -I/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/pqxx/include -I/usr/include/postgresql -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x --sysroot /home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/sysroot_x86_64/ -no-canonical-prefixes -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -fdiagnostics-color=always -nostdinc -nostdinc++ -B/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/gcc_toolchain_x86_64/bin -isystem/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/sysroot_x86_64//include/c++/10.3.0 -isystem/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/sysroot_x86_64//include/c++/10.3.0/x86_64-linux -isystem/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/sysroot_x86_64//lib/gcc/x86_64-linux/10.3.0/include-fixed -isystem/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/sysroot_x86_64//lib/gcc/x86_64-linux/10.3.0/include -isystem/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/sysroot_x86_64//usr/include -std=c++17 -O3 -DNDEBUG -std=c++17 -MD -MT src/CMakeFiles/pqxx.dir/wait.cxx.o -MF CMakeFiles/pqxx.dir/wait.cxx.o.d -o CMakeFiles/pqxx.dir/wait.cxx.o -c /home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/pqxx/src/wait.cxx
x86_64-linux-gcc: WARNING: unsafe header/library path used in cross-compilation: '-I/usr/include/postgresql'
[100%] Linking CXX static library libpqxx-7.9.a
cd /home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/bazel-out/k8-fastbuild/bin/external/pqxx/pqxx.build_tmpdir/src && /home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/external/cmake-3.22.1-linux-x86_64/bin/cmake -P CMakeFiles/pqxx.dir/cmake_clean_target.cmake
cd /home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/bazel-out/k8-fastbuild/bin/external/pqxx/pqxx.build_tmpdir/src && /home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/external/cmake-3.22.1-linux-x86_64/bin/cmake -E cmake_link_script CMakeFiles/pqxx.dir/link.txt --verbose=1
/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/external/gcc_toolchain_x86_64/bin/ar qc libpqxx-7.9.a -lpqxx -lpq CMakeFiles/pqxx.dir/array.cxx.o CMakeFiles/pqxx.dir/binarystring.cxx.o CMakeFiles/pqxx.dir/blob.cxx.o CMakeFiles/pqxx.dir/connection.cxx.o CMakeFiles/pqxx.dir/cursor.cxx.o CMakeFiles/pqxx.dir/encodings.cxx.o CMakeFiles/pqxx.dir/errorhandler.cxx.o CMakeFiles/pqxx.dir/except.cxx.o CMakeFiles/pqxx.dir/field.cxx.o CMakeFiles/pqxx.dir/largeobject.cxx.o CMakeFiles/pqxx.dir/notification.cxx.o CMakeFiles/pqxx.dir/params.cxx.o CMakeFiles/pqxx.dir/pipeline.cxx.o CMakeFiles/pqxx.dir/result.cxx.o CMakeFiles/pqxx.dir/robusttransaction.cxx.o CMakeFiles/pqxx.dir/row.cxx.o CMakeFiles/pqxx.dir/sql_cursor.cxx.o CMakeFiles/pqxx.dir/strconv.cxx.o CMakeFiles/pqxx.dir/stream_from.cxx.o CMakeFiles/pqxx.dir/stream_to.cxx.o CMakeFiles/pqxx.dir/subtransaction.cxx.o CMakeFiles/pqxx.dir/time.cxx.o CMakeFiles/pqxx.dir/transaction.cxx.o CMakeFiles/pqxx.dir/transaction_base.cxx.o CMakeFiles/pqxx.dir/util.cxx.o CMakeFiles/pqxx.dir/version.cxx.o CMakeFiles/pqxx.dir/wait.cxx.o
/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-ar: libdeps specified more than once
make[3]: *** [src/CMakeFiles/pqxx.dir/build.make:514: src/libpqxx-7.9.a] Error 1
make[3]: Leaving directory '/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/bazel-out/k8-fastbuild/bin/external/pqxx/pqxx.build_tmpdir'
make[2]: *** [CMakeFiles/Makefile2:134: src/CMakeFiles/pqxx.dir/all] Error 2
make[2]: Leaving directory '/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/bazel-out/k8-fastbuild/bin/external/pqxx/pqxx.build_tmpdir'
make[1]: *** [CMakeFiles/Makefile2:141: src/CMakeFiles/pqxx.dir/rule] Error 2
make[1]: Leaving directory '/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/79/execroot/__main__/bazel-out/k8-fastbuild/bin/external/pqxx/pqxx.build_tmpdir'
make: *** [Makefile:189: pqxx] Error 2

If I remove "CMAKE_STATIC_LINKER_FLAGS": "-lpqxx -lpq", from the file libpqxx.BUILD. It fails with (a lot of undefined reference to PQ - the error below has been truncated):

INFO: Analyzed target //main:rinha (1 packages loaded, 355 targets configured).
ERROR: /home/manoelmenezes/projects/rinha-backend-2024-q1/main/BUILD:3:10: Linking main/rinha failed: (Exit 1): gcc failed: error executing CppLink command (from target //main:rinha) 
  (cd /home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/sandbox/linux-sandbox/81/execroot/__main__ && \
  exec env - \
    PATH=/home/manoelmenezes/.cache/bazelisk/downloads/sha256/1ca5d63a30244788ff9a5b7c35584f57111a68c82bbff2929ec7b2e99f35c7a8/bin:/home/manoelmenezes/bazelisk/bazelisk-linux-amd64:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games \
    PWD=/proc/self/cwd \
  external/gcc_toolchain_x86_64/bin/gcc -o bazel-out/k8-fastbuild/bin/main/rinha bazel-out/k8-fastbuild/bin/main/_objs/rinha/main.pic.o bazel-out/k8-fastbuild/bin/model/libclient-service.a bazel-out/k8-fastbuild/bin/external/served/libserved.a bazel-out/k8-fastbuild/bin/external/boost/libasio.a bazel-out/k8-fastbuild/bin/external/boost/libchrono.a bazel-out/k8-fastbuild/bin/external/boost/libsystem.a bazel-out/k8-fastbuild/bin/external/boost/libcontainer.a bazel-out/k8-fastbuild/bin/external/boost/libatomic.a bazel-out/k8-fastbuild/bin/external/boost/libatomic_sse.a bazel-out/k8-fastbuild/bin/external/boost/libregex.a bazel-out/k8-fastbuild/bin/external/boost/libexception.a bazel-out/k8-fastbuild/bin/external/pqxx/pqxx/lib/libpqxx.a -lpthread -Wl,-S -Wl,-z,relro,-z,now -pass-exit-codes -lm -lstdc++ --sysroot external/sysroot_x86_64/ -Bexternal/gcc_toolchain_x86_64/bin -Bexternal/sysroot_x86_64//usr/lib -Bexternal/sysroot_x86_64//lib64 -Lexternal/sysroot_x86_64//lib64 -Lexternal/sysroot_x86_64//usr/lib -Lexternal/sysroot_x86_64//lib/gcc/x86_64-linux/10.3.0)
# Configuration: 0e913ec885832ba80eb84357b2922ef6ed6bd55134ad82350d1f632bf75f4fa4
# Execution platform: @@local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-ld: bazel-out/k8-fastbuild/bin/external/pqxx/pqxx/lib/libpqxx.a(connection.cxx.o): in function `pqxx::connection::connection(pqxx::connection::connect_mode, pqxx::zview)':
connection.cxx:(.text+0x5e9): undefined reference to `PQconnectStart'
/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-ld: connection.cxx:(.text+0x63f): undefined reference to `PQstatus'
/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-ld: bazel-out/k8-fastbuild/bin/external/pqxx/pqxx/lib/libpqxx.a(connection.cxx.o): in function `pqxx::connection::poll_connect()':
connection.cxx:(.text+0x693): undefined reference to `PQconnectPoll'
/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-ld: connection.cxx:(.text+0x6bc): undefined reference to `PQstatus'
/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-ld: bazel-out/k8-fastbuild/bin/external/pqxx/pqxx/lib/libpqxx.a(connection.cxx.o): in function `pqxx::connection::sock() const &':
connection.cxx:(.text+0x7cd): undefined reference to `PQsocket'

...

/home/manoelmenezes/.cache/bazel/_bazel_manoelmenezes/67983900e4065458c8253806ac6d53ac/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-ld: bazel-out/k8-fastbuild/bin/external/pqxx/pqxx/lib/libpqxx.a(util.cxx.o): in function `pqxx::describe_thread_safety()':
util.cxx:(.text.unlikely+0x330): undefined reference to `PQisthreadsafe'
collect2: error: ld returned 1 exit status
@tt4g
Copy link
Contributor

tt4g commented Mar 6, 2024

From the build message, it looks like you are building pqxx.
Isn't -lpqxx unnecessary?

BTW. It is recommended that you ask questions about how to use Bazel in the Bazel community.

@jtv
Copy link
Owner

jtv commented Mar 7, 2024

(It is normal that if you remove -lpq you get these link errors about missing functions like PQexec() etc. You do need the -lpq.)

@manoelmenezes
Copy link
Author

manoelmenezes commented Mar 7, 2024

thank you @jtv . Do you know how to solve the issue of "ar: libdeps specified more than once"? this error happens when -lpq is included.

@jtv
Copy link
Owner

jtv commented Mar 7, 2024

I'm sorry, I have nothing beyond what @tt4g already mentioned. I don't know bazel, and it's hard to read those long command lines on my phone screen.

@tt4g
Copy link
Contributor

tt4g commented Mar 7, 2024

I don't know Bazel's working specifications, but if Bazel is running CMake to build libpqxx, isn't the CMAKE_STATIC_LINKER_FLAGS variable not needed?
I think you are reporting a ar: libdeps specified more than once error because you are also adding the CMAKE_STATIC_LINKER_FLAGS linker option -lpq within CMake.

@jtv
Copy link
Owner

jtv commented Apr 23, 2024

@manoelmenezes did you try @tt4g's suggestion? I have no further ideas I'm afraid. I've never worked with Bazel.

@jtv
Copy link
Owner

jtv commented May 13, 2024

@manoelmenezes this looks like may also be a simple command line problem at its core. Can you see the command line that cmake produced for linking?

We're getting a bunch of bug reports now from people who run some variant of g++ -lpqxx -lpq myapp.cxx or g++ myapp.cxx -lpq -lpqxx, when they need to use the g++ myapp.cxx -lpqxx -lpq ordering instead.

@jtv
Copy link
Owner

jtv commented Jun 27, 2024

It's been a while. I would guess that the problem has been resolved... @manoelmenezes did you ever find out what the problem was?

I'd like to close this ticket if so, but it'd be nice to have the solution for future readers.

@jtv jtv closed this as completed Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants