R
R
red_crocodile2021-08-22 19:30:55
Arduino
red_crocodile, 2021-08-22 19:30:55

Scope c Arduino. How to pass define to the library?

Hello! I created the view code

#define MAX_FIELDS 2
#include<library.h>
void setup(){...

Library code:
#pragma once
#include <Arduino.h>
#ifndef MAX_FIELDS
#define MAX_FIELDS 0
#endif
class Library{...


And ifndef always works. Help move MAX_FIELDS inside library.h

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-08-22
@wataru

The problem is that .h files are just inserted into .c files when you do an include.
Those. in the file that you have listed first, which makes define 2 and include - MAX_FIELDS will be 2. And in the library.c file, which also does include library.h - MAX_FIELDS will already be 0.
If MAX_FIELDS affects the declaration of something (for example , the size of an array in some structure), then you will get an error at the linking stage (because different structures will be declared in different compilation objects). Otherwise, assigning MAX_FIELDS 2 in the conditional main.c has no effect.
You need to set MAX_FIELDS in the compilation options of the whole project. This is usually the -D switch, or it can still be set in the Makefile.
Another alternative is to pass a value to your functions instead of the MAX_FIELDS constant if you can.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question