dyld: Library not loaded: #rpath/libopenblas.dylib - c++

When I make my file, I have this error:
dyld: Library not loaded: #rpath/libopenblas.dylib Referenced
from: /Users/danyunhe2/reinf_learning2/cpp_original/./navig_test
Reason: image not found Abort trap: 6
I tried ln -sf <original path> /usr/local/lib but it didn't work.
From brew info openblas I got:
openblas: stable 0.3.5 (bottled), HEAD [keg-only]
Optimized BLAS library
https://www.openblas.net/
/usr/local/Cellar/openblas/0.3.5 (22 files, 120.7MB)
Poured from bottle on 2019-02-18 at 01:27:14
From: https://github.com/Homebrew/homebrew- core/blob/master/Formula/openblas.rb
==> Dependencies
Required: gcc ✔
==> Options
--HEAD
Install HEAD version
==> Caveats
openblas is keg-only, which means it was not symlinked into /usr/local,
because macOS provides BLAS and LAPACK in the Accelerate framework.
For compilers to find openblas you may need to set:
export LDFLAGS="-L/usr/local/opt/openblas/lib"
export CPPFLAGS="-I/usr/local/opt/openblas/include"
It told me to set compiler with LDFLAGS and CPPFLAGS. I tried but it didn't work. Anyone know how to deal with this?
I have my config.mk as:
# C++ compiler
cxx=g++-7 -fopenmp
# Compilation flags
cflags=-Wall -ansi -pedantic -O3
# BLAS/LAPACK flags for linear algebra
lp_lflags=-framework Accelerate
# FFTW flags (installed via Homebrew)
fftw_iflags=
fftw_lflags=-lfftw3
# libpng flags (installed via Homebrew)
png_iflags=
png_lflags=-lpng
and Makefile:
# Load the common configuration file
include config.mk
iflags=`gsl-config --cflags`
lflags=`gsl-config --libs`
objs=navigate.o reinf_learn.o common.o
src=$(patsubst %.o,%.cc,$(objs))
execs=navig_test
all:
$(MAKE) executables
executables: $(execs)
depend: $(src)
$(cxx) $(iflags) -MM $(src) >Makefile.dep
-include Makefile.dep
navig_test: navig_test.cc $(objs)
$(cxx) $(cflags) $(iflags) -o $# $^ $(lflags)
%.o: %.cc
$(cxx) $(cflags) $(iflags) -c $<
clean:
rm -f $(execs) $(objs)
.PHONY: clean all executables depend

On OSX it's DYLD_LIBRARY_PATH, which you need to specify at runtime like so:
export DYLD_LIBRARY_PATH=/usr/local/opt/openblas/lib
However, please appreaciate brew's warning regarding the Accelerate framework. It is way faster with many of the BLAS operations on all sorts of levels. You will just make programs run slower.

Related

Building a C/C++ program for Windows/MacOS/Linux with dependencies

