对 soci::session::get_last_insert_id 的未定义引用

undefined reference to soci::session::get_last_insert_id

所以,我已经从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 tosoci::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 中找到了旧的头文件和旧的库,并以某种方式将头文件和库与 v 4 混在一起,导致了问题。