Question

I want to finally get to know how to write an application which uses database. I chose C++, PostgreSQL and SOCI (SQL wrapper to C++). I use Xubuntu 11.4 and installed everyting which was necessary to run a simple program.

To use SOCI I installed:

1) libboost-dev 2) libpq-dev 3) libtool 4) SOCI, using this: http://soci.sourceforge.net/doc/backends/postgresql.html#required and I compiled SOCI with this command: cmake cmake -G "Unix Makefiles" -DWITH_BOOST=ON -DWITH_POSTGRESQL=ON ../

My simple program is veeery simple:

#include "soci-postgresql.h"

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

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

I compile it like this:

g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq

but it gives me error:

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

How to fix this, whats wrong? Did I miss to install something?

Some more infos:

/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

I also tried this one: g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci/postgresql and got the error:

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.

Was it helpful?

Solution 3

Ok guys, I found a solution on a polish programming website.

My new code (socitest.cpp):

#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;

}

And I have to compile it :

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

It should compile but it might not run. If it's not running, I have to do sth more:

1) create soci.conf file:

touch /etc/ld.so.conf.d/soci.conf

2) edit my soci.conf with gedit:

gedit /etc/ld.so.conf.d/soci.conf

3) paste in soci.conf:

/usr/local/lib64

(for Xubuntu x64) or

/usr/local/lib

(for Xubuntu x32) and save file

4) run command:

ldconfig

5) run my compiled socitest like this: ./socitest and it's wooooorking !!! :D

Thanks all of you for helping me:)

OTHER TIPS

g++ test.cpp -Iyour_soci_dir -lsoci_core -lsoci_postgresql -ldl -lpq
your_soci_dir is directory with installed soci include files.

You need to set the search path with -I option.

g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I<path to headers>
                                                       ^^^^^^^^^^^^^^^^^ Put the path

You need to provide all paths. I guess in your case, it should be like this :

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

There is also another option (which is more used in makefiles) : to use pkg-config. Here you can find such example :

PROGRAM = test
PROGRAM_FILES = test.c

CFLAGS  += -g $(shell pkg-config --cflags xmlsec1-nss)
LDFLAGS += -g
LIBS    += $(shell pkg-config --libs xmlsec1-nss) 

all: $(PROGRAM)

%: %.c 
    $(cc) $(PROGRAM_FILES) $(CFLAGS) $(LDFLAGS) -o $(PROGRAM) $(LIBS)

clean:
    @rm -rf $(PROGRAM)

In your case, it would be something like this (not tested) :

PROGRAM = test
PROGRAM_FILES = test.cpp

CC=g++
CXXFLAGS    += -g $(shell pkg-config --cflags soci_core) $(shell pkg-config --cflags soci_postgresql)
LDFLAGS += -g
LIBS    += $(shell pkg-config --libs soci_core) $(shell pkg-config --libs soci_postgresql) 

all: $(PROGRAM)

%: %.cpp
    $(CC) $(PROGRAM_FILES) $(CFLAGS) $(LDFLAGS) -o $(PROGRAM) $(LIBS)

clean:
    @rm -rf $(PROGRAM)

Above example code can be compiled with just one command, without soci.conf and ldconfig:

g++ socitest.cpp -o socitest -I/usr/include/postgresql -I/usr/local/include/soci -L/usr/local/lib64/ \
-lsoci_core -lsoci_postgresql -Wl,-rpath=/usr/local/lib64/

For 32 bit unix use /usr/local/lib instead of /usr/local/lib64

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top