#Preprocessor Timings

23 messages · Page 1 of 1 (latest)

worn fossil
#

If a CPP source file is including multiple .h - will the order will be to include first .h do its text substitutions and then include 2nd?

I'm getting error

g++ main.cpp -o production.out
In file included from map.h:11,
                 from main.cpp:1:
player.h: In function ‘int read_input(player*)’:
player.h:59:46: error: ‘WIDTH’ was not declared in this scope
   59 |         if (key=='d' && player_->position[X]<WIDTH-1) {
      |                                              ^~~~~
player.h:59:46: note: the macro ‘WIDTH’ had not yet been defined
In file included from main.cpp:1:
map.h:15: note: it was later defined here
   15 | #define WIDTH 5
      | 
In file included from map.h:11,
                 from main.cpp:1:
player.h:68:53: error: ‘HEIGHT’ was not declared in this scope
   68 |         else if (key == 's' && player_->position[Y]<HEIGHT-1) {
      |                                                     ^~~~~~
player.h:68:53: note: the macro ‘HEIGHT’ had not yet been defined
In file included from main.cpp:1:
map.h:16: note: it was later defined here
   16 | #define HEIGHT 5
      | 
make: *** [makefile:6: production] Error 1```


main.cpp is including first map.h then player.h

player.h
```cpp
#ifndef PLAYER_H
#define PLAYER_H

//#define DEBUG - Use compiler flag -D instead

#include <cstdint>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

#include "map.h"
#include "constants.h"


/* Player Position */
#define X 0
#define Y 1

struct player {
    std::uint16_t position[2];
    int symbol;
};

...

#endif /*PLAYER_H*/

map.h

#ifndef MAP_H
#define MAP_H

#define clear_screen() write(STDOUT_FILENO, "\033[2J\033[H", 7);

#include <iostream>
#include <cstdint>
#include <unistd.h>

#include "debug.h"
#include "player.h"

/* MAP DIMENSIONS */
#define WIDTH 5
#define HEIGHT 5

/* BLOCKS */
#define WALL 0
#define WALKABLE 1
#define PLAYER 2

struct map {
    std::int16_t data[HEIGHT][WIDTH] = {0};
};

...

#endif /*MAP_H*/
balmy falconBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hollow turtle
#

You could move those defines to another header. And im not sure if those circular includes are recommend.

worn fossil
hollow turtle
#

Can you send the whole code?

worn fossil
hollow turtle
#

There isnt a player.cpp or map.cpp?

worn fossil
#

it's a header only library

#

I will eventually split it but for learning the pitfalls I've merged it temporarily

hollow turtle
#

You also could use constexpr instead of defines. For learning purpose it is fine

#

What error messages you are getting after changing?

#

Maybe you miss the inline keyword? I only did header only libary with classes. There the keyword can be omitted.

worn fossil
#
g++ main.cpp -o production.out
In file included from map.h:11,
                 from main.cpp:1:
player.h: In function ‘int read_input(player*)’:
player.h:59:46: error: ‘WIDTH’ was not declared in this scope
   59 |         if (key=='d' && player_->position[X]<WIDTH-1) {
      |                                              ^~~~~
player.h:59:46: note: the macro ‘WIDTH’ had not yet been defined
In file included from main.cpp:1:
map.h:15: note: it was later defined here
   15 | #define WIDTH 5
      | 
In file included from map.h:11,
                 from main.cpp:1:
player.h:68:53: error: ‘HEIGHT’ was not declared in this scope
   68 |         else if (key == 's' && player_->position[Y]<HEIGHT-1) {
      |                                                     ^~~~~~
player.h:68:53: note: the macro ‘HEIGHT’ had not yet been defined
In file included from main.cpp:1:
map.h:16: note: it was later defined here
   16 | #define HEIGHT 5
      | 
make: *** [makefile:6: production] Error 1

#

Unfortunately I'm not aware of the 2 keywrods that you mentioned

#

I know that separating the defines in a different header works - but why?

hollow turtle
#

This circular includes can be tricky sometimes. I guess it was defined multiple times. Or some other cause. You got include guards so maybe it wasnt defined multiple times. I dont know.

worn fossil
#

alrigthy - no worries - thanks for the help

worn fossil
#

I will look into this

#

!solved