What change to be made in makefile to include protobuf - c++

Makefile:
CFLAGS = -c -g -W -O3 -Wall -Werror -Wshadow \
-Wno-long-long -Wpointer-arith -D_REENTRANT \
-D_POSIX_PTHREAD_SEMANTICS -DLINUX2 \
-I ./acl/lib_acl_cpp/include
BASE_PATH=./acl
LDFLAGS = -L$(BASE_PATH)/lib_acl_cpp/lib -l_acl_cpp \
-L$(BASE_PATH)/lib_protocol/lib -l_protocol \
-L$(BASE_PATH)/lib_acl/lib -l_acl \
-lpthread
redisConnection: redisConnection.o
g++ -o $# $^ $(LDFLAGS)
redisConnection.o: redisConnection.cpp
g++ $(CFLAGS) redisConnection.cpp -o redisConnection.o
And I have generated the nrtprofile.pb.cc and nrtprofile.pb.h with the help of protoc command.
What changes must be made in the makefile because I am getting the following error in the class redisConnection.cpp when I am using the functions:
undefined reference to `google::protobuf::MessageLite::ParseFromString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
I have included nrtprofile.pb.h in the redisConnection.cpp
As here there are two things redisConnection and redisConnection.o, I am getting confused where should I write nrtprofile.pb.cc.

Add -lprotobuf to the LDFLAGS variable.
However, this assumes that the protobuf library is installed in a location where the linker finds it (e.g. /usr/lib). If you have it somewhere else, you can provide the additional search path by setting the LD_LIBRARY_PATH variable like this: export LD_LIBRARY_PATH=/my/special/path.

You can find the needed linker and compiler flags here. Basically, compiler and linker-related, you need to do something like c++ my_program.cc my_proto.pb.cc `pkg-config --cflags --libs protobuf

Related

Error while compiling a project which includes CPLEX tool

I am working on the project which has to include the CPLEX tool at some point.
More in detail, I have the following classes implemented
(i.e. the corresponding files): Random.cpp, Instance.cpp, Timer.cpp. Solution.cpp which are included into Hybrid_ea.cpp which also have to include cplex library.
Finally, the project has been executed by running Algorithm.cpp (the main() function defined here).
I want to run the project on Linux platform, creating Makefile which looks like:
TARGET = Algorithm
CXXFLAGS = -ansi -O3
GENOBJS = Random.o
#CPLOBJS = Timer.o Random.o Instance.o Hybrid_ea.o
GREOBJS = Timer.o Random.o Instance.o Solution.o Hybrid_ea.o
SYSTEM = x86-64_linux
LIBFORMAT = static_pic
CPLEXDIR = /home/root/Desktop/projects/software/cplex-12.5/cplex
CONCERTDIR = /home/root/Desktop/projects/software/cplex-12.5/concert
CCC = g++
CCOPT = -m64 -O -fPIC -fexceptions -DNDEBUG -DIL_STD -std=c++11 -fpermissive -w
CPLEXBINDIR = $(CPLEXDIR)/bin/$(BINDIST)
CPLEXLIBDIR = $(CPLEXDIR)/lib/$(SYSTEM)/$(LIBFORMAT)
CONCERTLIBDIR = $(CONCERTDIR)/lib/$(SYSTEM)/$(LIBFORMAT)
CCLNFLAGS = -L$(CPLEXLIBDIR) -lilocplex -lcplex -L$(CONCERTLIBDIR) -lconcert -lm -pthread
CLNFLAGS = -L$(CPLEXLIBDIR) -lcplex -lm -pthread
CONCERTINCDIR = $(CONCERTDIR)/include
CPLEXINCDIR = $(CPLEXDIR)/include
CCFLAGS = $(CCOPT) -I$(CPLEXINCDIR) -I$(CONCERTINCDIR)
all: ${TARGET}
Algorithm: Algorithm.o $(GREOBJS)
$(CCC) $(CCFLAGS) Algorithm.o $(GREOBJS) -o Algorithm $(CCLNFLAGS)
Algorithm.o: Algorithm.cpp
$(CCC) -c $(CCFLAGS) Algorithm.cpp -o Algorithm.o
clean:
#rm -f *~ *.o ${TARGET} core
The linking process is somehow wrong. I checked, my CPLEX version is the right one since the others, simpler projects can be executed;
The full output given when trying to compile the project:
g++ -c -m64 -O -fPIC -fexceptions -DNDEBUG -DIL_STD -std=c++11 -fpermissive -w -I/home/root/Desktop/projects/LCAPS_software/cplex-12.5/cplex/include -I/home/root/Desktop/projects/LCAPS_software/cplex-12.5/concert/include Algorithm.cpp -o Algorithm.o
g++ -ansi -O3 -c -o Timer.o Timer.cc
g++ -ansi -O3 -c -o Random.o Random.cc
g++ -ansi -O3 -c -o Instance.o Instance.cpp
g++ -ansi -O3 -c -o Solution.o Solution.cpp
g++ -ansi -O3 -c -o hybrid_ea.o hybrid_ea.cpp
In file included from hybrid_ea.cpp:22:0:
hybrid_ea.h:39:10: fatal error: ilcplex/ilocplex.h: No such file or directory
#include <ilcplex/ilocplex.h>
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
<builtin>: recipe for target 'hybrid_ea.o' failed
make: *** [hybrid_ea.o] Error 1
Any help would be appreciated.
Only the file Algorithm.cpp is compiled with appropriate options for finding the CPLEX include files:
-I/home/root/Desktop/projects/LCAPS_software/cplex-12.5/cplex/include
-I/home/root/Desktop/projects/LCAPS_software/cplex-12.5/concert/include
As hybrid_ea.h also tries to include some CPLEX header files, the compilation of hybrid_ea.cpp should also have the options above.
If the makefile that you posted in your question is complete, then I suspect that the issue is the following: you didn't define a specific command to compile any .cc or .cpp file, except for Algorithm.cpp. Therefore, all other files are compiled using a default command g++ -ansi -O3 -c -o [file].o [file].cpp. And this default command doesn't have the include directives for the location of the CPLEX libraries.
As explained in ftp://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_10.html, these files are compiled using make's implicit rules. The implicit rule for C++ files is to use $(CXX) -c $(CPPFLAGS) $(CXXFLAGS). Notice how this rule uses CPPFLAGS and CXXFLAGS rather than the variable CCFLAGS that you defined at the end of your makefile to include the proper include directives.
So changing the end of your makefile to the following should work:
CPPFLAGS = $(CCOPT) -I$(CPLEXINCDIR) -I$(CONCERTINCDIR)
all: ${TARGET}
Algorithm: Algorithm.o $(GREOBJS)
$(CCC) $(CCFLAGS) Algorithm.o $(GREOBJS) -o Algorithm $(CCLNFLAGS)
clean:
#rm -f *~ *.o ${TARGET} core
Once you define the variable CPPFLAGS, it will be used automatically to compile any .cpp file that you have in your project.

