This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CODECOMPOSER: include multiple files with different extension

Part Number: CODECOMPOSER


Hello 

I have a project where I need to include different files depending on the hardware. The files cannot be combined and differ only in the extension at the end before the file extension, for example

#include "V2.11_M182_K.h"
#include "V2.11_M182_D.h"

Es sind ca. 10 Dateien. Ich würde ungern immer alle Namen von Hand anpassen. Leider geht die einfache Version:

#define  NAME "V2.11_M182"

#include NAME "_K.h"
#include NAME "_D.h"

Does anyone have any ideas?

I found already a solution with numbers:

Concatenate string in C #include filename

I was unfortunately not able to rewrite this for two strings. 

Stefan

  • Consider using a preprocessor symbol defined on the command line.  Then use #if to select which file to include.  The source code could look like ...

    #if defined(HW_CONFIG_K)
    #include "V2.11_M182_K.h"
    #elif defined(HW_CONFIG_D)
    #include "V2.11_M182_D.h"
    #else
    #error Missing hardware configuration
    #endif

    On the command line, you must have either -DHW_CONFIG_K or -DHW_CONFIG_D.  Otherwise, that #error causes the build to fail.

    Thanks and regards,

    -George

  • Hello George,

    thank you for the answer. Unfortunately the question was meant a bit different. 

    I always need all h-files. However, the name changes depending on the device. For example for devices V2.11_M182 I need

    #include "V2.11_M182_K.h"
    #include "V2.11_M182_D.h"

    After that I have a device with designation V5.31_P371 then I need

    #include "V5.31_P371 _K.h"
    #include "V5.31_P371 _D.h"

    I hope the question is clearer now and there is an answer for it.

    The h-files are generated via Excel. Device data is entered and then calculated via scripts. It should be possible to combine the files to two files. But it must be two files because of the integration of defines and variables/constants.

    The "_K.h" contains variables and constants, which are already declared as external in another file.
    In the file "_D.h" are various #defines which are used for the preprocessor.

    Regards

    Stefan

  • How about ...

    #define stringify(x) #x
    #define DN2(a, b) stringify(a ## _ ## b ## .h)
    #define DN(a, b) DN2(a, b)
    
    #ifndef DEVICE
    #error Must define DEVICE on the command line
    #endif
    
    #include DN(DEVICE, K)
    #include DN(DEVICE, D)

    To avoid the #error, the command line must include something similar to -DDEVICE=V2.11_M182 .

    Thanks and regards,

    -George

  • Hello George,

    thank you for the solution. This will help me a lot.

    Regards

    Stefan