Author Topic: How to #define a header file name in Compiler settings?  (Read 2020 times)

Offline hanshuckebein

  • Multiple posting newcomer
  • *
  • Posts: 36
How to #define a header file name in Compiler settings?
« 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?
« Last Edit: December 13, 2020, 12:37:33 am by hanshuckebein »

Offline zainka

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: How to #define a header file name in Compiler settings?
« Reply #1 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

Offline hanshuckebein

  • Multiple posting newcomer
  • *
  • Posts: 36
Re: How to #define a header file name in Compiler settings?
« Reply #2 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

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7178
    • My Best Post
Re: How to #define a header file name in Compiler settings?
« Reply #3 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.

« Last Edit: December 13, 2020, 01:46:11 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7178
    • My Best Post
Re: How to #define a header file name in Compiler settings?
« Reply #4 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.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7178
    • My Best Post
Re: How to #define a header file name in Compiler settings?
« Reply #5 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;
}
« Last Edit: December 13, 2020, 02:41:48 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13438
    • Travis build status
Re: How to #define a header file name in Compiler settings?
« Reply #6 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.
(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!]

Offline hanshuckebein

  • Multiple posting newcomer
  • *
  • Posts: 36
Re: How to #define a header file name in Compiler settings?
« Reply #7 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.


Offline hanshuckebein

  • Multiple posting newcomer
  • *
  • Posts: 36
Re: How to #define a header file name in Compiler settings?
« Reply #8 on: December 13, 2020, 07:12:27 pm »
I found the solution:

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

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13438
    • Travis build status
Re: How to #define a header file name in Compiler settings?
« Reply #9 on: December 13, 2020, 07:21:20 pm »
We need a ESCAPE_QUOTES macro...
(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!]