I need to build a program on Windows, MacOS and Linux which relies on multiples dependencies. I tried to limit them as much as I could but I should ended up using WxWidgets, libcurl, openssl and libarchive.
I choose those dependencies because I must be able to:
compute a md5 hash (openssl)
decompress a zip archive (libarchive)
provide a GUI (WxWidgets)
use HTTPS (libcurl)
I am aware that this might be an overkill to import 5 libraries to only do those 5 things but I do not know any better solution yet. As I am writing this, I'm only using libcurl and openssl and building for Windows, MacOS and Linux is already very difficult for me.
I would like to distribute a standalone executable for each platform in addition to the source code, to build my project, I use the following Makefile :
CXX = g++
NAME = foo
SRC = $(wildcard src/*.cpp)
OBJ = $(SRC:.cpp=.o)
CXXFLAGS = -I./inc -std=c++11 -Wall -Wextra -Werror -g
all : $(NAME)
$(NAME) : $(OBJ)
$(CXX) $(CXXFLAGS) -o $# $^ -lcurl -lssl -lcrypto
clean :
rm -rf $(SRC:.cpp=.o)
fclean : clean
rm -rf $(NAME) test
re : fclean all
reclean : fclean all clean
.PHONY : all clean fclean re reclean
This worked great(even on Windows thanks to MSYS2 and MINGW) until I tried to statically linked each library to distribute a standalone binary :
$(NAME) : $(OBJ)
$(CXX) $(CXXFLAGS) -o $# $^ -lcurl -lssl -lcrypto
becamed
$(NAME) : $(OBJ)
$(CXX) $(CXXFLAGS) -o $# $^ libcurl.a libssl.a libcrypto.a
On Linux(Ubuntu 22.04), I downloaded curl and openssl source codes and built them using -static flags, copied the *.a inside my project's directory and I was done.
Now I'm trying to build curl and openssl on Windows with MSYS2, most of my attempts failed with error messages and took forever(> 30 minutes) to complete the build process. After struggling a little too much time, I finally managed to compile openssl by adding -lcrypto -lcrypt32..?
I do not think I will be able to finish this project with this "building process" as I still need to figure it out how build and link libarchive and WxWidgets. Is there anything I am missed out regarding the building process ? Should I pick others libraries ? Or It is just how it is ?
Thanks for your help.

How do I compile a c++ program into a 32-bit executable instead of 64-bit

So I am trying to compile a c++ program and have the executable be 32-bit instead of 64-bit. The system (using a program to simulate a system) I want to run it on is 32-bit and seeing as compiling the program yields 64-bit ELF files I cannot run them. I have added the -m32 flag to the makefile and when compiling i get the following errors:
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
when using sudo apt install -lstdc++ it simply says it cannot find the library. Anybody perhaps able to give me some direction? I am running all of this using remote wsl in visual studio code on a windows 10 machine. Ultimately I just want the compiled executables from this program to be 32-bit ELFs instead of 64-bit.
This is the make file The program im trying to compile is a benchmark suite located here https://github.com/alifahmed/hopscotch. This is the makefile otherwise located in /cpu/2_bandwidth:
TARGET = bandwidth
.PHONY: all clean $(TARGET)
# directories
INC_DIR = ../include
KERN_DIR = ../kernels
CMN_DIR = ../common
OBJ_DIR = obj
# compiler flags ADDED -m32 FLAG
CXX = g++
CXXFLAGS = -m32 -O3 -fopenmp -march=native -I$(INC_DIR) -std=c++14 $(USER_DEFS)
# header files
HEADERS = $(wildcard $(INC_DIR)/*.h)
# src files
SRC = $(wildcard *.cpp) $(wildcard $(KERN_DIR)/*.cpp) $(wildcard $(CMN_DIR)/*.cpp)
# object files
OBJ = $(SRC:.cpp=.o)
all: $(TARGET)
clean:
#rm -rf $(OBJ)
#rm -rf $(TARGET)
#echo "Cleaned..."
%.o: %.cpp $(HEADERS)
$(CXX) $(CXXFLAGS) -c -o $# $<
$(TARGET): $(OBJ)
$(CXX) $(CXXFLAGS) $^ -o $#```
That's easy, just install the 32-bit development libraries
$ sudo apt update
$ sudo apt install gcc-multilib g++-multilib

Makefile unable to link libraries during runtime

So I am using nvidia's deepstream sdk and trying to modify the makefile of one of the sample examples given as I wish to link and add my own libraries. This is the makefile being employed where I am setting the path of the CUSTOM_LIB to point to the location of my library. The issue is the project gets compiled successfully but during run time, its unable to find the custom library. I performed ldd on the executable generated and there also it was showing the library as 'not found'. I think it's something to do with rpath but I am not sure about that.
APP:= sample
TARGET_DEVICE = $(shell gcc -dumpmachine | cut -f1 -d -)
NVDS_VERSION:=4.0
LIB_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/lib/
ifeq ($(TARGET_DEVICE),aarch64)
CFLAGS:= -DPLATFORM_TEGRA
endif
CUDA_VER:=10.0
CC:=g++
SRCS:= $(wildcard ../src/*.c)
#SRCS+= $(wildcard ../../apps-common/src/*.c)
#SRCS+=
INCS:= $(wildcard ../include/*.h)
PKGS:= gstreamer-1.0 gstreamer-video-1.0 x11 opencv
OBJS:= $(SRCS:.c=.o)
CFLAGS+= -I../include -I/usr/include -I$(CUSTOM_LIB)/include -I/usr/local/cuda-10.0/targets/aarch64-linux/include/ -I/usr/include/jsoncpp -DDS_VERSION_MINOR=0 -DDS_VERSION_MAJOR=4 -fpermissive -Wnarrowing
LIBS+= -L$(LIB_INSTALL_DIR) -L/usr/lib/aarch64-linux-gnu -L$(CUSTOM_LIB)/lib -L/usr/lib/aarch64-linux-gnu/ -lcurl -letlic -letolm -lssl -lcrypto -llogger -lpthread -lsqlite3 -ljsoncpp -lnvdsgst_meta -lnvbufsurface -lnvbufsurftransform -lnvds_meta -lnvdsgst_helper -lnvds_utils -lm -L/usr/local/cuda-$(CUDA_VER)/lib64/ -lcudart \
-lgstrtspserver-1.0 -Wl,-rpath,$(LIB_INSTALL_DIR)
CFLAGS+= `pkg-config --cflags $(PKGS)`
LIBS+= `pkg-config --libs $(PKGS)`
all: $(APP)
debug: CXXFLAGS += -DDEBUG -g
debug: CFLAGS += -DDEBUG -g
debug: $(APP)
%.o: %.c $(INCS) Makefile
$(CC) -c -o $# $(CFLAGS) $<
$(APP): $(OBJS) Makefile
$(CC) -o $(APP) $(OBJS) $(LIBS)
clean:
rm -rf $(OBJS) $(APP)
You need to set rpath to a colon-separated list of directories where your libraries are found. You only add LIB_INSTALL_DIR but not CUSTOM_LIB_DIR. Generally everything you pass to -L you need to pass to -rpath too, unless there is a specific reason not to. For example, if you are building a package that has more than a single library and you are going to install in a standard place like /usr/lib, you don't have to add the directory where libraries temporarily live to -rpath. If you are going to install to a non-standard directory, add that directory.

How to compile project using VTK library using Makefile?

For this reason I downloaded the C++ library VTK and made a local build in the build subdirectory on a OSX environment.
I would like to compile a project using this library (particularly I am using the class vtkSmartPointer) with Makefile.
Consider for example the following source code:
#include<iostream>
#include<vtkSmartPointer.h>
#include<vtkCallbackCommand.h>
int main()
{
vtkSmartPointer<vtkCallbackCommand> keypressCallback =
vtkSmartPointer<vtkCallbackCommand>::New();
std::cout<<"hello world\n";
return 0;
}
For the Makefile I started from the second answer in this post to which I aded VTK library path:
CXX = g++
# OpenCV trunk
CXXFLAGS = -std=c++11 \
-I ../VTK/Common/Core/ -I ../VTK/build/Common/Core/ -I ../VTK/build/Utilities/KWIML/ \
-I ../VTK/Utilities/KWIML/ \
-L../VTK/build/lib \
-lvtkCommon -lvtkFiltering -lvtkImaging -lvtkGraphics -lvtkGenericFiltering -lvtkIO
SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp,%.o,$(SOURCES))
DEPENDS := $(patsubst %.cpp,%.d,$(SOURCES))
# ADD MORE WARNINGS!
WARNING := -Wall -Wextra
# .PHONY means these rules get executed even if
# files of those names exist.
.PHONY: all clean
# The first rule is the default, ie. "make",
# "make all" and "make parking" mean the same
all: parking
clean:
$(RM) $(OBJECTS) $(DEPENDS) parking
# Linking the executable from the object files
parking: $(OBJECTS)
$(CXX) $(WARNING) $(CXXFLAGS) $^ -o $#
-include $(DEPENDS)
%.o: %.cpp Makefile
$(CXX) $(WARNING) $(CXXFLAGS) -MMD -MP -c $< -o $#
My environment variable DYLD_LIBRARY_PATH has the value ../cmake_bin_dir/instDir/lib:../VTK/build/lib/.
When I try to compile running make, I get the following error:
ld: library not found for -lvtkCommon
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What part of the Makefile or program or step in the process is not correct?
Thank you in advance.
The current VTK library does not contain libVtkCommon.so (see package contents section https://www.archlinux.org/packages/community/x86_64/vtk/). Are you looking for libVtkCommonCore.so? If that is the case you have to change -lvtkCommon to -lvtkCommonCore in your Makefile. The same seems to be the case for some of the other included libraries.

cannot find -lgtest when setting up Google Test

I'm using Google Test for C++ and trying to set it up on my linux machine.
My make file has the following code:
CC=g++
CFLAGS=-I $(GOOGLETESTDIR)/include -L $(GOOGLETESTDIR)/lib -lgtest -lpthread -Wall
DEPS=fib.h
OBJS=fib.o main.o
all: | r6
clean:
-rm -f r6 $(OBJS)
%.o: %.cpp $(DEPS)
$(CC) -c -o $# $< $(CFLAGS)
r6: $(OBJS)
$(CC) -o $# $^ $(CFLAGS)
.PHONY: all clean
I get the error when I run make:
/usr/bin/ld: cannot find -lgtest
How do I fix this? I'm new to this kind of testing and rather new to linux so I'm really lost.
I had this issue on Ubuntu 17.10 and basically what Alexander says is true.
Someone wrote a nice tutorial with explicit commands that can be found at https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/
It boils down to:
sudo apt install libgtest-dev cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
sudo cp *.a /usr/lib
Personally, I would appreciate a solution that did not manually move files into /usr/lib, but on the plus side this works as-is.
As of now, Google test framework is not shipped with prebuilt binaries; you need to build them yourself. See full details on how to do that in README (for Debian, the path is /usr/src/googletest/googletest/README.md).