Problem z instalacją SOCI (SQL wrapper dla C++) na Xubuntu

0

Zainstalowałem SOCI:

http://soci.sourceforge.net/doc/installation.html#unix za pomocą komend:

$ mkdir build
$ cd build
$ cmake cmake -G "Unix Makefiles" -DWITH_BOOST=ON -DWITH_POSTGRESQL=ON ../
$ make
$ make install

Mam prościutki program:

#include "soci-postgresql.h"

int main(int argc, char **argv){

  soci::session sql(postgresql, "testDB");
  return 0;
}

Kompiluję na różne sposoby:

  1. g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq
  2. g++ test.cpp -L/usr/local/lib -lsoci_core -lsoci_postgresql -ldl -lpq
  3. g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci

to daje mi błędy:

test.cpp:1:29: fatal error: soci-postgresql.h: No such file or directory compilation terminated

Kompiluję więc tak:

g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci/postgresql

i dostaję błąd:

g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci/postgresql In file included from test.cpp:1:0: /usr/local/include/soci/postgresql/soci-postgresql.h:27:26: fatal error: soci-backend.h: No such file or directory compilation terminated.

Co robić? Muszę napisać programa używającego bazy postgreSQL w C++, a to SOCI jest tu świetnym rozwiązaniem; "jedynie" kiszka z kompilacją:/

System to Xubuntu 11.04

0

Spróbuj zamiast "g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci/postgresql" użyć "g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci" i dołączyć wtedy w pliku cpp: #include <postgresql/soci-postgresql.h".

0
#include "postgresql/soci-postgresql.h"
#include "soci/soci.h"

int main(int argc, char **argv){
  
  soci::session sql(postgresql, "testDB");
  return 0;
}

kompilując:

g++ test2.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci

daje :

In file included from test2.cpp:1:0:
/usr/local/include/soci/postgresql/soci-postgresql.h:28:22: fatal error: libpq-fe.h: No such file or directory
compilation terminated.

1

Hmm, no tak... No więc po kolei:

  1. SOCI instaluje się w niestandardowym katalogu "/usr/local" zamiast "/usr". Nie jest to źle, ale trzeba robić jeszcze parę rzeczy uporządkowujących,
  2. Na początek (gdy SOCI jest zainstalowany w "/usr/local") przetestujemy prawidłowość instalacji np. za pomocą utworzonego pliku socitest.cpp, którego zawartość to:
#include <iostream>
#include <soci.h>
#include <postgresql/soci-postgresql.h>
 
int main(int argc, char **argv)
{
   try
   { 
      soci::session sql(soci::postgresql, "dbname=testDB");
   }
   catch (soci::postgresql_soci_error const & e)
   {
      std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
   }
   catch (std::exception const & e)
   {
      std::cerr << "Some other error: " << e.what() << std::endl;
   }
   return 0;
}

#Kompilujemy ten plik za pomocą: ** g++ socitest.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci -I /usr/include/postgresql -o socitest **,
#Kod powinien się skompilować, ale na moim Ubuntu nie uruchomi się ze względu na niestandardowe ścieżki do bibliotek SOCI. Żeby to poprawić to:
#Jako root tworzymy plik soci.conf
touch /etc/ld.so.conf.d/soci.conf
i edytujemy np. za pomocą gedita (jako root):
gedit /etc/ld.so.conf.d/soci.conf
dodając zawartość:
/usr/local/lib64
U mnie jest 64-bitowy Ubuntu i w tym katalogu są biblioteki SOCI. Stąd taka zawartość tego soci.conf. Potem jako root piszemy w konsoli ldconfig i od tego momentu dodatkowe biblioteki (SOCI) powinny być widoczne w systemie. A program powinien odpalić się normalnie (pominąwszy ewentualne błędy związane z brakiem bazy testDB na komputerze).

0

@norma: widzę, że się orientujesz - dzięki:) na razie nie mam dostępu do Xubuntu, ale może problem jest w tym, że źle go zainstalowałem?

Robiłem to tak:

  1. zainstalowałem:

a) libboost-dev b) libpq-dev c) libtool d) SOCI, wg tego:

$ mkdir build
$ cd build
$ cmake cmake -G "Unix Makefiles" -DWITH_BOOST=ON -DWITH_POSTGRESQL=ON ../
$ make
$ make install

i tu dodatkowe info:

/usr/local/include/soci$ ls

backend-loader.h postgresql soci-platform.h
blob-exchange.h prepare-temp-type.h soci-simple.h
blob.h procedure.h statement.h
boost-fusion.h ref-counted-prepare-info.h transaction.h
boost-gregorian-date.h ref-counted-statement.h type-conversion.h
boost-optional.h row-exchange.h type-conversion-traits.h
boost-tuple.h row.h type-holder.h
connection-pool.h rowid-exchange.h type-ptr.h
empty rowid.h unsigned-types.h
error.h rowset.h use.h
exchange-traits.h session.h use-type.h
into.h soci-backend.h values-exchange.h
into-type.h soci-config.h values.h
once-temp-type.h soci.h version.h