How to include tensorflow c++ ".so" file in makefile?

I have created a .so file using the bazel command:
bazel build -c opt --copt="-fPIC" :tensorFlow.so
Following is my makefile:
CFLAGS = -c -g -W -O3 -Wall -Wshadow \
-Wno-long-long -Wpointer-arith -D_REENTRANT \
-D_POSIX_PTHREAD_SEMANTICS -DLINUX2 \
-I ./acl/lib_acl_cpp/include
BASE_PATH=./acl
LDFLAGS = -L$(BASE_PATH)/lib_acl_cpp/lib -l_acl_cpp \
-L$(BASE_PATH)/lib_protocol/lib -l_protocol \
-L$(BASE_PATH)/lib_acl/lib -l_acl \
-lpthread -lprotobuf -ljsoncpp
redisConnection: redisConnection.o
g++ ./redisConnection.o ./UnifiedMetric.pb.o ./getMapFromFeatureDistribution.o ./featureStats.o $(LDFLAGS) -o redisConnection
redisConnection.o: redisConnection.cpp
g++ $(CFLAGS) redisConnection.cpp -o redisConnection.o
I went through various links but couldn't find any way. How do I include the .so file file created in the makefile and what other things have to be added?
Add the paths to header files in INCLUDEPATH.
This might be the exact replica for your problem.

Confusion with Makefile linking a library that seems to link but I can't call

