0

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)

gabriel.hayes
  • 2,267
  • 12
  • 15

2 Answers2

1

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>)
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • Hm. I guess I'll have to see if cl.exe supports that, I have heard it is not as up-to-date as other compilers but it is very convenient for windows users – gabriel.hayes Mar 27 '20 at 17:48
  • 1
    According to [MSVC C++ features](https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019) msvc has supported __has_include since vc2017 version 15.3. You will need to specify c++17 as the language standard. – SoronelHaetir Mar 27 '20 at 18:08
0

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.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • If all else fails I'll give this a shot. in general I want to avoid an unnecessary compilation however. The build-system I have in place is a made in-house (this C code is effectively used as FFI so some special things need to happen for the target platform) – gabriel.hayes Mar 27 '20 at 19:08