首页 > 解决方案 > Codelite 使用 libpqxx 连接示例和 g++ 在 macOS 上失败

问题描述

我已经尝试过makecmake来编译和部署libpqxx,但简单的代码似乎不起作用。https://github.com/jtv/libpqxx/blob/master/BUILDING-configure.md。我使用官方 Git repo 上的说明安装了该库,但在 Codelite 项目中的构建步骤仍然存在问题。

Codelite项目配置:

  1. Makefile 生成器 默认
  2. 编译器 GCC
  3. 编译器选项:-g;-O0;-Wall -lpqxx -lpq

当我列出已安装的库时,我可以看到 libpqxx 库:

lrwxr-xr-x    1 thomaspeters  staff     48 Aug 17 22:50 postgres_ext.h -> ../Cellar/postgresql/13.4/include/postgres_ext.h
lrwxr-xr-x    1 thomaspeters  staff     44 Aug 17 22:50 postgresql -> ../Cellar/postgresql/13.4/include/postgresql
drwxr-xr-x  129 root          staff   4128 Sep 12 02:05 pqxx

我安装了postgres客户端库和pqxx库。注意所有权设置为root。这是权限问题吗?

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

using namespace std;
using namespace pqxx;

int main(int argc, char* argv[]) {
   try {
      connection C("dbname = testdb user = postgres password = cohondob \
      hostaddr = 127.0.0.1 port = 5432");
      if (C.is_open()) {
         cout << "Opened database successfully: " << C.dbname() << endl;
      } else {
         cout << "Can't open database" << endl;
         return 1;
      }
      C.disconnect ();
   } catch (const std::exception &e) {
      cerr << e.what() << std::endl;
      return 1;
   }
}

我的示例代码生成以下构建流。似乎与标准版本的 c++ 编译器存在兼容性问题。

