0

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

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

brew install libpqxx

然后我用:make DatabaseTest在文件 DatabaseTest.cpp 上做到了。

我运行了以下简单的程序,遇到了一个奇怪的错误,我不知道该怎么办......

#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
4

1 回答 1

1

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

CXXFLAGS := 
LDFLAGS  := -lpqxx

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

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

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

于 2015-06-16T00:48:01.510 回答