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"],
)
Related
By default, Bazel supports #include'ing files relative to the WORKSPACE directory. However, as soon as a relative #include path from the workspace root contains a symlink, this seems to no longer hold. For example, suppose we have some directory structure as follows:
WORKSPACE
libs/foo/src/foo.cpp
libs/foo/itf/foo.h
libs/foo/BUILD
foo -> libs/foo
Where foo in the root directory is a symlink to libs/foo. Suppose we have:
foo.h:
#pragma once
void foo();
foo.cpp:
#include "foo/itf/foo.h"
void foo() {}
BUILD:
load("#rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "foo",
srcs = ["src/foo.cpp"],
hdrs = ["itf/foo.h"],
)
In this case, bazel build //... gives the following error message:
Starting local Bazel server and connecting to it...
INFO: Analyzed 2 targets (38 packages loaded, 166 targets configured).
INFO: Found 2 targets...
ERROR: [...]/foo/libs/foo/BUILD:3:11: Compiling libs/foo/src/foo.cpp failed: (Exit 1): gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 16 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
libs/foo/src/foo.cpp:1:10: fatal error: foo/itf/foo.h: No such file or directory
1 | #include "foo/itf/foo.h"
| ^~~~~~~~~~~~~~~
Is this a known issue or known behavior?
Bazel is not aware of the symlink. Therefore this does not work - you have to change your include path to libs/foo/itf/foo.h. You can add includes = ["libs"] to the cc_library to get around this.
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",
],
)
I'm writing a simple Bazel BUILD file but I have to include MKL library.
My main.c include this library:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "omp.h"
#include "mkl.h"
#include "mkl_types.h"
#include "mkl_dfti.h"
The last 3 libs that are located in $MKLROOT set by a module environment.
My bazel build file is:
load("#rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "mkl_headers",
srcs = glob(["include/*(.cc|.cpp|.cxx|.c++|.C|.c|.h|.hh|.hpp|.ipp|.hxx|.inc|.S|.s|.asm|.a|.lib|.pic.a|.lo|.lo.lib|.pic.lo|.so|.dylib|.dll|.o|.obj|.pic.o)"]),
includes = ["include"],
visibility = ["//visibility:public"],
)
cc_library(
name = "mkl_libs_linux",
srcs = [
"lib/libiomp5.so",
"lib/libmklml_intel.so",
],
visibility = ["//visibility:public"],
)
cc_binary(
name = "mklfft",
srcs = ["main.c"],
deps = [
":mkl_libs_linux"
],
)
I tried to take an example from the BUILD tensorflow mkl file but it's very complicated.
The bazel build command return:
INFO: Analyzed target //mklfft:mklfft (2 packages loaded, 8 targets configured).
INFO: Found 1 target...
ERROR: missing input file 'mklfft/mkl.h', owner: '//mklfft:mkl.h'
ERROR: missing input file 'mklfft/mkl_dfti.h', owner: '//mklfft:mkl_dfti.h'
ERROR: missing input file 'mklfft/mkl_types.h', owner: '//mklfft:mkl_types.h'
ERROR: /C/mklfft/BUILD:6:1: //mklfft:mkl: missing input file '//mklfft:mkl.h'
ERROR: /C/mklfft/BUILD:6:1: //mklfft:mkl: missing input file '//mklfft:mkl_dfti.h'
ERROR: /C/mklfft/BUILD:6:1: //mklfft:mkl: missing input file '//mklfft:mkl_types.h'
ERROR: missing input file 'mklfft/readFile.c', owner: '//mklfft:readFile.c'
Target //mklfft:mklfft failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /C/mklfft/BUILD:6:1 3 input file(s) do not exist
INFO: Elapsed time: 0.342s, Critical Path: 0.03s
INFO: 0 processes.
Can you clarify the method to link external shared libraries with bazel?
linking with "lib/libiomp5.so",
"lib/libmklml_intel.so",
it's not enough. You need to add libmkl_intel_thread.so and -libmkl_core.so also.
Please check mkl linker adviser to see what mkl suggests to use:
https://software.intel.com/content/www/us/en/develop/articles/intel-mkl-link-line-advisor.html
I am building a test hello-world executable module linking to a simple shared object library 'libshared' which, in turn, links to a static library 'libsodium' that was built in the 'depends' directory.
OS is Ubuntu 16.04.6 LTS.
When I build this for Linux the build is okay.
When I build this for Mingw this libtool error message is printed:
*** Warning: This system cannot link to static lib archive /home/ubuntu/repo/test-dll/depends/x86_64-w64-mingw32/share/../lib/libsodium.la.
*** I have the capability to make that library automatically link in when
*** you link to this library. But I can only do this if you have a
*** shared version of the library, which you do not appear to have.
and recipe for target 'libshared.la' failed.
The Makefile.am:
.PHONY: gen
.INTERMEDIATE: $
# shared lib
lib_LTLIBRARIES = libshared.la
libshared_la_SOURCES = \
src/example_dll.c
libshared_la_CPPFLAGS = -I$(top_srcdir)/src -I /src
libshared_la_CFLAGS = -DBUILDING_EXAMPLE_DLL -O2 -Wno-unused-parameter
libshared_la_LDFLAGS = -no-undefined
libshared_la_LIBADD = -lsodium
#libshared_la_LIBADD = $(prefix)/lib/libsodium.la
# executable
inst_PROGRAMS = testexe
instdir=$(prefix)/bin
testexe_SOURCES = \
src/example_exe.c
testexe_CFLAGS = -DBUILDING_EXAMPLE_DLL -O2 -Wno-unused-parameter
testexe_CPPFLAGS = -I$(top_srcdir)/src
testexe_LDADD = libshared.la
How I invoke configure and make (build_win.sh):
export HOST=x86_64-w64-mingw32
CXX=x86_64-w64-mingw32-g++-posix
CC=x86_64-w64-mingw32-gcc-posix
PREFIX="$(pwd)/depends/$HOST"
set -eu -o pipefail
set -x
cd depends/ && make HOST=$HOST V=1 NO_QT=1
cd ../
./autogen.sh
CONFIG_SITE=$PWD/depends/x86_64-w64-mingw32/share/config.site ./configure --prefix="${PREFIX}" --host=x86_64-w64-mingw32
CC="${CC} -g " CXX="${CXX} -g " make V=1
configure.ac params is trivial
The only way I managed to build it was if I manually moved libsodium.la into the root of the distribution and 'libsodium.a' in the '.libs' subdirectory and manually corrected libsodium.la parameters 'libdir to '' (was '/home/ubuntu/repo/test-dll/depends/x86_64-w64-mingw32/share/../lib/libsodium.a') and 'installed' to 'no' (was 'yes').
So my questions are:
Why is it trying to link libsodium as a shared library when it is in 'depends' directory and set as 'installed=yes' (I need to link it as static)?
Could I have correct automake settings for MINGW to link my shared library to static libs in 'depends' directory?
Just found a solution (or workaround) how to make this link on Mingw (and produce dll):
instead of libshared_la_LIBADD = -lsodium that causes libtool to search for the libsodium shared library, I just passed an option -Wl,-lsodium to the linker in libshared_la_CFLAGS.
I'm trying using package manager nix for my C++ project.
default.nix:
{ pkgs ? import <nixpkgs> {} }:
let
stdenv = pkgs.stdenv;
in rec {
myProject = stdenv.mkDerivation {
name = "lynx";
version = "dev-0.4.0";
buildInputs = [
pkgs.cmake
pkgs.gtest
];
};
}
I built the project in its directory by using cmake without any problems. Then I exported the project as an Eclipse project (I did it under nix-shell):
cd ..
mkdir lynx_eclipse
cd lynx_eclipse
cmake -G"Eclipse CDT4 - Unix Makefiles" -D PLATFORM:STRING="posix" -DCMAKE_BUILD_TYPE=Debug ../lynx/
Having opened the exported project in Eclipse I found that include paths are fine and point to /nix/store/*. But when I try to build the project I have an error:
gtest/gtest.h: No such file or directory
I see that Eclipse doesn't add nix'es paths to the compiler flags:
/nix/store/ix03iknfgyrx7421fppjdczd9r4sw7pz-gcc-wrapper-5.3.0/bin/g++ -I/home/ubuntu-pc/dcs/lynx/inc -I/home/ubuntu-pc/dcs/lynx_eclipse/inc -I/home/ubuntu-pc/dcs/lynx/test/./inc -std=c++11 -static-libstdc++ -g -o CMakeFiles/test_utils.dir/utils_test.cpp.o -c /home/ubuntu-pc/dcs/lynx/test/utils_test.cpp
That could be reason of the problem but I don't know how to change Eclipse's behaviour.
Judging by the number of the views the problem is very specific. However, I found the solution somehow.
After the export to Eclipse project I made builder.sh file:
export NIX_PATH=nixpkgs=/home/xxxx/.nix-defexpr/channels/nixpkgs
/home/xxxx/.nix-profile/bin/nix-shell default.nix --run "make $1"
Then I set the file as a build command in C\C++ General -> C\C++ Make Project
bash ${project_loc}/build.sh
That's all.