News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

How to #define a header file name in Compiler settings?

Started by hanshuckebein, December 13, 2020, 12:33:02 AM

Previous topic - Next topic

hanshuckebein

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?

zainka

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

hanshuckebein

Sorry, but that is no answer to my question. I want to know, how to do it via Build options | Compiler settings | #defines

stahta01

#3
I think you did the CB part right.

But you might need to change

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

To

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


If the above fails try defining in CB as

UART_CONFIG_H="\"uart_config.h\""

Tim S.

C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

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.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

#5
Found example code that worked for what you wanted.

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

The program code had this below.

#include <stdio.h>

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

#include STRINGIFY(INCLUDE_FILE)

int main()
{
    printf("Hello world!\n");
    return 0;
}
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

oBFusCATed

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.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

hanshuckebein

@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.


hanshuckebein

I found the solution:

     -DUART_CONFIG_H=\\"uart_config.h\\"

oBFusCATed

(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]