所以,我从souceforge下载了soci 4.0.0并将其解压缩到/tmp/中。 然后:
cd /tmp/soci-4.0.0
mkdir build
cd build
cmake -G "Unix Makefiles" -DWITH_BOOST=OFF -DWITH_ORACLE=OFF -DSOCI_TESTS=ON -DWITH_SQLITE3=ON -DSOCI_SQLITE3_TEST_CONNSTR="test.db" ../
make
make test
我得到了:
Running tests... Test project /tmp/soci-4.0.0/build
Start 1: soci_empty_test 1/6 Test #1: soci_empty_test .................. Passed 0.00 sec
Start 2: soci_empty_test_static 2/6 Test #2: soci_empty_test_static ........... Passed 0.00 sec
Start 3: soci_postgresql_test 3/6 Test #3: soci_postgresql_test .............***Failed 0.01 sec
Start 4: soci_postgresql_test_static 4/6 Test #4: soci_postgresql_test_static ......***Failed 0.01 sec
Start 5: soci_sqlite3_test 5/6 Test #5: soci_sqlite3_test ................ Passed 1.30 sec
Start 6: soci_sqlite3_test_static 6/6 Test #6: soci_sqlite3_test_static ......... Passed 1.28 sec
67% tests passed, 2 tests failed out of 6
Total Test time (real) = 2.61 sec
The following tests FAILED:
3 - soci_postgresql_test (Failed)
4 - soci_postgresql_test_static (Failed) Errors while running CTest make: *** [Makefile:86: test] Chyba 8
我不在乎posgresql失败(可能缺少一些开发文件(
但是后来我尝试遵守这个最社会.cpp文件:
#include <soci/soci.h>
#include <soci/sqlite3/soci-sqlite3.h>
#include <exception>
#include <iostream>
#include <string>
#include <memory>
int main() {
std::string dbname=":memory:";
soci::session sql(soci::sqlite3, dbname);
std::cout << "ok" << std::endl;
sql.once << "create table test(id integer primary key AUTOINCREMENT, s text);";
std::cout << "created" << std::endl;
sql.begin();
sql.once << "insert into test(s) values(:s)", soci::use(dbname);
std::cout << "inserted" << std::endl;
sql.once << "insert into test(s) values(:s)", soci::use(dbname);
long id = 0;
if(!sql.get_last_insert_id("test", id)) {
std::cout << "cannot get last inserted id" << std::endl;
}
std::cout << "inserted id = " << id << std::endl;
int i;
soci::statement st = (sql.prepare << "select last_insert_rowid();", soci::into(i));
st.execute();
while (st.fetch())
{
std::cout << i << 'n';
}
sql.commit();
return 0;
}
g++ -std=c++14 -o socitest socitest.cpp -I../include/soci/ -I ../include/private/ -I/usr/include/ -L./lib -L/usr/lib/x86_64-linux-gnu -lsoci_core -lsoci_sqlite3 -lsqlite3 -ldl
我得到了这个错误:
/usr/bin/ld:/tmp/ccwEUoUG.o: in functionmain':
socitest.cpp:(.text+0x272): undefined reference to
soci::session::get_last_insert_id(std::__cxx11::basic_string, std::allocator> const&, long&(' collect2:错误:ld 返回 1 个退出状态
当我注释掉时
if(!sql.get_last_insert_id("test", id)) {
std::cout << "cannot get last inserted id" << std::endl;
}
我可以编译程序,但是运行它后我得到了SIGSEGV:
LD_LIBRARY_PATH=lib ./socitest
ok
created
Neoprávněný přístup do paměti (SIGSEGV) (core dumped [obraz paměti uložen])
我做错了什么?
我发现了错误。
而不是
g++ -std=c++14 -o socitest socitest.cpp -I../include/soci/-I ../include/private/-I/usr/include/-L./lib -L/usr/lib/x86_64-linux-gnu -lsoci_core -lsoci_sqlite3 -lsqlite3 -ldl
它应该是
g++ -std=c++14 -o socitest socitest.cpp -I../include/-I./include/-I/usr/include/-L./lib -lsoci_core -lsoci_sqlite3 -lsqlite3 -ldl
首先,我包括#include <soci/soci.h>
,因此-I
应该是../包括/而不是 ../包括/社会/
我还在系统中安装了 soci v 3,因此 g++ 在/usr/lib/x86_64-linux-gnu 中找到了旧的标头和旧库,并以某种方式将标头和库与 v 4 混合在一起,并导致问题中提到的问题。