首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我的项目不会使用未定义的引用错误进行编译,比如“对`grpc::g_core_codegen_interface的未定义引用”

我的项目不会使用未定义的引用错误进行编译,比如“对`grpc::g_core_codegen_interface的未定义引用”
EN

Stack Overflow用户
提问于 2020-07-13 00:06:38
回答 1查看 1.8K关注 0票数 3

我刚刚使用gRPC实现了一个数据库项目的分布式版本。当我尝试用makefile制作时,我一直使用protobuf和gRPC获取未定义的引用错误。make的部分输出如下。因为字数的限制,我只包括了其中的一部分。

代码语言:javascript
复制
g++ -o rundb benchmarks/ycsb_query.o benchmarks/tpcc_wl.o benchmarks/ycsb_wl.o benchmarks/ycsb_store_procedure.o benchmarks/tpcc_query.o benchmarks/tpcc_helper.o benchmarks/tpcc_store_procedure.o concurrency_control/lock_manager.o concurrency_control/tictoc_manager.o concurrency_control/row_f1.o concurrency_control/row_lock.o concurrency_control/f1_manager.o concurrency_control/row_tictoc.o storage/catalog.o storage/index_hash.o storage/index_btree.o storage/index_base.o storage/row.o storage/log.o storage/table.o system/stats.o system/manager.o system/cc_manager.o system/thread.o system/global.o system/logging_thread.o system/store_procedure.o system/txn.o system/worker_thread.o system/parser.o system/workload.o system/txn_table.o system/main.o system/caching.o utils/helper.o utils/semaphore_sync.o utils/packetize.o grpc/grpc_async_client.o grpc/grpc_sync_server.o grpc/grpc_async_server.o grpc/grpc_sync_client.o -Wall -L./libs -pthread -lrt -std=c++0x -O3 -ljemalloc -lprotobuf -Wall -g -std=c++11 -I. -I./benchmarks -I./concurrency_control -I./storage -I./system -I./transport -I./utils -I./grpc -I./grpc_system -D NOGRAPHITE=1 -O3 -g -ggdb
/usr/bin/ld: concurrency_control/lock_manager.o: in function `google::protobuf::internal::GenericTypeHandler<sundial_rpc::SundialRequest_TupleData>::New(google::protobuf::Arena*)':
/usr/local/include/google/protobuf/repeated_field.h:802: undefined reference to `sundial_rpc::SundialRequest_TupleData* google::protobuf::Arena::CreateMaybeMessage<sundial_rpc::SundialRequest_TupleData>(google::protobuf::Arena*)'
/usr/bin/ld: system/stats.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& sundial_rpc::SundialRequest_RequestType_Name<unsigned int>(unsigned int)':
/home/libin/Sundial/./grpc_system/sundial_grpc.pb.h:105: undefined reference to `sundial_rpc::SundialRequest_RequestType_descriptor()'
/usr/bin/ld: system/stats.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& sundial_rpc::SundialResponse_ResponseType_Name<unsigned int>(unsigned int)':
/home/libin/Sundial/./grpc_system/sundial_grpc.pb.h:136: undefined reference to `sundial_rpc::SundialResponse_ResponseType_descriptor()'
/usr/bin/ld: system/txn.o: in function `google::protobuf::internal::GenericTypeHandler<sundial_rpc::SundialResponse_TupleData>::New(google::protobuf::Arena*)':
/usr/local/include/google/protobuf/repeated_field.h:802: undefined reference to `sundial_rpc::SundialResponse_TupleData* google::protobuf::Arena::CreateMaybeMessage<sundial_rpc::SundialResponse_TupleData>(google::protobuf::Arena*)'
/usr/bin/ld: system/txn.o: in function `TxnManager::RemoteNodeInfo::~RemoteNodeInfo()':
/home/libin/Sundial/system/txn.h:102: undefined reference to `sundial_rpc::SundialResponse::~SundialResponse()'
/usr/bin/ld: /home/libin/Sundial/system/txn.h:102: undefined reference to `sundial_rpc::SundialRequest::~SundialRequest()'
/usr/bin/ld: system/txn.o: in function `sundial_rpc::SundialRequest::SundialRequest()':
/home/libin/Sundial/./grpc_system/sundial_grpc.pb.h:494: undefined reference to `sundial_rpc::SundialRequest::SundialRequest(google::protobuf::Arena*)'
/usr/bin/ld: system/txn.o: in function `sundial_rpc::SundialResponse::SundialResponse()':
/home/libin/Sundial/./grpc_system/sundial_grpc.pb.h:909: undefined reference to `sundial_rpc::SundialResponse::SundialResponse(google::protobuf::Arena*)'
/usr/bin/ld: system/txn.o: in function `TxnManager::send_remote_read_request(unsigned long, unsigned long, unsigned long, unsigned long, access_t)':
/home/libin/Sundial/system/txn.cpp:301: undefined reference to `sundial_rpc::SundialRequest::Clear()'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:302: undefined reference to `sundial_rpc::SundialResponse::Clear()'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:309: undefined reference to `sundial_rpc::SundialResponse::SundialResponse(sundial_rpc::SundialResponse const&)'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:309: undefined reference to `sundial_rpc::SundialRequest::SundialRequest(sundial_rpc::SundialRequest const&)'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:309: undefined reference to `sundial_rpc::SundialRequest::~SundialRequest()'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:309: undefined reference to `sundial_rpc::SundialResponse::~SundialResponse()'
/usr/bin/ld: system/txn.o: in function `google::protobuf::internal::GenericTypeHandler<sundial_rpc::SundialRequest_ReadRequest>::New(google::protobuf::Arena*)':
/usr/local/include/google/protobuf/repeated_field.h:802: undefined reference to `sundial_rpc::SundialRequest_ReadRequest* google::protobuf::Arena::CreateMaybeMessage<sundial_rpc::SundialRequest_ReadRequest>(google::protobuf::Arena*)'
/usr/bin/ld: system/txn.o: in function `TxnManager::process_2pc_phase1()':
/home/libin/Sundial/system/txn.cpp:351: undefined reference to `sundial_rpc::SundialRequest::Clear()'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:352: undefined reference to `sundial_rpc::SundialResponse::Clear()'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:362: undefined reference to `sundial_rpc::SundialResponse::SundialResponse(sundial_rpc::SundialResponse const&)'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:362: undefined reference to `sundial_rpc::SundialRequest::SundialRequest(sundial_rpc::SundialRequest const&)'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:362: undefined reference to `sundial_rpc::SundialRequest::~SundialRequest()'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:362: undefined reference to `sundial_rpc::SundialResponse::~SundialResponse()'
/usr/bin/ld: system/txn.o: in function `TxnManager::process_2pc_phase2(RC)':
/home/libin/Sundial/system/txn.cpp:420: undefined reference to `sundial_rpc::SundialRequest::Clear()'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:421: undefined reference to `sundial_rpc::SundialResponse::Clear()'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:429: undefined reference to `sundial_rpc::SundialResponse::SundialResponse(sundial_rpc::SundialResponse const&)'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:429: undefined reference to `sundial_rpc::SundialRequest::SundialRequest(sundial_rpc::SundialRequest const&)'
/usr/bin/ld: /home/libin/Sundial/system/txn.cpp:429: undefined reference to `sundial_rpc::SundialRequest::~SundialRequest()'
/usr/bin/ld: /home/libin/Sundial/grpc/grpc_sync_client.cpp:37: undefined reference to `sundial_rpc::Sundial_GRPC_SYNC::Stub::contactRemote(grpc_impl::ClientContext*, sundial_rpc::SundialRequest const&, sundial_rpc::SundialResponse*)'
/usr/bin/ld: /home/libin/Sundial/grpc/grpc_sync_client.cpp:36: undefined reference to `grpc_impl::ClientContext::~ClientContext()'
/usr/bin/ld: grpc/grpc_sync_client.o: in function `sundial_rpc::Sundial_GRPC_SYNC::Stub::~Stub()':
/home/libin/Sundial/./grpc_system/sundial_grpc.grpc.pb.h:73: undefined reference to `vtable for sundial_rpc::Sundial_GRPC_SYNC::Stub'
/usr/bin/ld: grpc/grpc_sync_client.o: in function `sundial_rpc::Sundial_GRPC_SYNC::Stub::~Stub()':
/home/libin/Sundial/./grpc_system/sundial_grpc.grpc.pb.h:73: undefined reference to `vtable for sundial_rpc::Sundial_GRPC_SYNC::Stub'
/usr/bin/ld: grpc/grpc_sync_client.o: in function `Sundial_Sync_Client::contactRemote(unsigned long, sundial_rpc::SundialRequest, sundial_rpc::SundialResponse)':
/home/libin/Sundial/grpc/grpc_sync_client.cpp:36: undefined reference to `grpc_impl::ClientContext::~ClientContext()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:20: rundb] Error 1

