使用Google Protocol Buffer编译Qt项目的问题

时间:2015-11-01 13:34:56

标签: c++ qt makefile protocol-buffers qmake

我正在使用Qt和Protocol Buffer构建网络应用程序。我有一个可以成功编译的服务器和一个Qt客户端,它不能用Protocol Buffer编译。

通过在以下Makefile上运行make来构建服务器

PWD := $(shell pwd)
CXX := g++

LINKER := g++
INCDIRS := -I. -I$(PWD)/lib -I$(PWD)/lib/protobuf-2.6.1/src 
LIBDIRS := $(PWD)/lib/protobuf-2.6.1/src/.libs 
LIBS := -l protobuf 
CXXFLAGS := -std=c++11 -pthread -g #-Wall -Wextra 
BUILD_DIR=build
BIN := $(BUILD_DIR)/bin

PROTO := $(shell bash -c "pwd")/lib/protobuf-2.6.1/src/protoc
MODEL_DIR := model
MODEL_BUILD_DIR := build
BIN := $(BUILD_DIR)/bin
MODELS := $(shell bash -c "cd $(MODEL_DIR) && ls *.proto")
MODELS_SRCFILES := $(patsubst %.proto, %.pb.cc, $(MODELS))
MODELS_OBJFILES := $(addprefix $(MODEL_DIR)/$(MODEL_BUILD_DIR)/, $(patsubst %.cc,%.o,$(MODELS_SRCFILES)))
INCDIRS += -I$(MODEL_DIR)/$(MODEL_BUILD_DIR)

