1

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.

NITIKA KHATKAR
  • 149
  • 2
  • 4
  • 15
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Alan Birtles Aug 10 '18 at 12:42
  • I added -lprotobuf to the LDFLAGS. Still it does not recognize the classes in nrtproto.pb.cc. Where should I include in the makefile? – NITIKA KHATKAR Aug 10 '18 at 12:55
  • Every `.cc` or `.cpp` file should be compiled separately into a `.o` file and linked together into your `redisConnection` binary at the end. – Botje Aug 10 '18 at 13:30
  • It worked. Thanks a lot!!!!! – NITIKA KHATKAR Aug 12 '18 at 04:51

2 Answers2

0

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.

Georg P.
  • 2,785
  • 2
  • 27
  • 53
0

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

schaiba
  • 362
  • 3
  • 11