Can't use linkopts when building libpqxx using Bazel - c++

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

Related

meson linker error when adding dependency

I am trying to switch to meson and evaluate. I setup a small project and created this meson.build file
project('utils', 'cpp')
json_dep = dependency('jsoncpp')
boost_dep = dependency('boost', modules : [ 'filesystem' ])
occ_dep = dependency('OpenCASCADE', method: 'cmake')
utils_deps = [ occ_dep, json_dep, boost_dep ]
utils_lib = library('utils', dependencies: utils_deps)
If I use utils_deps = [ json_dep, boost_dep ] then the compilation works. However adding occ_dep to the list and compiling produces the following error
FAILED: src/libs/utils/libutils.dylib
c++ -o src/libs/utils/libutils.dylib -Wl,-dead_strip_dylibs -Wl,
-headerpad_max_install_names -Wl,-undefined,error -shared -install_name
#rpath/libutils.dylib -Wl,-rpath,/opt/homebrew/Cellar/jsoncpp/1.9.5/lib
/opt/homebrew/lib/libTKernel.7.6.2.dylib /opt/homebrew/opt/tbb/lib/libtbb.dylib
/opt/homebrew/opt/tbb/lib/libtbbmalloc.dylib /opt/homebrew/lib/libTKMath.7.6.2.dylib
/opt/homebrew/lib/libTKG2d.7.6.2.dylib /opt/homebrew/lib/libTKG3d.7.6.2.dylib
/Library/Developer/CommandLineTools/SDKs/
MacOSX12.sdk/System/Library/Frameworks/AppKit.framework
/Library/Developer/CommandLineTools/SDKs/
MacOSX12.sdk/System/Library/Frameworks/IOKit.framework
/opt/homebrew/lib/libTKService.7.6.2.dylib
/opt/homebrew/opt/freeimage/lib/libfreeimage.dylib
/opt/homebrew/opt/freetype/lib/libfreetype.dylib
/Library/Developer/CommandLineTools/SDKs/
MacOSX12.sdk/System/Library/Frameworks/AppKit.framework
/opt/homebrew/lib/libTKQADraw.7.6.2.dylib
/opt/homebrew/Cellar/jsoncpp/1.9.5/lib/libjsoncpp.dylib
/opt/homebrew/Cellar/boost/1.78.0_1/lib/libboost_filesystem-mt.dylib
ld: can't map file, errno=22 file '/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/System/Library/Frameworks/AppKit.framework' for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I am working on a Macbook Pro M1 cheap.
Thanks
it seems that in the linking file it search a file instead of Library
/Developer/CommandLineTools/SDKs/MacOSX12.sdk/System/Library/Frameworks/AppKit.framework
your dependency against OpenCascade uses cmake method, try to specify also the lib needed using modules method
occ_dep = dependency('OpenCASCADE', method: 'cmake', modules : ['openCascade lib ......'])
see also https://mesonbuild.com/Dependencies.html

Cannot link library for macOS-arm64 with executable for macOS-arm64