我确实读过以前的一些文章,指出它是由于没有链接protobuf库而引起的,但是我确实将它包含在LDFLAGS中。makefile在下面。

代码语言:javascript
复制
CC=g++
CFLAGS=-Wall -g -std=c++11

.SUFFIXES: .o .cpp .h

SRC_DIRS = ./ ./benchmarks/ ./concurrency_control/ ./storage/ ./system/ ./transport/ ./utils/ ./grpc/ ./grpc_system/
INCLUDE = -I. -I./benchmarks -I./concurrency_control -I./storage -I./system -I./transport -I./utils -I./grpc -I./grpc_system

CFLAGS += $(INCLUDE) -D NOGRAPHITE=1 -O3 -g -ggdb
LDFLAGS = -Wall -L./libs -pthread -lrt -std=c++0x -O3 -ljemalloc -lprotobuf
LDFLAGS += $(CFLAGS)

CPPS = $(foreach dir, $(SRC_DIRS), $(wildcard $(dir)*.cpp))
OBJS = $(CPPS:.cpp=.o)
DEPS = $(CPPS:.cpp=.d)

all:rundb

rundb : $(OBJS)
    $(CC) -o $@ $^ $(LDFLAGS)

-include $(OBJS:%.o=%.d)

%.d: %.cpp
    $(CC) -MM -MT $*.o -MF $@ $(CFLAGS) $<

