How to use SOCI C++ Database library?
Asked Answered
C

2

9

I'm trying to implement soci in my program but I don't know how. I'm using C++ on Linux, on a project using netbeans. I have followed the steps in: http://soci.sourceforge.net/doc/structure.html to install it, and I tried to copy the files soci.h from /src/core and soci-mysql.h from /src/backends/mysql in my project but it gives a compilation error (these files include other soci files, but it's illogical to copy all files into the directory...). I have read the guide several time but I don't understand what I'm doing wrong. The examples only include these files.

Thanks.

Edit: I have given more information in a comment below the answer. I don't know what steps I have to follow to implement soci.

Clodhopping answered 31/12, 2010 at 17:44 Comment(2)
Did you actually run make install? There's no need to copy header files to your project's directory.Sheff
I did ran that command. If I don't copy those files the compiler gives a error saying that the files soci.h and soci-mysql.h weren't found. I just copies the following includes from a example on the documentation: [code] #include "soci.h" #include "soci-mysql.h" [/code] but it didn't work, the compiler said that those files didn't exist, so I tried to copy them from the soci package.Clodhopping
R
8

The relevant bit on that page is

When the configure script is run without parameters, the remaining part of the process will use /usr/local/include/soci as a default destination for SOCI header files and /usr/local/lib as a default destination for library files

Now /usr/local/include ought to be in your default include path (e.g. try something like gcc -c -v -x c++ /dev/null -o /dev/null to see the list your install uses) and so you can include these using

#include <soci/soci.h>
#include <soci/soci-mysql.h>

You then need to add the libraries to your link step. It looks like you'll have both static and shared versions of the libraries. You'll need to add -lsoci_core -lsoci_mysql to your link step; however if that doesn't work then you'll also need to specify /usr/local/lib as a search directory i.e. -L/usr/local/lib -lsoci_core -lsoci_mysql. (Again it's probably there already but you can see using gcc -print-search-dirs.) However, the issue then is that if you're using the shared version and /usr/local/lib isn't in your distributions library search path (see /etc/ld.so.conf and/or /etc/ld.so.conf.d/*) then it won't be able to find the shared library at runtime. You'll need to either hard-code in the path to the library with the linker switch -rpath or add /usr/local/lib to the system-wide search path as before or in your environment (variable LD_LIBRARY_PATH). I'm not sure what the best way to do this is - I'd suggest -rpath to avoid modifying the system in general, although if you're building a lot of libraries into /usr/local/lib it might make sense to add it.

Rensselaerite answered 2/1, 2011 at 21:48 Comment(4)
The library throws the exception "Failed to find shared library for backend mysql" even specifying /usr/local/lib as a search directory and -rpath, but I could solve it copying the three mysql backend .so files to the project directory. Thanks!Clodhopping
Hmm, I might have that wrong then: it's possible you only need to link in -lsoci_core and then it will dlopen the mysql library itself? It's odd it can't then find the library, though, as it would have known that /usr/local/lib would be the place to look at build time. Sorry, I don't know soci specifically to suggest anything better but glad you got it working!Rensselaerite
No, I have just tried that and it doesn't work without the -lsoci_mysql option. I don't know why, but soci also attemps to dlopen the mysql backend library. I found the file where that happens searching the previus error on google: code.google.com/p/boost-soci/source/browse/trunk/libs/soci/src/… PD: Can you tell me how you put "code tags" on comments?Clodhopping
Neast - from that file it looks like you could have also set environment variable SOCI_BACKENDS_PATH to /usr/local/lib. Sure, you can surround code with backticks ` in comments. I've also seen people link text somehow but I don't know how to do that, sorry.Rensselaerite
S
2

I got the same doesn't load backend error on my C++ program when I execute session sql("mysql://db=...)

I found a solution (at least on my Ubuntu 11.04). Just do:

sudo -i ln -s /usr/lib/libsoci_mysql-gcc-3_0-3.0.0.so /usr/lib/libsoci_mysql.so

It seem that the SOCI library search for the file /usr/lib/libsoci_mysql.so that is not in the system, buy if you make a link to the library /usr/lib/libsoci_mysql-gcc-3_0-3.0.0.so that it's in the system it works (I think debian/ubuntu makes a file name change from the original name, but it have side effects because the SOCI library search inside for the original name).

I found the error using the bash environment variable LD_DEBUG=files and running my C++ binary.

Hope it helps.

Saprophagous answered 29/11, 2011 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.