连接 C++ 和 Postgresql
Connecting C++ and Postgresql
我正在尝试将 C++ 连接到 Postgres。
我最近用 Homebrew 安装了 libpqxx,如下所示:
brew install libpqxx
然后我用: make DatabaseTest
在文件 DatabaseTest.cpp.
我 运行 下面的简单程序遇到了一个 st运行ge 错误,我不知道该如何处理...
#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
。
请参阅下面的示例生成文件。
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++ 连接到 Postgres。
我最近用 Homebrew 安装了 libpqxx,如下所示:
brew install libpqxx
然后我用: make DatabaseTest
在文件 DatabaseTest.cpp.
我 运行 下面的简单程序遇到了一个 st运行ge 错误,我不知道该如何处理...
#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
。
请参阅下面的示例生成文件。
CXXFLAGS :=
LDFLAGS := -lpqxx
# Executable output command
$(EXE): $(OBJECTS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^
# build rule for c++ files
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) -o $@ $<
当您从命令行调用 make 时,它只是使用可用于该文件的标准命令构建文件。这意味着它没有链接,也没有对其进行 运行 优化。