I am trying yo build a Makefile for a project that uses the soplex library (which also depends on libz and libgmp).
So I have this little Makefile:
SOPLEXPATH =../../lib/soplex-3.0.0/lib/
SOPLEXINCLUDE =../../lib/soplex-3.0.0/src/
SOPLEXDEP =../../lib/soplex-3.0.0/src/
CC = g++
CPPFLAGS = -g -std=c++0x -O3 -I $(SOPLEXINCLUDE)
#CPPFLAGS += -DNDEBUG
CPPFLAGS += -pg -ggdb
CPPFLAGS += -Wall -Werror=return-type
LIBS = -L $(SOPLEXPATH) -lz -lgmp -lsoplex
SRCS = $(wildcard ./src/core/*.cpp)
OBJS = $(addsuffix .o, $(basename $(SRCS)))
DEPS = $(addsuffix .d, $(basename $(SRCS)))
all : kea
kea : $(OBJS)
$(CC) $(CPPFLAGS) $(LIBS) -o bin/kea-core $(OBJS)
clean :
rm -f bin/kea-core $(OBJS) $(DEPS) *~
-include $(DEPS)
%.d: %.c
#$(CC) -MM -MT $(subst .d,.o,$#) -MT $# $(CPPFLAGS) $< > $#
And all seems to compile to object files (.o) correctly, but then the linker complains about not finding the function soplex::SoPlex::SoPlex() (the constructor of SoPlex):
g++ -g -std=c++0x -O3 -I ../../lib/soplex-3.0.0/src/ -pg -ggdb -Wall -Werror=return-type -c -o src/core/ecircuit.o src/core/ecircuit.cpp
g++ -g -std=c++0x -O3 -I ../../lib/soplex-3.0.0/src/ -pg -ggdb -Wall -Werror=return-type -c -o src/core/solver_soplex.o src/core/solver_soplex.cpp
g++ -g -std=c++0x -O3 -I ../../lib/soplex-3.0.0/src/ -pg -ggdb -Wall -Werror=return-type -c -o src/core/main.o src/core/main.cpp
g++ -g -std=c++0x -O3 -I ../../lib/soplex-3.0.0/src/ -pg -ggdb -Wall -Werror=return-type -L ../../lib/soplex-3.0.0/lib/ -lz -lgmp -lsoplex -o bin/kea-core ./src/core/ecircuit.o ./src/core/solver_soplex.o ./src/core/main.o
./src/core/solver_soplex.o: In function `SolvSoplex::SolvSoplex(ECircuit&, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >&, SolvSoplex::Mode)':
/home/diego/Projects/kea-landscape-tool/src/core/solver_soplex.cpp:9: undefined reference to `soplex::SoPlex::SoPlex()'
/home/diego/Projects/kea-landscape-tool/src/core/solver_soplex.cpp:9: undefined reference to `soplex::SoPlex::~SoPlex()'
collect2: error: ld returned 1 exit status
Makefile:20: recipe for target 'kea' failed
make: *** [kea] Error 1
Since all the .o files are created I tried to compile by hand doing:
g++ -g -std=c++0x -O3 -I ../../lib/soplex-3.0.0/src/ -Wall -Werror=return-type -pg -ggdb -L/home/diego/Projects/kea-landscape-tool/../../lib/soplex-3.0.0/lib/ -lsoplex -lz -lgmp -o bin/kea-core src/core/main.o src/core/ecircuit.o src/core/solver_soplex.o
And it failed with the same error.
then I tried switching the position of the -L and -l.. flags like this, and it compiled: g++ -g -std=c++0x -O3 -I ../../lib/soplex-3.0.0/src/ -Wall -Werror=return-type -pg -ggdb -o bin/kea-core src/core/main.o src/core/ecircuit.o src/core/solver_soplex.o -L/home/diego/Projects/kea-landscape-tool/../../lib/soplex-3.0.0/lib/ -lsoplex -lz -lgmp
Seeing that, I tried to change the rule in the Makefile as follows:
kea : $(OBJS)
$(CC) $(CPPFLAGS) -o bin/kea-core $(OBJS) $(LIBS)
But it just failed miserably triggering about 100 errors all inside soplex.cpp (as in, it depends on -lgmp and -lz, but it could not find them? Too long to paste here)
I am pretty confused, any idea on how to fix this?
thanks.
Try putting the $LIBS at the end of the command.
Change this:
LIBS = -L $(SOPLEXPATH) -lz -lgmp -lsoplex
Into this:
LIBS = -L $(SOPLEXPATH) -lsoplex -lgmp -lz
You always need to put B after A, if A calls functions in B. With static libraries as least.

lopencv_core not found when using opencv

I have opencv3 installed through home-brew and pkg-config can find the linkers too by
pkg-config --cflags --libs opencv
the outputs contains -lopencv_core, but when I add that in Makefile like this
CC=clang++
CFLAGS= -Wall -g -std=c++0x
LFLAGS= -I/usr/local/Cellar/opencv3/3.2.0/include -I/usr/local/Cellar/opencv3/3.2.0/bin -I/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_core
CFILES=blah.cpp
HFILES=blah.hpp
OFILES=blah.o
all: main
%.o: %.cpp $(HFILES)
$(CC) -c $(CFLAGS) $< -o $# $(LFLAGS)
main: $(OFILES) $(HFILES)
$(CC) $(CFLAGS) $(OFILES) -o main $(LFLAGS)
it says
ld: library not found for -lopencv_core
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How should I link that to gcc?
Assuming you want CFLAGS to be the options for compilation and LFLAGS to
be the options for linkage, the settings:
CFLAGS= -Wall -g -std=c++0x
LFLAGS= -I/usr/local/Cellar/opencv3/3.2.0/include -I/usr/local/Cellar/opencv3/3.2.0/bin -I/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_core
are confused.
The -I path option is meaningful for compilation and meaningless for linkage. It
tells the preprocessor to search in path for header files that you #include in
the source code. There will be header files in:
/usr/local/Cellar/opencv3/3.2.0/include
There will be no header files in:
/usr/local/Cellar/opencv3/3.2.0/bin
just executables, which are irrelevant to building your program. And there will be no header files in:
/usr/local/Cellar/opencv3/3.2.0/lib
just static and or dynamic libraries.
The library libopencv_core that linker can't find is presumably
in /usr/local/Cellar/opencv3/3.2.0/lib. The way to tell the
linker to search there for the library is:
-L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_core
So these settings make sense:
CFLAGS := -Wall -g -std=c++0x -I/usr/local/Cellar/opencv3/3.2.0/include
LFLAGS := -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_core
However, to compile and link an opencv program you would probably be better
leaving it to pkg-config to get the options right:
CFLAGS := -Wall -g -std=c++0x $(shell pkg-config --cflags opencv)
LFLAGS := $(shell pkg-config --libs opencv)
The -l option looks in the usual places for the mentioned library. Where and what it looks varies by OS, which you didn't mention. Possibly it can be fixed by adding -L /usr/local/lib or some other path to where you know the libraries are.

Cannot find -lperl doing a makefile on c++

sorry about my bad english...
Well, I'm now to linux, perl and c++, but I have to do some codes for the university and I'm getting some troubles while doing the makefile.
I have a code in perl which is running perfectly. As well, I have a code in C++ that calls perl as a subroutine. Everything is working properly, but when I do the makefile on my computer, it says:
sathlervbn Spam C # make clean; make
rm -f *.o
g++ -Wall -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fstack-protector -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/perl/5.14/CORE -c -o main.o main.cpp
g++ -L/usr/lib -Wall -Wl,-E -fstack-protector -L/usr/local/lib -L/usr/lib/perl/5.14/CORE - lperl -ldl -lm -lpthread -lc -lcrypt -o main libSpam.a main.o
/usr/bin/ld: cannot find -lperl
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
The problem is that when I run the makefile in my professor's computer, it's works...
Here is the code of makefile:
#CC= /usr/bin/g++
CPP = g++
CPPFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ccopts)
#LD= /usr/bin/g++
LD = g++
#LFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ldopts)
LFLAGS = -Wall -Wl,-E -fstack-protector -L/usr/local/lib -L/usr/lib/perl/5.14/CORE - lperl -ldl -lm -lpthread -lc -lcrypt
MAINOBJS = libSpam.a main.o
EMAILS = main
EXECS = $(EMAILS)
#Regra Implicita:
.c.o:
$(CPP) $(CPPFLAGS) -c $<
all: emails
emails: $(EMAILS)
main: $(MAINOBJS)
$(LD) -L/usr/lib $(LFLAGS) -o $# $(MAINOBJS)
clean:
rm -f *.o
Does anyone know how to solve it?
You need to install the perl library for C. If you're on a Debian based system (including Ubuntu) sudo apt-get install libperl-dev or something similar may be sufficient, depending on which version of perl you're using.
Update: ok, this is a bit strange - I've installed perl-base, and it installed /usr/lib/libperl.so.5.14 but it did not make a /usr/lib/libperl.so symlink as you'd expect. I wonder why not? If I manually create the symlink with ln -s /usr/lib/libperl.so.5.14 /usr/lib/libperl.so it links correctly.
Update the second I had perl-base installed, but not libperl-dev which gave me /usr/lib/libperl.so.5.14 but not /usr/lib/libperl.so. I suspect (don't know for sure, but strongly suspect) that the correct answer isn't to manually make the symlink, but to install libperl-dev.