Can't compile example from google protocol buffers

為{幸葍}努か 提交于 2019-11-27 22:18:55

问题


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



来源:https://stackoverflow.com/questions/10404027/cant-compile-example-from-google-protocol-buffers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!