gpt4 book ai didi

c++ - 未定义对soci::session::get_last_insert_id的引用

转载 作者:行者123 更新时间:2023-12-02 10:15:32 25 4
gpt4 key购买 nike

因此,我从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失败(可能缺少一些开发文件)

但是然后我尝试编译这个socitest.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:在函数 main':
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应该是../include/而不是../include/soci/

我还在系统中安装了soci v 3,因此g++在/ usr / lib / x86_64-linux-gnu中找到了旧的 header 和旧的库,并以某种方式将 header 和库与v 4混合在一起,并导致了题。

关于c++ - 未定义对soci::session::get_last_insert_id的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62112695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com