Can't compile example from google protocol buf

2019-01-25 09:22发布

问题:

I grep for other topics, but they dont help me =(. On my working server, i have no sudo privilegies, so i install PB with

./configure --prefix=/home/username/local

Then i create source files with "person" example and succesfully compile it with protoc.

I have no pkg-info =(. I try to compile it with

g++ -I /home/username/local/include -L /home/username/local/lib -lprotobuf -lpthread main.cpp person.pb.cc

and then have a billion simular errors i.e.

person.pb.cc:(.text+0x4cf): undefined reference to `google::protobuf::internal::kEmptyString'

I think, that it is a problem with linking, but how to solve it?

echo $LD_LIBRARY_PATH /home/username/local/lib

in main.cpp:

#include "person.pb.h"
...

Thanks.

回答1:

Put the library at the end:

g++ -I /home/username/local/include -L /home/username/local/lib main.cpp person.pb.cc -lprotobuf -pthread

From GCC Link Options:

-llibrary
-l library
    Search the library named library when linking. 
    (The second alternative with the library as a separate argument
    is only for POSIX compliance and is not recommended.)

    It makes a difference where in the command you write this option;
    the linker searches and processes libraries and object files in the
    order they are specified.
    Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but
    before bar.o. If bar.o refers to functions in `z', those functions
    may not be loaded.

Also, use -pthread instead of -lpthread as -pthread may set flags for preprocessor and linker.



回答2:

Library linking flags go at the end of the compiler's arguments:

g++ -I /home/username/local/include -L /home/username/local/lib main.cpp person.pb.cc -lprotobuf -lpthread