0

I am developing a library where the size of some variables depends on a #define and some #define which are created depending on the value of other #define.

e.g.

int variable1[SIZE_USER]
#if SIZE_USER>3
  #define CONDITION 1
#else
  #define CONDITION 0
#endif

The idea is that when a user wants to work with the library they create there own header file with all the preprocessor directives (#define) that are needed and uses this file in the same directory where there "main.cpp" file is and not inside the library source files.

The problem is that when I include the configuration file (which has all the #define's) in the same directory where all my header files of the library are I do not have problems.

i.e.

#include <config.h>
//My Library Code...

But if I declare the configuration header file outside the source files of my Library the compiler doesn't find the #define's that were declared in the "main.cpp" file.

i.e.

#include<config.h>
#include<myLibrary.h>
//User code...

Is there something obvious I am missing on how the compiler is working?

6
  • How and where SIZE_USER is created?
    – Jose
    May 17, 2018 at 14:09
  • it is created in an external header file e.g. "config.h"
    – Yudop
    May 17, 2018 at 14:11
  • When the library source files are compiled, the compiler needs to have access to the user header file. So either this header file has to be located where the source code is, or the path has to be passed when compiling, e.g. by passing it to the compiler using the -I flag. Is the library compiled at the same time as the user program which consumes it? May 17, 2018 at 14:11
  • 1
    precompiled headers?
    – Reinhold
    May 17, 2018 at 14:11
  • 1
    Use angle brackets only for .h files provided by the compiler. When you make it difficult for the compiler to find a .h file then there is always a risk that the compiler finds another config.h file. Sounds like that is exactly what happened, "config.h" is far too common a filename. It is diagnosable, but you have to tell us what kind of compiler you use to get the most usable answer. May 17, 2018 at 14:18

1 Answer 1

2
#define SIZE_USER 4

int variable1[SIZE_USER]
#if SIZE_USER > 3
  #define CONDITION 1
#else
  #define CONDITION 0
#endif

First problem is missing spaces in the #if directive.

#include "myLibrary.h"

Second problem: use "" for your included headers and spaces matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.