SERVER_DIR := server
SERVER_SRCFILES := $(wildcard $(SERVER_DIR)/*.cpp) 
SERVER_OBJFILES := $(addprefix $(BIN)/, $(patsubst %.cpp,%.o,$(SERVER_SRCFILES)))
SERVER_BUILD := $(BUILD_DIR)/server

CLIENT_DIR := client
QMAKE := qmake

LIB_DIR := lib
LIB_SRCFILES := $(wildcard $(LIB_DIR)/*.cpp) 
LIB_OBJFILES := $(addprefix $(BIN)/, $(patsubst %.cpp,%.o,$(LIB_SRCFILES)))

.PHONY: init_dir clean client model 

all: init_dir model $(SERVER_BUILD) 

run:
    @export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(LIBDIRS) && ./build/server

run_client:
    @export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(LIBDIRS) && ./build/client

init_dir: 
    @mkdir -p $(BIN)/$(SERVER_DIR) $(BIN)/$(MODEL_DIR) $(MODEL_DIR)/$(MODEL_BUILD_DIR) $(BIN)/$(LIB_DIR) 

model: $(addprefix, $(MODEL_DIR)/, $(MODELS))
    @cd $(MODEL_DIR) && \
    for model in $(MODELS); do echo proto compiling $$model; \
    $(PROTO) -I=. --cpp_out=./$(MODEL_BUILD_DIR) $$model; done  && \
    cd $(MODEL_BUILD_DIR) && \
    for modelcc in $(MODELS_SRCFILES); do echo compiling $$modelcc; $(CXX) $(CXXFLAGS) $(INCDIRS) -c $$modelcc ; done

$(SERVER_BUILD): $(LIB_OBJFILES) $(MODELS_OBJFILES) $(SERVER_OBJFILES) 
    $(LINKER) $^ -L $(LIBDIRS) $(LIBS) -o $@


$(BIN)/$(SERVER_DIR)/%.o: $(SERVER_DIR)/%.cpp
    $(CXX) $(CXXFLAGS) $(INCDIRS) -c $< -o $@

$(BIN)/$(LIB_DIR)/%.o: $(LIB_DIR)/%.cpp
    $(CXX) $(CXXFLAGS) $(INCDIRS) -c $< -o $@

client:
    @cd $(CLIENT_DIR) && $(QMAKE) && $(MAKE) 

clean:
    rm -rf $(BUILD_DIR) $(MODEL_DIR)/$(MODEL_BUILD_DIR)/*

客户端Qt项目使用此.pro:

QT       += core gui

CONFIG += c++11

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = client
TEMPLATE = app

SOURCES += main.cpp\
        mainwindow.cpp \
        ../model/build/main.model.pb.cc \
        ../model/build/guess.model.pb.cc \
        ../model/build/jack.model.pb.cc \
        ../model/build/system.model.pb.cc \
        changename.cpp


HEADERS  += mainwindow.h \
    ../model/build/main.model.pb.h \
    ../model/build/guess.model.pb.h \
    ../model/build/jack.model.pb.h \
    ../model/build/system.model.pb.h \

FORMS    += mainwindow.ui \

DESTDIR=../build 
OBJECTS_DIR=../build/bin/client
MOC_DIR=../build/bin/client


unix:!macx: LIBS += -L../lib/protobuf-2.6.1/src/.libs/ -lprotoc

INCLUDEPATH += ../lib/protobuf-2.6.1/src/.libs   ../lib ../model/build/
DEPENDPATH += ../lib/protobuf-2.6.1/src/.libs

unix:!macx: PRE_TARGETDEPS += ../lib/protobuf-2.6.1/src/.libs/libprotoc.a

../model/build/*.model.pb.h生成四个protoc生成的文件,即make model

我的问题是单独运行make model可以成功编译协议缓冲区实现文件。但是将它们包含为SOURCES并用qmake生成的Makefile编译它们不起作用。

错误如下:

g++ -c -m64 -pipe -O2 -std=c++0x -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I../lib/protobuf-2.6.1/src/.libs -I../lib -I../model/build -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I../build/bin/client -I. -o ../build/bin/client/main.o main.cpp
In file included from mainwindow.h:6:0,
                 from main.cpp:1:
../model/build/main.model.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is
 #error This file was generated by a newer version of protoc which is
  ^
../model/build/main.model.pb.h:13:2: error: #error incompatible with your Protocol Buffer headers. Please update
 #error incompatible with your Protocol Buffer headers.  Please update
  ^
../model/build/main.model.pb.h:14:2: error: #error your headers.
 #error your headers.
  ^
In file included from ../model/build/main.model.pb.h:27:0,
                 from mainwindow.h:6,
                 from main.cpp:1:
../model/build/jack.model.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is
 #error This file was generated by a newer version of protoc which is
  ^
../model/build/jack.model.pb.h:13:2: error: #error incompatible with your Protocol Buffer headers. Please update
 #error incompatible with your Protocol Buffer headers.  Please update
  ^
../model/build/jack.model.pb.h:14:2: error: #error your headers.
 #error your headers.
  ^
In file included from ../model/build/main.model.pb.h:28:0,
                 from mainwindow.h:6,
                 from main.cpp:1:
../model/build/system.model.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is
 #error This file was generated by a newer version of protoc which is
  ^
../model/build/system.model.pb.h:13:2: error: #error incompatible with your Protocol Buffer headers. Please update
 #error incompatible with your Protocol Buffer headers.  Please update
  ^
../model/build/system.model.pb.h:14:2: error: #error your headers.
 #error your headers.
  ^
In file included from ../model/build/system.model.pb.h:28:0,
                 from ../model/build/main.model.pb.h:28,
                 from mainwindow.h:6,
                 from main.cpp:1:
../model/build/guess.model.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is
 #error This file was generated by a newer version of protoc which is
  ^
../model/build/guess.model.pb.h:13:2: error: #error incompatible with your Protocol Buffer headers. Please update
 #error incompatible with your Protocol Buffer headers.  Please update
  ^
../model/build/guess.model.pb.h:14:2: error: #error your headers.
 #error your headers.
  ^
In file included from ../model/build/main.model.pb.h:27:0,
                 from mainwindow.h:6,
                 from main.cpp:1:
../model/build/jack.model.pb.h: In member function ‘void BlackJack::Request::clear_command()’:
../model/build/jack.model.pb.h:297:20: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
   if (command_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
                    ^
../model/build/jack.model.pb.h: In member function ‘void BlackJack::Request::set_command(const string&)’:
../model/build/jack.model.pb.h:308:20: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
   if (command_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
                    ^
../model/build/jack.model.pb.h: In member function ‘void BlackJack::Request::set_command(const char*)’:
../model/build/jack.model.pb.h:316:20: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
   if (command_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
                    ^
../model/build/jack.model.pb.h: In member function ‘void BlackJack::Request::set_command(const char*, size_t)’:
../model/build/jack.model.pb.h:324:20: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
   if (command_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
                    ^
../model/build/jack.model.pb.h: In member function ‘std::string* BlackJack::Request::mutable_command()’:
../model/build/jack.model.pb.h:332:20: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
   if (command_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
                    ^
../model/build/jack.model.pb.h: In member function ‘std::string* BlackJack::Request::release_command()’:
../model/build/jack.model.pb.h:340:20: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
   if (command_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
                    ^
../model/build/jack.model.pb.h:344:45: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
     command_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
                                             ^
../model/build/jack.model.pb.h: In member function ‘void BlackJack::Request::set_allocated_command(std::string*)’:
../model/build/jack.model.pb.h:349:20: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
   if (command_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
                    ^
../model/build/jack.model.pb.h:357:45: error: ‘GetEmptyStringAlreadyInited’ is not a member of ‘google::protobuf::internal’
     command_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
                                             ^

可能是什么问题?

感谢您的帮助

0 个答案:

没有答案