0

I am trying to compile a simple piece of code which selects multiple rows from database using libpqxx library on centos 6.3.

#include <pqxx/pqxx>

using namespace std;
using namespace pqxx;

int main(int argc, char* argv[])
{
    char * sql;

    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;
        }
        /* Create SQL statement */
        sql = "SELECT * from COMPANY";

        /* Create a non-transactional object. */
        nontransaction N(C);

        /* Execute SQL query */
        result R( N.exec( sql ));

        /* List down all the records */
        for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
            cout << "ID = " << c[0].as<int>() << endl;
            cout << "Name = " << c[1].as<string>() << endl;
            cout << "Age = " << c[2].as<int>() << endl;
            cout << "Address = " << c[3].as<string>() << endl;
            cout << "Salary = " << c[4].as<float>() << endl;
        }
        cout << "Operation done successfully" << endl;
        C.disconnect ();
    }catch (const std::exception &e){
        cerr << e.what() << std::endl;
        return 1;
    }

    return 0;
}

I get the following linker error:

ASP:/root/test/libpqxx_folder$ g++ -g libpqxx_select_from_table.cpp -L/usr/lib64 -lpqxx -lpq -o select libpqxx_select_from_table.cpp: In function 'int main(int, char**)': libpqxx_select_from_table.cpp:21: warning: deprecated conversion from string constant to 'char*' /tmp/cc0bFRAY.o: In function main': /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:30: undefined reference topqxx::result::begin() const' /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:31: undefined reference to pqxx::tuple::operator[](int) const' /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:32: undefined reference topqxx::tuple::operator const' /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:33: undefined reference to pqxx::tuple::operator[](int) const' /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:34: undefined reference topqxx::tuple::operator const' /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:35: undefined reference to pqxx::tuple::operator[](int) const' /tmp/cc0bFRAY.o: In functionbool pqxx::field::to, std::allocator > >(std::basic_string, std::allocator >&) const': /usr/local/include/pqxx/field.hxx:190: undefined reference to pqxx::field::c_str() const' /usr/local/include/pqxx/field.hxx:191: undefined reference topqxx::field::is_null() const' /usr/local/include/pqxx/field.hxx:192: undefined reference to pqxx::field::size() const' /tmp/cc0bFRAY.o: In functionconst_result_iterator': /usr/local/include/pqxx/result.hxx:334: undefined reference to pqxx::tuple::tuple(pqxx::result const*, unsigned long)' /tmp/cc0bFRAY.o: In functionbool pqxx::field::to(int&) const': /usr/local/include/pqxx/field.hxx:119: undefined reference to pqxx::field::c_str() const' /usr/local/include/pqxx/field.hxx:120: undefined reference topqxx::field::is_null() const' /tmp/cc0bFRAY.o: In function bool pqxx::field::to<float>(float&) const': /usr/local/include/pqxx/field.hxx:119: undefined reference topqxx::field::c_str() const' /usr/local/include/pqxx/field.hxx:120: undefined reference to `pqxx::field::is_null() const' collect2: ld returned 1 exit status

Thanks in advance for your help!

jam
  • 3,640
  • 5
  • 34
  • 50
  • have you linked against the respective library? – anderas Aug 13 '14 at 15:15
  • Just build ok. LC_ALL=C make g++ -std=c++11 -lpqxx test.cpp -o test test.cpp: In function 'int main(int, char**)': test.cpp:20:13: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] sql = "SELECT * from COMPANY"; ^ – Tanuki Aug 13 '14 at 15:42
  • what is the library version you have? – Tanuki Aug 13 '14 at 15:43
  • thanks @andreas and Tanuki for responding -rwxr-xr-x. 1 root root 942 Oct 22 2012 libpqxx.la -rwxr-xr-x. 1 root root 364576 Oct 22 2012 libpqxx-3.1.so -rwxr-xr-x. 1 root root 365560 Oct 28 2012 libpqxx-4.0.so -rwxr-xr-x. 1 root root 166000 Feb 25 22:43 libpq.so.5.2 lrwxrwxrwx. 1 root root 12 Aug 11 23:12 libpq.so.5 -> libpq.so.5.2 lrwxrwxrwx. 1 root root 12 Aug 11 23:16 libpq.so -> libpq.so.5.2 lrwxrwxrwx. 1 root root 14 Aug 13 09:39 libpqxx.so -> libpqxx-4.0.so – Rohit Jerath Aug 13 '14 at 17:57
  • Problems like this typically occur due to libraries mismatch. On upgrading my libpqxx and libpq libraries the issue was resolved. – Rohit Jerath Aug 17 '14 at 05:45

0 Answers0