I want to do database coding in c++. What modern C++ libraries should be in my toolbox? shows that SOCI is a good library which is cross-platform and free. But the installation of SOCI is a big issue, since I can't find a good blog or tutorial or article or anything giving a detailed rundown. I followed the instructions on the SOCI official page http://soci.sourceforge.net/doc/3.2/installation.html but after succesfully (kinda since it doesn't detect boost. so I had to run it without boost) running cmake, when I build the .sln in visual studio, it gives me 4 errors. I tried everything for days, but nothing seems to work. There is just one video on yt https://www.youtube.com/watch?v=gFGLKaDnwmI, but it shows a method were you have to manipulate the micros in the lib files. I don't want to do that. And since I thought it was a reputed and popular lib, I wouldn't have to to that. Anyways, the build o/p is too large, so I have posted the errors that I get. The first one appears three times. So, that why I think I am getting the 4 failed in the final result.

5>C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt\stdio.h(1935): fatal error C1189: #error:  Macro definition of snprintf conflicts with Standard Library function declaration

8>LINK : fatal error LNK1104: cannot open file '..\..\..\lib\Debug\libsoci_postgresql_3_2.lib'

========== Build: 8 succeeded, 4 failed, 0 up-to-date, 3 skipped ==========

I have another question which is not important but just as an fyi (don't flag me for this. If you want I'll delete it). Is database programming not done in c++ that often? or if done do c++ coders prefer the C apis more than c++ ones? Cos, I have had real trouble finding good recent articles or blog on the same.

2 Answers

Aleksey On

What version of soci are you trying to build? I found thread with your error:

The build fails because there are two included files that redefine the 'snprintf' symbol. The first definition is in "D:\devsrc\soci-3.2.3\core\soci-platform.h" and the second one (which produces the error) is in "C:\Program Files (x86)\Windows Kits\10\include\10.0.10150.0\ucrt\stdio.h" (a Standard Library file).

To fix the error, comment the line 27 of "D:\devsrc\soci-3.2.3\core\soci-platform.h":

// #define snprintf _snprintf

, so that SOCI ignores its own definition and uses that of the Standard Library instead.

However the author of library states:

For the record, I have just managed to build SOCI 3.2.3 with VS2015 (CL.EXE version19.00.24215.1) without any problems or modifications required.

git clone https://github.com/SOCI/soci.git git co release/3.2 mkdir _build cd _build cmake ..

then load SOCI.sln with VS2015 and build.

Also you can try to build master branch (aka 4.0.0 version).

Vaibhav_M On

Apparently this issue is not just with SOCI. While searching for solution, I came across a lot of other libraries who are having the same issue with VS17. https://github.com/robotology/icub-firmware-shared/issues/25 https://forum.juce.com/t/solved-error-with-vs2015-regarding-snprintf/14831 to link a few. Seems like after some version the 'snprintf' was added to VS (Standard Library). So its conflicting with the macros of all the libraries who use it.

The only way to get around it is to either change the name of the macro manually (like shown in the video) or edit the macro definitions (which I did and prefer cos its far cleaner) as follows :

#define snprintf _snprintf


#if _MSC_VER < 1900
#define snprintf _snprintf

This got rid of all my errors and the solution build succesfuly without any errors. I don't know why the people maintaining haven't make changes to the library itself to avoid this . I hope to get in touch with them and ask them to do the same.

Hope this helps everyone who is having similar issues with any library.