I have some trouble with creating a build system on a **Monterey M1 MacBook**:
So far I have a working Makefile to build and link a library.
(simplified: g++ -c all .cpp files into .o files → ar -r <.o files> libmyLibrary.a>
→ works great
THE PROBLEM:
When I try to build an executable binary that uses said libmyLibrary.a. The compilation of source files works fine, but I get the following (seemingly nonsensical) linker warning:
ld: warning: ignoring file /Path/to/lib/libmyLibrary.a,
building for macOS-arm64 but attempting to link with file built for macOS-arm64
→ I ofc then get some Undefined symbols for architecture arm64: ... <stuff from library> referenced from: <stuff from executable>
How can building for the same target as the library be a problem?
This is caused by having an incompatible version of ar installed in your system. ar in homebrew binutils seems to be broken. This can be resolved by symlinking llvm-ar over ar, or using the system ar.
I managed to compile the executable after changing how I link my library. My Makefile also linked a precompiled header .pch file.
After removing that, it worked fine.

Create a fully static C++ binary in Bazel

Is it possible to create a fully static binary in Bazel or is there a bug in GCC or bazel preventing me.
This is the error that I get
/bin/ld.gold: error: /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginT.o: requires dynamic R_X86_64_32 reloc against '__TMC_END__' which may overflow at runtime; recompile with -fPIC
Which if I understand correctly is a version libstdc++ that is not statically linked. Was wondering if there was a version of libstdc++ that is statically linked that I could make Bazel use.
Tried looking at this issue on Bazel's repo but the workarounds mentioned do not work for me
https://github.com/bazelbuild/bazel/issues/8672
Info Dump
Bazel version:3.3.1
GCC version: gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
OS: Ubuntu 20.04 in a Docker container
My build file
cc_binary(
name = "main",
srcs = ["main.cpp"],
linkshared=True,
copts = ["-shared", "-fpic", "-fPIC"],
features = ["fully_static_link"],
)
main.cpp
int main()
{
return 1;
}
Other versions of the build file with similar results
cc_binary(
name = "main",
srcs = ["main.cpp"],
linkshared=True,
linkstatic=1,
linkopts = ["-static"]
)
cc_binary(
name = "main",
srcs = ["main.cpp"],
linkshared=True,
linkstatic=1,
linkopts = ["-static"],
features = ["fully_static_link"],
copts=["-fpic"],
)
TL;DR
Bazel will not compile a fully static binary in Ubuntu 20.04, GCC 9.3.0. I'm seeking to understand if the issue posted is related or if I don't understand something about static linking?

How to use glog with bazel?

I was able to install glog with:
brew install glog
Then I can successfully compile and use it using g++:
g++ src/main/main_logger.cc -std=c++17 -lglog
How can I do this with bazel?
I get this error:
fatal error: 'glog/logging.h' file not found
#include <glog/logging.h>
^~~~~~~~~~~~~~~~
1 error generated.
UPDATE:
Instead of installing and building glog locallay, I ended up referencing it as a git repo in the WORKSPACE file:
git_repository(
name = "glog",
remote = "https://github.com/google/glog.git",
tag = "v0.5.0",
)
Now I can depend on it in my cc_binary rules like this:
cc_binary(
name = "main_logger",
srcs = ["main_logger.cc"],
deps = [
"//src/lib:CPPLib",
"#com_github_gflags_gflags//:gflags",
"#glog",
],
)
Complete example here.
There is already a doc about to use glog within a project which uses the Bazel build tool. link
Then you can create a BUILD file, and use bazel build //src/main:main_logger to build it.
cc_binary(
name = "main_logger",
srcs = ["main_logger.cc"],
deps = ["#com_github_google_glog//:glog"],
)

Trouble setting up Lua in Xcode

How can I include Lua in my project in Xcode?
I have installed Lua via the instructions on the website (curl, extract, make macosx install, etc).
I can reference lua
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
But when I go to use it, I get an error (even writing something as simple as the following)
lua_State *L = luaL_newstate();
lua_close(L);
It tells me :
Undefined symbols for architecture x86_64:
"_luaL_newstate", referenced from:
_main in main.o
"_lua_close", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any advice would be much appreciated. If you need further information, let me know and I can edit it in. Thanks!
EDIT:
Added the -llua flag (SFML included the /usr/local/lib and include). Now getting "ignoring file /usr/local/lib/liblua.a, file was built for archive which is not the architecture being linked (i386): /usr/local/lib/liblua.a"
EDIT2:
I changed Base SDK to Latest OS X (OS X 10.9) and Build Active Architecture Only to "Yes" and now it will compile.
In Xcode, select < ProjectName > with blue icon on top of the left pane (where all the sources are), then in main window select a target under Targets. In Build Settings tab, select All instead of Basic and set following parameters:
Other Linker Flags = -llua
Header Search Paths = /usr/local/include
Library Search Paths = /usr/local/lib
Assuming Lua headers were installed in /usr/local/include, and liblua.a in /usr/local/lib.
You may also use search field to find them.