我正面临一个困扰了我一段时间的编程问题。我打算开始一个个人项目,我需要使用数据库来存储某些信息,我决定使用SQLite,但是我不喜欢C-ish API,所以我在SQLite wiki中遇到了SOCI包装器。
我去了SOCI官方网站,阅读了文档,决定试一试。我按照文档中"安装"一章的说明进行操作,在安装了所有要求之后,我编译并安装了它:
cmake -DWITH_BOOST=ON -DSOCI_TESTS=ON -DWITH_SQLITE3=ON
make
make test
sudo make install
所有测试都成功完成,但是当尝试运行(在使用g++ test.cpp -o1 -lsoci_core -lsoci_sqlite3
编译后)一个程序时,例如:
test.cpp:
#include "soci/soci.h"
#include "soci/sqlite3/soci-sqlite3.h"
#include <iostream>
int main()
{
soci::session sql(soci::sqlite3, "testdb.db");
return 0;
}
我得到一个错误说:"错误,而加载共享库:libsoci_sqlite3.so.3.1:不能打开共享对象文件:没有这样的文件或目录。"但看看安装日志,我可以清楚地看到,共享库已安装。
我想我已经找到问题了。做一个:
strace -e open ./1 2>&1 | grep soci
输出如下内容:
open("/usr/local/lib/libsoci_core.so.3.1", O_RDONLY) = 3
open("/lib/x86_64-linux-gnu/tls/x86_64/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/tls/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/x86_64/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/tls/x86_64/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/tls/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/x86_64/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib/tls/x86_64/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib/tls/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib/x86_64/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/tls/x86_64/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/tls/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/libsoci_sqlite3.so.3.1", O_RDONLY) = -1 ENOENT (No such file or directory)
./1: error while loading shared libraries: libsoci_sqlite3.so.3.1: cannot open shared object file: No such file or directory
通过查看它,您可以清楚地看到它只在soci_core
中搜索/usr/local/lib/
,而通常它也应该搜索soci_sqlite3
。一个快速和肮脏的hack,解决这个问题是创建一个smylink到libsoci_sqlite3.so.3.1
在任何其他文件夹中列出,但我很确定,有一个更好的方法来修复它。
在您的SOCI安装库位于/usr/local/lib64/
下面的语句应该可以工作:
g++ test.cpp -o test -I/usr/local/include/soci -L/usr/local/lib64/-lsoci_core -lsoci_sqlite3
- wl,当地的rpath =/usr/lib64/