/usr/bin/make -j16 -e -f  Makefile
----------Building project:[ pgSample - Debug ]----------
clang: warning: -lpqxx: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lpq: 'linker' input unused [-Wunused-command-line-argument]
/usr/bin/g++  -c  "/Users/thomaspeters/Documents/Development/C-Simple-Projects/C-Simple-Projects/pgSample/main.cpp" -g -O0 -Wall -lpqxx -lpq  -o Debug/main.cpp.o -I. -I.
clang: warning: -lpqxx: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lpq: 'linker' input unused [-Wunused-command-line-argument]
In file included from /Users/thomaspeters/Documents/Development/C-Simple-Projects/C-Simple-Projects/pgSample/main.cpp:2:
In file included from /usr/local/include/pqxx/pqxx:2:
In file included from /usr/local/include/pqxx/array:4:
In file included from /usr/local/include/pqxx/array.hxx:17:
/usr/local/include/pqxx/internal/encoding_group.hxx:14:15: warning: nested namespace definition is a C++17 extension; define each namespace separately [-Wc++17-extensions]
namespace pqxx::internal
              ^~~~~~~~~~
               { namespace internal
/usr/local/include/pqxx/internal/encoding_group.hxx:18:6: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
enum class encoding_group
     ^
/usr/local/include/pqxx/internal/encoding_group.hxx:53:3: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
  std::size_t(char const buffer[], std::size_t buffer_len, std::size_t start);
  ^
In file included from /Users/thomaspeters/Documents/Development/C-Simple-Projects/C-Simple-Projects/pgSample/main.cpp:2:
In file included from /usr/local/include/pqxx/pqxx:2:
In file included from /usr/local/include/pqxx/array:4:
In file included from /usr/local/include/pqxx/array.hxx:18:
/usr/local/include/pqxx/internal/encodings.hxx:18:15: warning: nested namespace definition is a C++17 extension; define each namespace separately [-Wc++17-extensions]
namespace pqxx::internal
              ^~~~~~~~~~
               { namespace internal
/usr/local/include/pqxx/internal/encodings.hxx:58:3: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
  auto const scan{get_glyph_scanner(enc)};
  ^
/usr/local/include/pqxx/internal/encodings.hxx:58:14: error: declaration of variable 'scan' with deduced type 'const auto' requires an initializer
  auto const scan{get_glyph_scanner(enc)};
             ^
/usr/local/include/pqxx/internal/encodings.hxx:58:18: error: expected ';' at end of declaration
  auto const scan{get_glyph_scanner(enc)};
                 ^
                 ;
In file included from /Users/thomaspeters/Documents/Development/C-Simple-Projects/C-Simple-Projects/pgSample/main.cpp:2:
In file included from /usr/local/include/pqxx/pqxx:2:
In file included from /usr/local/include/pqxx/array:4:
/usr/local/include/pqxx/array.hxx:50:8: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
  enum class juncture
       ^
/usr/local/include/pqxx/array.hxx:82:32: warning: default member initializer for non-static data member is a C++11 extension [-Wc++11-extensions]
  std::string::size_type m_pos = 0u;
                               ^
/usr/local/include/pqxx/array.hxx:67:42: warning: use of enumeration in a nested name specifier is a C++11 extension [-Wc++11-extensions]
    internal::encoding_group = internal::encoding_group::MONOBYTE);
                                         ^
In file included from /Users/thomaspeters/Documents/Development/C-Simple-Projects/C-Simple-Projects/pgSample/main.cpp:2:
In file included from /usr/local/include/pqxx/pqxx:3:
In file included from /usr/local/include/pqxx/binarystring:4:
In file included from /usr/local/include/pqxx/binarystring.hxx:21:
In file included from /usr/local/include/pqxx/result.hxx:23:
/usr/local/include/pqxx/except.hxx:89:11: error: exception specification of overriding function is more lax than base version
  virtual ~sql_error() noexcept override;
          ^
/usr/local/include/pqxx/except.hxx:42:23: note: overridden virtual function is here
struct PQXX_LIBEXPORT failure : std::runtime_error
                      ^
/usr/local/include/pqxx/except.hxx:89:23: error: expected ';' at end of declaration list
  virtual ~sql_error() noexcept override;
                      ^
                      ;
/usr/local/include/pqxx/except.hxx:92:4: error: expected expression
  [[nodiscard]] PQXX_PURE std::string const &query() const noexcept;
   ^
/usr/local/include/pqxx/except.hxx:92:17: error: expected member name or ';' after declaration specifiers
  [[nodiscard]] PQXX_PURE std::string const &query() const noexcept;
                ^
/usr/local/include/pqxx/compiler-public.hxx:25:21: note: expanded from macro 'PQXX_PURE'
#  define PQXX_PURE __attribute__((pure))
                    ^
In file included from /Users/thomaspeters/Documents/Development/C-Simple-Projects/C-Simple-Projects/pgSample/main.cpp:2:
In file included from /usr/local/include/pqxx/pqxx:3:
In file included from /usr/local/include/pqxx/binarystring:4:
In file included from /usr/local/include/pqxx/binarystring.hxx:21:
In file included from /usr/local/include/pqxx/result.hxx:23:
/usr/local/include/pqxx/except.hxx:95:4: error: expected expression
  [[nodiscard]] PQXX_PURE std::string const &sqlstate() const noexcept;
   ^
/usr/local/include/pqxx/except.hxx:95:17: error: expected member name or ';' after declaration specifiers
  [[nodiscard]] PQXX_PURE std::string const &sqlstate() const noexcept;
                ^
/usr/local/include/pqxx/compiler-public.hxx:25:21: note: expanded from macro 'PQXX_PURE'
#  define PQXX_PURE __attribute__((pure))
                    ^
In file included from /Users/thomaspeters/Documents/Development/C-Simple-Projects/C-Simple-Projects/pgSample/main.cpp:2:
In file included from /usr/local/include/pqxx/pqxx:3:
In file included from /usr/local/include/pqxx/binarystring:4:
In file included from /usr/local/include/pqxx/binarystring.hxx:21:
In file included from /usr/local/include/pqxx/result.hxx:23:
/usr/local/include/pqxx/except.hxx:201:71: error: expected member name or ';' after declaration specifiers
  explicit unexpected_rows(std::string const &msg) : range_error{msg} {}
                                                                      ^
/usr/local/include/pqxx/except.hxx:201:65: error: expected '('
  explicit unexpected_rows(std::string const &msg) : range_error{msg} {}
                                                                ^
/usr/local/include/pqxx/except.hxx:201:69: error: expected ';' after expression
  explicit unexpected_rows(std::string const &msg) : range_error{msg} {}
                                                                    ^
                                                                    ;
/usr/local/include/pqxx/except.hxx:201:66: warning: expression result unused [-Wunused-value]
  explicit unexpected_rows(std::string const &msg) : range_error{msg} {}
                                                                 ^~~
/usr/local/include/pqxx/except.hxx:212:3: error: expected member name or ';' after declaration specifiers
  {}
  ^
/usr/local/include/pqxx/except.hxx:211:20: error: expected '('
          sql_error{err, Q, sqlstate}
                   ^
/usr/local/include/pqxx/except.hxx:211:21: warning: expression result unused [-Wunused-value]
          sql_error{err, Q, sqlstate}
                    ^~~
/usr/local/include/pqxx/except.hxx:211:26: warning: expression result unused [-Wunused-value]
          sql_error{err, Q, sqlstate}
                         ^
/usr/local/include/pqxx/except.hxx:211:37: error: expected ';' after expression
          sql_error{err, Q, sqlstate}
                                    ^
                                    ;
/usr/local/include/pqxx/except.hxx:211:29: warning: expression result unused [-Wunused-value]
          sql_error{err, Q, sqlstate}
                            ^~~~~~~~
/usr/local/include/pqxx/except.hxx:222:3: error: expected member name or ';' after declaration specifiers
  {}
  ^
/usr/local/include/pqxx/except.hxx:221:20: error: expected '('
          sql_error{err, Q, sqlstate}
                   ^
/usr/local/include/pqxx/except.hxx:221:21: warning: expression result unused [-Wunused-value]
          sql_error{err, Q, sqlstate}
                    ^~~
/usr/local/include/pqxx/except.hxx:221:26: warning: expression result unused [-Wunused-value]
          sql_error{err, Q, sqlstate}
                         ^
/usr/local/include/pqxx/except.hxx:221:37: error: expected ';' after expression
          sql_error{err, Q, sqlstate}
                                    ^
                                    ;
/usr/local/include/pqxx/except.hxx:221:29: warning: expression result unused [-Wunused-value]
          sql_error{err, Q, sqlstate}
                            ^~~~~~~~
/usr/local/include/pqxx/except.hxx:231:3: error: expected member name or ';' after declaration specifiers
  {}
  ^
/usr/local/include/pqxx/except.hxx:230:20: error: expected '('
          sql_error{err, Q, sqlstate}
                   ^
/usr/local/include/pqxx/except.hxx:230:21: warning: expression result unused [-Wunused-value]
          sql_error{err, Q, sqlstate}
                    ^~~
/usr/local/include/pqxx/except.hxx:230:26: warning: expression result unused [-Wunused-value]
          sql_error{err, Q, sqlstate}
                         ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
17 warnings and 20 errors generated.
make[1]: *** [Debug/main.cpp.o] Error 1
make: *** [All] Error 2
==== build ended with errors (20 errors, 46 warnings) ===

标签: c++codelitelibpqxx

解决方案


刚刚学习,还有很长的路要走,但发现了一些链接器选项,可以使编译器的行为有所不同。我能够删除所有编译器警告、链接器警告,现在我的代码可以编译了。

使用:-std=cxx17

在 C++ 链接器选项中,这会导致编译器使用 cxx2017 标准。呃,仍然需要一些阅读。我可以理解它,但还不是很深刻。

托马斯


推荐阅读