%.o: %.cpp %.d
    $(CC) -c $(CFLAGS) -o $@ $<

.PHONY: clean
clean:
    rm -f rundb *.o */*.o *.d */*.d

所以我认为LDFLAGS = -Wall -L./libs -pthread -lrt -std=c++0x -O3 -ljemalloc -lprotobuf会解决这个问题,但它没有解决。

EN

回答 1

Stack Overflow用户

发布于 2021-06-22 11:27:04

您可以尝试以下两个步骤:

步骤1:

在makefile中添加以下两行:

代码语言:javascript
复制
CFLAGS += `pkg-config --cflags protobuf grpc`
代码语言:javascript
复制
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
           -lgrpc++_reflection\
           -ldl

请参考这个Makefile (负载平衡的C++ grpc示例):https://chromium.googlesource.com/external/github.com/grpc/grpc/+/HEAD/examples/cpp/load_balancing/Makefile

生成文件应该如下所示:

代码语言:javascript
复制
CC=g++
CFLAGS=-Wall -g -std=c++11

# Add the below line
CFLAGS += `pkg-config --cflags protobuf grpc`

.SUFFIXES: .o .cpp .h

SRC_DIRS = ./ ./benchmarks/ ./concurrency_control/ ./storage/ ./system/ ./transport/ ./utils/ ./grpc/ ./grpc_system/
INCLUDE = -I. -I./benchmarks -I./concurrency_control -I./storage -I./system -I./transport -I./utils -I./grpc -I./grpc_system

CFLAGS += $(INCLUDE) -D NOGRAPHITE=1 -O3 -g -ggdb
LDFLAGS = -Wall -L./libs -pthread -lrt -std=c++0x -O3 -ljemalloc -lprotobuf
LDFLAGS += $(CFLAGS)

# Add the below line
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
           -lgrpc++_reflection\
           -ldl

CPPS = $(foreach dir, $(SRC_DIRS), $(wildcard $(dir)*.cpp))
OBJS = $(CPPS:.cpp=.o)
DEPS = $(CPPS:.cpp=.d)

all:rundb

rundb : $(OBJS)
    $(CC) -o $@ $^ $(LDFLAGS)

-include $(OBJS:%.o=%.d)

%.d: %.cpp
    $(CC) -MM -MT $*.o -MF $@ $(CFLAGS) $<

%.o: %.cpp %.d
    $(CC) -c $(CFLAGS) -o $@ $<

.PHONY: clean
clean:
    rm -f rundb *.o */*.o *.d */*.d

第2步:

在命令提示符中,您需要找到两个文件的路径:

"protobuf.pc"

  • Path of "grpc.pc"

  1. 路径

您可以使用以下命令找到路径:

代码语言:javascript
复制
sudo find / -name "protobuf.pc"
Example output: /usr/local/lib/pkgconfig

sudo find / -name "grpc.pc"
Example output: /usr/lib/pkgconfig"

最后,您需要通过运行以下命令,在环境变量"PKG_CONFIG_PATH“中添加这两条路径,用冒号(:)分隔:

代码语言:javascript
复制
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"

现在,您可以运行命令并继续前进。

注意:

在步骤2中,如果protobuf.pc和grpc.pc同时存在于同一个目录中,那么您只需要在环境变量中指定该目录。

示例:

代码语言:javascript
复制
sudo find / -name "protobuf.pc"
Example output: /lib64/pkgconfig

sudo find / -name "grpc.pc"
Example output: /lib64/pkgconfig

在这种情况下,可以将环境变量设置为:

代码语言:javascript
复制
export PKG_CONFIG_PATH="/lib64/pkgconfig"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62867643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档