Use conditional compilation to check if user has SQL Native Client? (ODBC)

54 views Asked by At

I want to use sqlncli.h but only if it is available to the user, and if not, I want to use less optimal but functional replacement code (I really just want to use Multiple Active Result Sets, other DBMSes don't seem to require special configuration for this). The user will have the source files, and will be building it themselves on their machine, so can I use some sort of preprocessor/conditional compilation flag to handle this?

I know there are flags like _WIN32 I can use to check if the user is on windows (which I do, because windows users need windows.h to use my code).

This is a general purpose ODBC lib so I don't know whether a user will even be connecting to SQL Server or not.

Is my only option to have the user specify whether or not they want to use SQL Server+Native Client (by defining some flag for example)

2

There are 2 answers

2
SoronelHaetir On

If you have a modern compiler (c++17) you can use __has_include to test for the existence of a particular file:

#if __has_include(<sqlncli.h>)
1
Miles Budnek On

This sort of thing is usually handled by a build-system generator like CMake or Automake. As part of generating your build scripts, you attempt to compile a simple program that includes the relevant library. If it succeeds you either change the way your build scripts get generated to compile different files and/or you set some preprocessor flags so that you can #ifdef-select the correct implementation.