在Xubuntu上使用SOCI(sql包装器),简单的程序在编译时失败

Using SOCI (sql wrapper) on Xubuntu, simple program fails while compilig

本文关键字:简单 程序 失败 编译 包装 Xubuntu SOCI sql      更新时间:2023-10-16

我想最终了解如何编写使用数据库的应用程序。我选择了C++,PostgreSQL和SOCI(SQL包装器到C++)。我使用 Xubuntu 11.4 并安装了运行简单程序所必需的一切。

要使用 SOCI I 安装:

1) libboost-dev2) libpq-dev3)libtool4)SOCI,使用这个:http://soci.sourceforge.net/doc/backends/postgresql.html#required 和我用这个命令编译了SOCI:cmake cmake -G "Unix Makefiles" -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: fatal 错误:soci-backend.h:没有这样的文件或目录编译 终止。

g++ test.cpp -Iyour_soci_dir -lsoci_core -lsoci_postgresql -ldl -lpq
your_soci_dir是包含已安装 soci 包含文件的目录。

您需要

使用-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)

好的伙计们,我在波兰编程网站上找到了解决方案。

我的新代码(社会.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

它应该编译,但它可能不会运行。如果它没有运行,我必须做更多的事情:

1) 创建 soci.conf 文件:

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

2)使用gedit编辑我的soci.conf:

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

3) 粘贴到社会共享:

/

usr/local/lib64

(对于 Xubuntu x64)或

/

usr/local/lib

(对于 Xubuntu x32)并保存文件

4) 运行命令:

LDCONFIG

5)像这样运行我编译的socitest:./socitest,它正在呜呜!!:D

谢谢大家帮助我:)

上面的示例代码可以只用一个命令编译,不需要 soci.conf 和 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/

对于 32 位 unix,请使用/usr/local/lib 而不是/usr/local/lib64