繁体   English   中英

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

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

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

要使用我安装的SOCI:

1)libboost-dev 2)libpq-dev 3)libtool 4)SOCI,使用这个: http ://soci.sourceforge.net/doc/backends/postgresql.html#required并用这个命令编译社会: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:致命错误: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)

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

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

它应该编译但可能无法运行。 如果它没有运行,我必须做更多:

1)创建soci.conf文件:

触摸/etc/ld.so.conf.d/soci.conf

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

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

3)粘贴在soci.conf中:

在/ usr /本地/ lib64下

(对于Xubuntu x64)或

在/ usr / local / lib目录

(对于Xubuntu x32)并保存文件

4)运行命令:

LDCONFIG

5)运行我编译的社交网站:./ socitest,它是wooooorking! :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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM