连接C ++和Postgresql

时间:2015-06-15 23:25:50

标签: c++ postgresql linker-errors libpqxx

我试图将C ++连接到Postgres。

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

brew install libpqxx

然后我在文件DatabaseTest.cpp上使用:make 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

1 个答案:

答案 0 :(得分: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时,它只使用该文件可用的标准命令构建文件。这意味着它没有链接,也没有对其进行优化。