我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译

My project won't compile with undefined reference error like "undefined reference to `grpc::g_core_codegen_interface'"

本文关键字:未定义 编译 错误 引用 codegen undefined 项目 reference to core 我的      更新时间:2023-10-16

我刚刚使用gRPC实现了数据库项目的分布式版本。当我尝试使用我的makefile时,我不断地使用protobuf和gRPC获得未定义的引用错误。make的部分输出如下。由于字数限制,我只收录了其中的一部分。

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中。生成文件如下。

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会解决这个问题,但它没有。

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

步骤1:

在makefile中添加以下两行:

CFLAGS += `pkg-config --cflags protobuf grpc`
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

你的Makefile应该是这样的:

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步:

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

  1. ";protobuf.pc">
  2. 路径";grpc.pc">

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

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

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

export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"

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

注意:

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

示例:

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

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

export PKG_CONFIG_PATH="/lib64/pkgconfig"
相关文章: