Search code examples
c++sqlsoci

undefined reference to soci::session::get_last_insert_id


So, I have downloaded soci 4.0.0 from souceforge and unpacked it into /tmp/. Then:

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

And I got:

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

I dont care about the posgresql fails (probably missing some development files)

But then I try complie this socitest.cpp file:

#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 

And I got this error:

/usr/bin/ld: /tmp/ccwEUoUG.o: in function main': socitest.cpp:(.text+0x272): undefined reference tosoci::session::get_last_insert_id(std::__cxx11::basic_string, std::allocator > const&, long&)' collect2: error: ld returned 1 exit status

When I comment out

if(!sql.get_last_insert_id("test", id)) {
        std::cout << "cannot get last inserted id" << std::endl;
}

I can compile the program, but after running it I got SIGSEGV:

LD_LIBRARY_PATH=lib ./socitest
ok
created
Neoprávněný přístup do paměti (SIGSEGV) (core dumped [obraz paměti uložen])

What I am doing wrong?


Solution

  • I found the error.

    Instead of

    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

    it should be

    g++ -std=c++14 -o socitest socitest.cpp -I../include/ -I./include/ -I/usr/include/ -L./lib -lsoci_core -lsoci_sqlite3 -lsqlite3 -ldl

    First of all, I include #include <soci/soci.h>, therefore the -I shoud be ../include/ and not ../include/soci/

    I also had installed soci v 3 in the system, so g++ find the old headers and old libraries in /usr/lib/x86_64-linux-gnu and somehow mixed the headers and libraries up with the v 4 and causes the problems mentioned in the question.