2017-02-07 6 views
1

libpqxxのコード例をコンパイル中にエラーが発生しましたか?私はlibpqxxコード例をコンパイルしようとしています

コマンドを使用して
#include <iostream> 
#include <pqxx/pqxx> 

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

    pqxx::result r = txn.exec(
    "SELECT id " 
    "FROM Employee " 
    "WHERE name =" + txn.quote(argv[1])); 

    if (r.size() != 1) 
    { 
    std::cerr 
     << "Expected 1 employee with name " << argv[1] << ", " 
     << "but found " << r.size() << std::endl; 
    return 1; 
    } 

    int employee_id = r[0][0].as<int>(); 
    std::cout << "Updating employee #" << employee_id << std::endl; 

    txn.exec(
    "UPDATE EMPLOYEE " 
    "SET salary = salary + 1 " 
    "WHERE id = " + txn.quote(employee_id)); 

    txn.commit(); 
} 

c++ add_employee.cxx -lpqxx -lpq 

は、残念ながら、私はエラーを持っている:

test2.cpp:(.text+0x162): undefined reference to `pqxx::transaction_base::exec(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
test2.cpp:(.text+0x32e): undefined reference to `pqxx::transaction_base::exec(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
/tmp/cchvWstf.o: In function `pqxx::string_traits<int>::null()': 
test2.cpp:(.text._ZN4pqxx13string_traitsIiE4nullEv[_ZN4pqxx13string_traitsIiE4nullEv]+0x47): undefined reference to `pqxx::internal::throw_null_conversion(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
/tmp/cchvWstf.o: In function `pqxx::connect_direct::connect_direct(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': 
test2.cpp:(.text._ZN4pqxx14connect_directC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN4pqxx14connect_directC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x1f): undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
/tmp/cchvWstf.o: In function `pqxx::transaction<(pqxx::isolation_level)0, (pqxx::readwrite_policy)1>::transaction(pqxx::connection_base&)': 
test2.cpp:(.text._ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE[_ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE]+0xba): undefined reference to `pqxx::dbtransaction::fullname(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
test2.cpp:(.text._ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE[_ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE]+0x190): undefined reference to `pqxx::basic_transaction::basic_transaction(pqxx::connection_base&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, pqxx::readwrite_policy)' 

私はそれをコンパイルできますか?私はフラグの順序を変更しようとしているが、それは助けにはならない。ありがとうございました。

答えて

0

まず、libpxx.soが/ usr/libに正しくインストールされているかどうかを確認します。その後、g ++ -L/usr/lib/add_employee.cpp -o add_employee -lpqxx -lpqを使用してみます。これにより、add_employee(部分-o add_employee)という実行可能ファイルが生成され、生成されます。

0

私は最近、Ubuntu 16.04でlibpqxx-3.1を使ってコードをコンパイルしようとしたときにこの問題が発生しました。同じコードがUbuntu 14.04で正常にコンパイルされましたが、Ubuntu 16.04のリンカエラー(あなたのようなもの)が吐き出されました。この犯人は、gcc 4.8.4とgcc 5.4.0との間でlibstdC++が変更されたように見えます。 https://gcc.gnu.org/gcc-5/changes.html

A Dual ABI is provided by the library. A new ABI is enabled by default. The old ABI is still supported and can be used by defining the macro _GLIBCXX_USE_CXX11_ABI to 0 before including any C++ standard library headers.

で「ランタイムライブラリ」のドキュメントごとに、これはあなたが直面している問題であるかどうかを確認し、あなたのcppファイルの先頭に次のマクロを追加してコンパイルします。あなたはリンク先のライブラリを使用しているが

#define _GLIBCXX_USE_CXX11_ABI 0

それが正常にリンクする場合は

は、あなたは、あなたのコードがのstd ::文字列(デフォルトで行いますgcc5など)の新しい実装でコンパイルしていることが確認されました古い実装(gcc4が行うように)。導入されたマクロでは、コードとlibpqxxの両方が同じ実装を使用しています。

長期的な解決策は、使用しているgccと同じバージョンのライブラリを使用することです。 Ubuntu 16.04が古いgccによって構築されたライブラリをなぜ提供しているのかは分かりませんが、確かに私に頭痛を引き起こしています。上記の解決方法に固執するか、独自のlibpqxxビルドをコンパイルしてインストールするか、Linuxディストリビューションのそれ以降のバージョンにアップグレードすることができます(うまくいけば、新しいバージョンのライブラリを出荷することができます)。

関連する問題