/usr/local/include/soci/postgresql$ ls
common.h soci-postgresql.h

/usr/local/lib$ ls
libCOS4.a libomniORB4.so.1
libCOS4.so libomniORB4.so.1.6
libCOS4.so.1 libomnithread.a
libCOS4.so.1.6 libomnithread.so
libCOSDynamic4.a libomnithread.so.3
libCOSDynamic4.so libomnithread.so.3.4
libCOSDynamic4.so.1 libsoci_core.a
libCOSDynamic4.so.1.6 libsoci_core.so
libomniCodeSets4.a libsoci_core.so.3.1
libomniCodeSets4.so libsoci_core.so.3.1.0
libomniCodeSets4.so.1 libsoci_empty.a
libomniCodeSets4.so.1.6 libsoci_empty.so
libomniConnectionMgmt4.a libsoci_empty.so.3.1
libomniConnectionMgmt4.so libsoci_empty.so.3.1.0
libomniConnectionMgmt4.so.1 libsoci_postgresql.a
libomniConnectionMgmt4.so.1.6 libsoci_postgresql.so
libomniDynamic4.a libsoci_postgresql.so.3.1
libomniDynamic4.so libsoci_postgresql.so.3.1.0
libomniDynamic4.so.1 pkgconfig
libomniDynamic4.so.1.6 python2.7
libomniORB4.a python3.2
libomniORB4.so

0

Yay, @norma: jesteś super, udało się:) Teraz już wszystko działa!:D Niesamowite, serdeczne dzięki:)

0

@norma: mam nadzieję, że jeszcze tu zajrzysz;) i może pomożesz mi z jeszcze jedną rzeczą - program mi się kompiluje, ale nie działa:/ zainstalowałem PostgreSQL i mam bazę testDB:

  1. createdb testDB
  2. createtable cities (city varchar(80), location varchar(80));

i mam program:

#include <iostream>
#include <soci.h>
#include <postgresql/soci-postgresql.h>
#include <string>
using namespace std;
 
int main(int argc, char **argv)
{
   try
   { 
      soci::session sql(soci::postgresql, "dbname=testDB");

	sql << "insert into cities values('Londyn', 'UK')";

	string row = "";
	sql << "select * from cities;", soci::into(row);
	cout << row << "\n"; 
   }
   catch (soci::postgresql_soci_error const & e)
   {
      std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
   }
   catch (std::exception const & e)
   {
      std::cerr << "Some other error: " << e.what() << std::endl;
   }
   return 0;

}

ale nic nie wyświetla, nawet w bazie select * pokazuje pustkę :/ co znów robię nie tak ... ?

0

A jednak nie - taki kod:

#include <iostream>
    #include <soci.h>
    #include <postgresql/soci-postgresql.h>
    #include <string>
    using namespace std;
     
    int main(int argc, char **argv)
    {
       try
       { 
          soci::session sql(soci::postgresql, "dbname=testDB");
    
    	string row = "";
    	sql << "select * from cities;", soci::into(row);
    
    	sql << "insert into cities values('London', 'UK')";
    	
    	sql << "select * from cities;", soci::into(row);
    	cout << row << "\n"; 
       }
       catch (soci::postgresql_soci_error const & e)
       {
          std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
       }
       catch (std::exception const & e)
       {
          std::cerr << "Some other error: " << e.what() << std::endl;
       }
       return 0;
    
    }

pokazuje mi tylko to, co mam w bazie - a nie widzi wykonania komendy ...wyświetla tylko 1 wynik

0

Spoko, jednak dałem radę:

#include <iostream>
#include <string>
#include <soci.h>
#include <postgresql/soci-postgresql.h>
#include <string>
using namespace std;
using namespace soci;
     
    int main(int argc, char **argv)
    {
       try
       { 
         soci::session sql(soci::postgresql, "dbname=testDB");
    
    	sql << "insert into cities values('London', 'UK')";
	sql.commit();
    	
    	rowset<row> rs = (sql.prepare << "select name, location from cities");

for (rowset<row>::const_iterator it = rs.begin(); it != rs.end(); ++it)
{
    row const& row = *it;

    // dynamic data extraction from each row:
    cout << "city: " << row.get<std::string>(0) << '\n'
         << "location: " << row.get<std::string>(1) << '\n';
}
       }

       catch (soci::postgresql_soci_error const & e)
       {
          std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
       }
       catch (std::exception const & e)
       {
          std::cerr << "Some other error: " << e.what() << std::endl;
       }
       return 0;
    
    }

kod działa i ma się dobrze:)

0

Hej, norma, zaglądasz tu może jeszcze?:) Wiesz, jak zainstalować SOCI na Windowsie? Jak mam tylko mingw i nic poza tym? Dokumentacja jest oszczędna w słowach, jeśli o to chodzi, ale może robiłeś to już i mógłbyś podpowiedzieć?

0

Mam tylko Linuksa.

1 użytkowników online, w tym zalogowanych: 0, gości: 1