I have to include google protocol buffer in pintool in order to save trace data but when I compile with this makefile makefile.rules
TOOL_LIBS += -lprotobuf -pthread -lboost_system
$(OBJDIR)Source1$(OBJ_SUFFIX): trace.pb.cc
$(CXX) $(TOOL_CXXFLAGS) $(COMP_OBJ)$# $<
$(OBJDIR)Source2$(OBJ_SUFFIX): protoio.cc
$(CXX) $(TOOL_CXXFLAGS) $(COMP_OBJ)$# $<
$(OBJDIR)Source3$(OBJ_SUFFIX): tracerProto.cpp
$(CXX) $(TOOL_CXXFLAGS) $(COMP_OBJ)$# $<
# Build the tool as a dll (shared object).
$(OBJDIR)tracerProto$(PINTOOL_SUFFIX): $(OBJDIR)Source1$(OBJ_SUFFIX) trace.pb.h $(OBJDIR)Source2$(OBJ_SUFFIX) protoio.hh $(OBJDIR)Source3$(OBJ_SUFFIX)
$(LINKER) $(TOOL_LDFLAGS_NOOPT) $(LINK_EXE)$# $(^:%.h=) $(TOOL_LPATHS) $(TOOL_LIBS)
but I got this error when run using this command pin -t obj-intel64/tracerProto2.so -- /bin/ls
E: Unable to load tracerProto2.so: dlopen failed: library "libprotobuf.so.17" not found
but protocol buffer library is installed already.
I have done using protobuf in another project which is built by cmake. It works correctly. How can I solve this problem?
Related
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.
I'm trying to get started with the mosquitto MQTT library (c++, raspberry pi). I think I have successfully installed all of the packages I need, and was able to build and execute the included "temperature_conversion" sample without errors. I do receive a bunch of warnings about deprecated functions, but I'm ignoring those as not part of the immediate problem.
Though not a complete newbie, I'm not well experienced with linux development tools and building/linking/MakeFiles. In trying to create my own stand-alone test based on the sample found here:
https://github.com/eclipse/mosquitto/tree/master/examples/temperature_conversion
Output when running 'make' (on my modified version): [edited to include change based on JDAllen's comment]
pi#raspberrypi:~/Documents/c/offgrid $ make
g++ main.o mqtt_manager.o -o offgrid -L/usr/local/lib/libmosquittopp.so.1 /usr/local/lib/libmosquitto.so.1
main.o: In function `main':
/home/pi/Documents/c/offgrid/main.cpp:8: undefined reference to `mosqpp::lib_init()'
/home/pi/Documents/c/offgrid/main.cpp:11: undefined reference to `mosqpp::mosquittopp::loop_forever(int, int)'
.
.
(Similar 'undefined reference' messages omitted for clarity)
.
.
collect2: error: ld returned 1 exit status
Makefile:11: recipe for target 'offgrid' failed
make: *** [offgrid] Error 1
My modified MakeFile: [edited to include change based on JDAllen's comment]
CFLAGS=-Wall -ggdb -I/usr/local/include
LDFLAGS=-L/usr/local/lib/libmosquittopp.so.1 /usr/local/lib/libmosquitto.so.1
.PHONY: all clean
all : offgrid
offgrid : main.o mqtt_manager.o
${CXX} $^ -o $# ${LDFLAGS}
main.o : main.cpp
${CXX} -c $^ -o $# ${CFLAGS}
mqtt_manager.o : mqtt_manager.cpp
${CXX} -c $^ -o $# ${CFLAGS}
clean :
-rm -f *.o offgrid
UNMODIFIED sample application's MakeFile:
CFLAGS=-Wall -ggdb -I../../lib -I../../lib/cpp
LDFLAGS=-L../../lib ../../lib/cpp/libmosquittopp.so.1 ../../lib/libmosquitto.so.1
.PHONY: all clean
all : mqtt_temperature_conversion
mqtt_temperature_conversion : main.o temperature_conversion.o
${CXX} $^ -o $# ${LDFLAGS}
main.o : main.cpp
${CXX} -c $^ -o $# ${CFLAGS}
temperature_conversion.o : temperature_conversion.cpp
${CXX} -c $^ -o $# ${CFLAGS}
clean :
-rm -f *.o mqtt_temperature_conversion
I have verified that /usr/local/include contains mosquittopp.h, and that /usr/local/lib contains libmosquittopp.so.1
Could someone please enlighten me on what I'm missing here? I'm pretty sure I could make it work by copying the /lib directory structure from the example, but I would like to use the ?installed? versions in /usr because that seems like the way one should go about such things.
[EDIT1: I have now verified that copying /lib from the example directory into my own and replacing the first two lines in the Makefile with paths relative to those directories does, indeed, allow my modified code to build without the aforementioned errors. I still want to be able to use the files in /usr/local/lib though - why doesn't this work? What further steps can I take to see where things are going wrong?]
If requested, I can update this with my modified code but unless I've done something stupid, it's a structurally equivalent pared down version of the original example linked above.
Well, I don't completely understand what's going on in the back-end, but I was able to figure out what works based another discussion with the same/similar problem here:
https://www.raspberrypi.org/forums/viewtopic.php?t=254740
In case the above link doesn't work, the following is an excerpt from that discussion:
If you are building some C code as a client you will need "-lmosquitto" at the end of the gcc command.
That says "use the library libmosquitto" which will resolve all those missing symbols.
Note that is a lower case "-l", the upper case "-L" indicates a library search path.
Use "-lmosquittopp" if you are building C++.
You will need to have installed the mosquitto client development library packages: libmosquitto-dev for C and/or libmosquittopp-dev for C++.
After a small amount of trial & error, I ended up with the following Makefile that appears to work just fine:
LDFLAGS=-lmosquittopp
.PHONY: all clean
all : offgrid
offgrid : main.o mqtt_manager.o
${CXX} $^ -o $# ${LDFLAGS}
main.o : main.cpp
${CXX} -c $^ -o $# ${CFLAGS}
mqtt_manager.o : mqtt_manager.cpp
${CXX} -c $^ -o $# ${CFLAGS}
clean :
-rm -f *.o offgrid
I am trying to statically link libssh to my project which is built using a mingw/mysys makefile, however no matter what I try I get a whole bunch of undefined reference errors. I've spent hours researching this but I still can't fix it. Below is my makefile and some example output. I also compiled the libssh.a file using cmake with the WITH_STATIC_LIB option set to 1. I don't understand how I can continue to get these errors even though my linker can find the libssh.a file. Did I build it incorrectly?
Example output:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: build/BMU.o: in function `ZN3ssh7SessionC1Ev':
./include/libssh/libsshpp.hpp:130: undefined reference to `_imp__ssh_new'
.
.
.
Makefile
CC := g++
TARGET := "dist/target"
BUILDDIR := build
SRCDIR := src
CFLAGS := -std=c++17 -g -mconsole
SRCEXT := cpp
SOURCES := $(wildcard $(SRCDIR)/*.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%, $(BUILDDIR)/%, $(SOURCES:.$(SRCEXT)=.o))
INCLUDE := -I./include
LIB := -L./lib -lws2_32 -lssh -lmodbus -static
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
#printf "\e[33m\e[1mBuilding...\e[0m\n";
#mkdir -p $(BUILDDIR)
#echo " $(notdir $#) from $(notdir $<)"
#$(CC) $(CFLAGS) $(INCLUDE) -c -o $# $<
$(TARGET): $(OBJECTS)
#printf "\e[35m\e[1mLinking...\e[0m\n";
#mkdir -p dist
#echo " $(notdir $(OBJECTS))"
#$(CC) $(CFLAGS) -o $# $^ $(LIB)
Building Libssh:
First I did
git clone https://git.libssh.org/projects/libssh.git libssh
then using cmake-gui built it for MySys makefile with 'WITH_STATIC_LIB" set to 'ON' and 'WITH_SHARED_LIB' to 'OFF'
Then I ran make from my terminal and it created a libssh.a file which I moved to the ./lib folder in my project.
The make file was able to find the libssh.a file but I still got these errors.
Any help is appreciated and yes I looked a tons of other stackoverflow posts before posting myself.
Other things I tried
#define LIBSSH_STATIC 1 and without it
using vcpkg to install libssh, but this generates a .lib file which has the same problems as the .a file
Edit:
Including LIBSSH_STATIC causes the output to look like:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: ./lib\libssh.a(channels.c.obj):channels.c:(.text+0x1979): undefined reference to `_imp__ssh_buffer_add_data'
The correct CMake parameter for building static libraries (including for libssh) is -DBUILD_SHARED_LIBS:BOOL=OFF
As long as you're seeing _imp__ in the undefined reference to errors your linker is looking for the symbols exported from a shared library (DLL).
To make sure the project that uses libssh looks for the static library you must define LIBSSH_STATIC, for example by adding compiler flag -DLIBSSH_STATIC to CFLAGS in your Makefile.
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.
Makefiles are quite confusing to me. I am attempting to "make" my project that I have worked on in Windows. The confusing part is actually constructing the make file from scratch. I would am trying to also link to the SDL2 library, and that is in a '.a' format.
Here is my code for the make file so far, I have tried multiple versions, and this is the latest:
CXX = gcc
OUT = Engine
SRC =Software-Rendering/src/
SDL_INCLUDE_DIR =Software-Rendering/lib/SDL2/include/SDL/
LIB_DIR =Software-Rendering/lib/SDL2/x86/linuxLib/
SDL = -l${LIB_DIR}libSDL -l${LIB_DIR}/libSDL2main
CPP_FILES =Bitmap.cpp Main.cpp Vector3.cpp Window.cpp
H_FILES =Bitmap.h ErrorReport.h Vector3.h Window.h
O_FILES = Bitmap.o ErrorReport.o Main.o Vector3.o Window.o
all: $(OUT)
$(OUT): $(O_FILES)
$(CXX) -o $# $^ ${SDL}
#Making all of the object files down
$(O_FILES): $(H_FILES)
$(CXX) -c $(CPP_FILES)
#Make sure we can easily clean up the directory
clean:
rm -f Engine ${O_FILES}
clean_obj:
rm -f ${O_FILES}
I decided to put the ".a" files in a special directoy in my project so whenever someone clones my repository on github all of the files for compiling and linking are already there.
Why isn't this working and how can I make it work?
Your library linking directive are wrong -- -l prefixes lib to the name you specify, and then searches through the libdir path set by the -L options. So what you want is something like:
SDL = -L$(LIB_DIR) -lSDL -lSDL2main
You can make it clearer/more standard by using the standard varnames for libraries:
LDFLAGS = -L$(LIB_DIR)
LDLIBS = -lSDL -lSDL2main
$(OUT): $(O_FILES)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $#
Also, get rid of the explicit command to compile source files -- the default built in rule is fine and easier to use.