1

データベースを使用するアプリケーションの書き方を最終的に知りたいです。C++、PostgreSQL、および SOCI (C++ への SQL ラッパー) を選択しました。Xubuntu 11.4 を使用し、簡単なプログラムを実行するために必要なすべてをインストールしました。

私がインストールしたSOCIを使用するには:

1) libboost-dev 2) libpq-dev 3) libtool 4) SOCI: http://soci.sourceforge.net/doc/backends/postgresql.html#requiredを使用し、次のコマンドで SOCI をコンパイルしました: cmake cmake - G "Unix Makefile" -DWITH_BOOST=ON -DWITH_POSTGRESQL=ON ../

私の単純なプログラムは非常に単純です。

#include "soci-postgresql.h"

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

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

私は次のようにコンパイルします:

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

しかし、それは私にエラーを与えます:

test.cpp:1:29: 致命的なエラー: soci-postgresql.h: そのようなファイルまたはディレクトリのコンパイルは終了しませんでした。

これを修正する方法、何が問題なのですか? 何かをインストールするのを逃しましたか?

いくつかの詳細情報:

/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

私もこれを試しましg++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci/postgresqlた:そしてエラーが発生しました:

g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci/postgresql test.cpp:1:0 からインクルードされたファイル: /usr/local/include/soci/postgresql/soci-postgresql .h:27:26: 致命的なエラー: soci-backend.h: そのようなファイルまたはディレクトリのコンパイルは終了しませんでした。

4

4 に答える 4

1

わかりました、洗練されたプログラミング Web サイトで解決策を見つけました。

私の新しいコード (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;

}

そして、私はそれをコンパイルする必要があります:

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

コンパイルする必要がありますが、実行されない可能性があります。実行されていない場合は、さらに sth を実行する必要があります。

1) soci.conf ファイルを作成します。

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

2) gedit で soci.conf を編集します。

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

3) soci.conf に貼り付けます:

/usr/local/lib64

(Xubuntu x64 の場合) または

/usr/local/lib

(Xubuntu x32の場合)ファイルを保存します

4) コマンドを実行します。

ldconfig

5) コンパイルした socitest を次のように実行します。:D

私を助けてくれてありがとう:)

于 2012-07-11T11:05:28.857 に答える
1

g++ test.cpp -Iyour_soci_dir -lsoci_core -lsoci_postgresql -ldl -lpq
your_soci_dir は、soci インクルード ファイルがインストールされたディレクトリです。

于 2012-07-06T11:11:08.000 に答える
1

オプションで検索パスを設定する必要があり-Iます。

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

すべてのパスを指定する必要があります。あなたの場合、次のようになるはずです:

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

別のオプション (makefile でより使用されます) もあります: pkg-configを使用することです。ここでそのような例を見つけることができます:

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)

あなたの場合、それは次のようなものになります(テストされていません):

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)
于 2012-07-06T11:15:21.883 に答える