thrift cpp sample code compile error

為{幸葍}努か 提交于 2019-12-04 06:06:28

The -lthrift option should be at the end of the command line, after the cpp files.

I've compiled successfully the sample on Ubuntu 11.10/gcc 4.6.1/boost 1.49 with the following makefile:

THRIFT_VER =thrift-0.8.0
USR_DIR    =${HOME}/usr
THRIFT_DIR =${USR_DIR}/${THRIFT_VER}
INCS_DIRS  =-I${USR_DIR}/include -I${THRIFT_DIR}/include/thrift
LIBS_DIRS  =-L${USR_DIR}/lib -L${USR_DIR}/${THRIFT_VER}/lib
CPP_DEFS   =-D=HAVE_CONFIG_H
CPP_OPTS   =-Wall -O2
LIBS       =-lthrift

GEN_SRC    = ../gen-cpp/SharedService.cpp  \
             ../gen-cpp/shared_types.cpp   \
             ../gen-cpp/tutorial_types.cpp \
             ../gen-cpp/Calculator.cpp
GEN_INC    = -I../gen-cpp

default: server client

server: CppServer.cpp
    g++ ${CPP_OPTS} ${CPP_DEFS} -o CppServer ${GEN_INC} ${INCS_DIRS} CppServer.cpp ${GEN_SRC} ${LIBS_DIRS} ${LIBS}

client: CppClient.cpp
    g++ ${CPP_OPTS} ${CPP_DEFS} -o CppClient ${GEN_INC} ${INCS_DIRS} CppClient.cpp ${GEN_SRC} ${LIBS_DIRS} ${LIBS}

clean:
    $(RM) -r CppClient CppServer

Take care to put tabs at the beginning of the make commands.

My layout is:

boost 1.49 installed in ${HOME}/usr
boost headers in ${HOME}/usr/include
boost libs in ${HOME}/usr/lib

thrift 0.8.0 installed in ${HOME}/usr/thrift-0.8.0
thrift headers in ${HOME}/usr/thrift-0.8.0/include
thrift libs in ${HOME}/usr/thrift-0.8.0/lib

To run the samples:

#!/bin/bash
THRIFT_VER=thrift-0.8.0
USR_DIR=${HOME}/usr
THRIFT_DIR=${USR_DIR}/${THRIFT_VER}
export LD_LIBRARY_PATH=${THRIFT_DIR}/lib:${LD_LIBRARY_PATH}
exec $1

after some search I might find the reason...It's because your g++'s version... in short, the working gcc are:

g++ 4.4.6: OK g++ 4.5.3: OK g++ 4.6.1: complains about undefined reference to

links: http://mail-archives.apache.org/mod_mbox/thrift-user/201107.mbox/%3C4E321FD6.4090503@verizon.net%3E

http://mail-archives.apache.org/mod_mbox/thrift-dev/201107.mbox/%3C4E3161D1.3090509@ens-lyon.fr%3E

Here is an example for Os X.

First install the dependencies and thrift itself, easiest to do with brew:

brew install boost
brew install thrift

Note that if you've already tried to install thrift manually you may have to uninstall it first.

Download thrift and compile the tutorial:

cd ./thrift-0.8.0/tutorial
thrift --gen cpp ./tutorial.thrift
cd ./cpp

Replace the existing Makefile with following (based on jedf solution):

THRIFT_VER =thrift/0.8.0
USR_DIR    =/usr/local/Cellar
THRIFT_DIR =${USR_DIR}/${THRIFT_VER}
INCS_DIRS  =-I${USR_DIR}/include -I${THRIFT_DIR}/include/thrift -I${USR_DIR}/boost/1.49.0/include/boost
LIBS_DIRS  =-L${USR_DIR}/${THRIFT_VER}/lib
CPP_DEFS   =-D=HAVE_CONFIG_H
CPP_OPTS   =-Wall -O2
LIBS       =-lthrift

GEN_SRC    = ../gen-cpp/SharedService.cpp  \
             ../gen-cpp/shared_types.cpp   \
             ../gen-cpp/tutorial_types.cpp \
             ../gen-cpp/Calculator.cpp
GEN_INC    = -I../gen-cpp

default: server client

server: CppServer.cpp
        g++ ${CPP_OPTS} ${CPP_DEFS} -o CppServer ${GEN_INC} ${INCS_DIRS} CppServer.cpp ${GEN_SRC} ${LIBS_DIRS} ${LIBS}

client: CppClient.cpp
        g++ ${CPP_OPTS} ${CPP_DEFS} -o CppClient ${GEN_INC} ${INCS_DIRS} CppClient.cpp ${GEN_SRC} ${LIBS_DIRS} ${LIBS}

clean:
        $(RM) -r CppClient CppServer

Compile:

make

Run the server and the client:

./CppServer
./CppClient

In addition, whenever you need to compile third party code that requires thrift use the following:

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