Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: hanshuckebein on December 13, 2020, 12:33:02 am

Title: How to #define a header file name in Compiler settings?
Post by: hanshuckebein on December 13, 2020, 12:33:02 am
Some of my source files contain lines like this:

   #include UART_CONFIG_H       // Symbol to be defined via compiler -D option

Defining the name under Build options | Compiler settings | #defines like this:

   UART_CONFIG_H="uart_config.h"

produces the following compiler invocation:

avr-gcc … -DUART_CONFIG_H="uart_config.h" -I. -I.. -I/usr/lib/avr/include -c ../uart-0.c -o obj/Release/uart-0.o

but this leads to a compiler error:

../uart-0.c:24:10: error: #include expects "FILENAME" or <FILENAME>

Why?

How to define UART_CONFIG_H in Build options | Compiler settings | #defines?
Title: Re: How to #define a header file name in Compiler settings?
Post by: zainka on December 13, 2020, 01:09:36 am
Use -D
Add -D to the compiler followed by the define of interest
-DUART_CONFIG_H

An example i use (not complete but ). As you may see you can also pass a value

arm-none-eabi-gcc -c -DUSE_HAL_DRIVER -DUART_CONFIG_H -DSYSTEM_TIME=10
Title: Re: How to #define a header file name in Compiler settings?
Post by: hanshuckebein on December 13, 2020, 01:41:00 am
Sorry, but that is no answer to my question. I want to know, how to do it via Build options | Compiler settings | #defines
Title: Re: How to #define a header file name in Compiler settings?
Post by: stahta01 on December 13, 2020, 01:44:35 am
I think you did the CB part right.

But you might need to change
Code
#include UART_CONFIG_H       // Symbol to be defined via compiler -D option
To
Code
#include "UART_CONFIG_H"       // Symbol to be defined via compiler -D option

If the above fails try defining in CB as

Code
UART_CONFIG_H="\"uart_config.h\""

Tim S.

Title: Re: How to #define a header file name in Compiler settings?
Post by: stahta01 on December 13, 2020, 02:31:29 am
Looks like you need a C or C++ Macro expert to answer this question.

Because I can not get it to work using a #define statement in C; therefore I can not help you figure out what looks possibly impossible.

Tim S.
Title: Re: How to #define a header file name in Compiler settings?
Post by: stahta01 on December 13, 2020, 02:38:06 am
Found example code that worked for what you wanted.

In CB I have this set
Code
INCLUDE_FILE="stdlib.h"
Edit: The CB Define can also be done without the double quotes as in
Code
INCLUDE_FILE=stdlib.h
and it still works.

The program code had this below.
Code
#include <stdio.h>

#define STRINGIFY(X) STRINGIFY2(X)   
#define STRINGIFY2(X) #X

#include STRINGIFY(INCLUDE_FILE)

int main()
{
    printf("Hello world!\n");
    return 0;
}
Title: Re: How to #define a header file name in Compiler settings?
Post by: oBFusCATed on December 13, 2020, 01:04:16 pm
The shell is probably removing the "" from your string.
Try to use double or triple quotes.
Unfortunately we don't have a macro to preserve quotes. We only have REMOVE_QUOTES.
The other option seems to be to use escaping like \".
I'm not sure this is cross platform or if you need it to be cross platform.
Title: Re: How to #define a header file name in Compiler settings?
Post by: hanshuckebein on December 13, 2020, 03:19:45 pm
@stahta01:

Your trick using STRINGIFY doesn't work with avr-gcc. This probably depends on the behavior of the c preprocessor and might be implementation dependent.

@oBFusCATed:

I tested several versions with double and triple " with and without preceding \ – nothing works. It seems to be impossible to define a double quoted -D macro. Also using ' to protect " doesn't help.

The only way I found, to do the trick is adding the folder which contains the header file to the -I options and define the macro as -DUART_CONFIG_H=<uart_config.h>, but that's not really conforming to the rules.

Title: Re: How to #define a header file name in Compiler settings?
Post by: hanshuckebein on December 13, 2020, 07:12:27 pm
I found the solution:

     -DUART_CONFIG_H=\\"uart_config.h\\"
Title: Re: How to #define a header file name in Compiler settings?
Post by: oBFusCATed on December 13, 2020, 07:21:20 pm
We need a ESCAPE_QUOTES macro...