gpt4 book ai didi

c++ - 连接 C++ 和 Postgresql

转载 作者:行者123 更新时间:2023-11-28 06:13:09 26 4
gpt4 key购买 nike

我正在尝试将 C++ 连接到 Postgres。

我最近用 Homebrew 安装了 libpqxx,如下所示:

brew install libpqxx

然后我用:在 DatabaseTest.cpp 文件上制作 DatabaseTest

我运行了以下简单程序并遇到了一个奇怪的错误,我不知道该如何处理...

#include <iostream>
#include <pqxx/pqxx>

int main(int, char *argv[])
{
pqxx::connection c("dbname=company user=accounting");
}

我运行 make,它告诉我链接器失败,这对我来说意义不大……知道这里可能发生了什么吗?抱歉,如果这很简单,自从我完成 C++ 以来已经有一段时间了!

  "pqxx::connection_base::init()", referenced from:
pqxx::basic_connection<pqxx::connect_direct>::basic_connection(char const*) in DatabaseTest-bdc916.o
"pqxx::connection_base::close()", referenced from:
pqxx::basic_connection<pqxx::connect_direct>::~basic_connection() in DatabaseTest-bdc916.o
"pqxx::connection_base::connection_base(pqxx::connectionpolicy&)", referenced from:
pqxx::basic_connection<pqxx::connect_direct>::basic_connection(char const*) in DatabaseTest-bdc916.o
"pqxx::connectionpolicy::connectionpolicy(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
pqxx::connect_direct::connect_direct(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in DatabaseTest-bdc916.o
"pqxx::connectionpolicy::~connectionpolicy()", referenced from:
pqxx::connect_direct::~connect_direct() in DatabaseTest-bdc916.o
"vtable for pqxx::connect_direct", referenced from:
pqxx::connect_direct::connect_direct(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in DatabaseTest-bdc916.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [DatabaseTest] Error 1

最佳答案

您需要一个 makefile 并且需要包含 pqxx 的链接器标志。在我的 linux 机器上,链接器标志是 -lpqxx。请参阅下面的示例 makefile。

CXXFLAGS := 
LDFLAGS := -lpqxx

# Executable output command
$(EXE): $(OBJECTS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^

# build rule for c++ files
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) -o $@ $<

当您只是从命令行调用 make 时,它​​只是使用可用于该文件的标准命令构建文件。这意味着它没有链接,也没有对其运行优化。

关于c++ - 连接 C++ 和 Postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30856577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com