I have the following code which includes a library based on a #define'd variable:
#if LIB_SELECTOR==1
#include "path/to/lib1.h"
#else
#include "path/to/lib2.h"
#endif
However, this library needs to be included in a number of source files, and maintaining this conditional across all of them could be problematic and prone to error. Ideally, I would simply do something like:
#if LIB_SELECTOR==1
#define DYNAMIC_LIB "path/to/lib1.h"
#else
#define DYNAMIC_LIB "path/to/lib2.h"
#endif
Then, in all of the requisite source files, simply use
#include DYNAMIC_LIB
I've tried it in my code, and received the error error: #include expects "FILENAME" or <FILENAME>. This doesn't give me much hope, but I am wondering what the proper way to solve this problem is.
What you have should work, but the files that use #include DYNAMIC_LIB have to have DYNAMIC_LIB defined first. On a source file that fails, you can test with:
#ifndef DYNAMIC_LIB
#error "DYNAMIC_LIB is not defined!"
#endif
#include DYNAMIC_LIB
You don't have to maintain that conditional in all your source files. You can create one header file that has the conditional, and let your other source files include that file instead. For example:
/* lib_selector.h */
#pragma once
#if LIB_SELECTOR==1
#include "path/to/lib1.h"
#else
#include "path/to/lib2.h"
#endif
Then all your other source files would #include "lib_selector.h".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With