#define scope (microchip studio) - c-preprocessor

Im using microchip studio
How can I define F_CPU in main.c file and make all files "see" this definition?
I declared in main.c:
#include <avr/io.h>
#include "UART.h"
#define F_CPU 12000000
and in UART.c I tried using this definition :
ubrr_value = (F_CPU/(2*BAUD_RATE))-1;
i recieved this error:
Severity Code Description Project File Line
Error 'F_CPU' undeclared (first use in this function) UART_TEST XX\UART_TEST\UART_TEST\UART.c 55

How can I define F_CPU in main.c file and make all files "see" this definition?
You can't.
You can define F_CPU in your compiler command line arguments. For example gcc compiler takes -DF_CPU=12000000. And compile all your sources with that definition.
You can refactor your code, and put F_CPU into separate file and include it from all files that need it.
// config.h
#deifne F_CPU 12000000
// main.c
#include "config.h"
// uart.c
#include "config.h"
...
ubrr_value = (F_CPU/(2*BAUD_RATE))-1;
...

Related

How to use the pre-processor directive #include in a multi-file arduino c project?

I've wrote some code but when it's in a single file I find the code overwhelming and so I've tried to seperate the code into 3 files. The first being the main section (SACode.c), the second for making a char/string dictionary (CharCharStarArray.h) and the last for storing constants (constants.h). The SACode needs to be able to access CharCharStarArray.h and constants.h. and so I've used the following code in SAcode.c
/* File CharCharStarArray.h*/
#ifndef CharCharStarArray.h
#include "CharCharStarArray.h"
/* File constants.h*/
#ifndef constants.h
#include "constants.h"
//more code
#endif
#endif
The header file CharCharStarArray.h also needs to access the constants.h file and so in the CharCharStarArray.h file I've included this snippet
#ifndef constants.h
#include "constants.h"
//more code
#endif
When compiled I get "redefinition" or "previously defined here" errors from the constants.h file
I've commented out the include constants code in the SAcode file and it compiles but this seems like bad practice. If I didn't include the CharCharStarArray.h file then SACode.c wouldn't be able to access the constants file
Here's a recreation of the problem
headefiles
#ifndef header1.h
#include "header1.h"
#ifndef header2.h
#include "header2.h"
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
/*header2*/
#endif
/*header1*/
#endif
header1.h
#ifndef header2.h
#include "header2.h"
int header1var = 5;
#endif
header2.h
int header2var = 3;

Redefined symbol in multiple c code with #ifndef directive

I have a stupid problem and I don't see where it comes from. I took care of using #ifndef directive to make sure all my #include are not redefined. Sadly for three of them that's happening. Here my multiple files arch :
t_include.h
#ifndef T_INCLUDE_H_
#define T_INCLUDE_H_
/* Project specific dependencies*/
#include "utilities.h"
#include "fsp_function.h"
#include "ti/csl/csl_tsc.h"
#include "ti/csl/csl_cache.h"
#include "ti/csl/csl_cacheAux.h"
#include "ti_sp_complex_convolution_A_input1.h"
#include "ti_sp_complex_convolution_A_input2.h"
#include "to_sp_complex_convolution_A_output.h"
#endif /* T_INCLUDE_H_ */
t_function.h
#ifndef T_FUNCTION_H_
#define T_FUNCTION_H_
#include "t_include.h"
/*output vector*/
#define INPUT1A_LENGTH 5000
#define INPUT2A_LENGTH 2800
#define OUTPUTA_LENGTH 2202
extern FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
/*misc parameter*/
#define CPU_CLOCK_KHZ 1400000
#define CPU_CLOCK_MS 1/CPU_CLOCK_KHZ
#define FIR_NB_MACS INPUT1A_LENGTH * OUTPUTA_LENGTH /* FIR algorithm complexity */
#define NB_OF_REP 10
#define UMA_L2CACHE_L1DCACHE 0
/* Project specific types */
typedef struct{
ect...
And now c file only include t_function.h :
t_function.c
/* Dependencies */
#include "t_function.h"
FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
/* API */
etc...
And t_main_function.c
/* dependencies */
#include "t_function.h"
void main(void) {
etc...
It should work but during linking here the errors comming :
<Linking>
error #10056: symbol "sp_complex_convolution_A_output" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
error #10056: symbol "sp_complex_convolution_A_input2" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
error #10056: symbol "sp_complex_convolution_A_input1" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
error #10056: symbol "sp_complex_convolution_A_output_thales" redefined: first defined in "./TEST/t_function.obj"; redefined in "./TEST/t_main_function.obj"
>> Compilation failure
error #10010: errors encountered during linking; "CONVOLUTION_COMPLEX.out" not built
So the error only com from three symbol sp_complex_convolution_A_output, sp_complex_convolution_A_input1 and sp_complex_convolution_A_input2 Which are defined in their own .h which is also protected by #ifndef directives:
ti_sp_complex_convolution_A_input1.h
#ifndef __TI_SP_COMPLEX_CONVOLUTION_A_INPUT1_H_
#define __TI_SP_COMPLEX_CONVOLUTION_A_INPUT1_H_
FLOAT32 sp_complex_convolution_A_input1[2 * 2500] = {
etc...
And the same for the other two...
So I really don't know why it is happening.
Thx for helping
Definitions like:
FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
should go into a source file.
The header files should contain only declarations like:
extern FLOAT32 sp_complex_convolution_A_output_thales[OUTPUTA_LENGTH];
As a rule of thumb, do no put anything that allocates memory into header files.

C preprocessor #error in header file included in multiple source files

I have two source files, main.c and datamgr.c - and two header files, config.h and datamgr.h
The testing system we're using expects these files, and only these files.
main.c:
#include "datamgr.h"
#include "config.h"
int main() {
custom_type a = 1;
a = foo();
return 0;
}
datamgr.c:
#include "datamgr.h"
#include "config.h"
custom_type foo() {
custom_type a = 1;
return a;
}
datamgr.h:
#ifndef DATAMGR_H
#define DATAMGR_H
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
custom_type foo();
#endif
config.h:
#ifndef CONFIG_H
#define CONFIG_H
#ifndef SET_MAX_TEMP
#error "Max temperature not set."
#endif
#ifndef SET_MIN_TEMP
#error "Max temperature not set."
#endif
typedef custom_type uint16_t
#endif
Now, the problem is that I can only define SET_MAX_TEMP and SET_MIN_TEMP in main.c, but both main.c and datamgr.c need both the header files. So if I leave them undefined in datamgr.c I get a compiler error. However, if I do define them in datamgr.c and later overwrite them in main.c, I get a different compiler error.
Please, any assistance as to how to get this horrible setup to work would be greatly appreciated.
You can pass these defines directly while compiling:
gcc -DSET_MAX_TEMP -DSET_MIN_TEMP <your files>
In datamgr.c do:
#define SET_MAX_TEMP
#define SET_MIN_TEMP
#include "datamgr.h"
#include "config.h"
#undef SET_MAX_TEMP
#undef SET_MIN_TEMP
In a comment, you said:
Because main.c is the file that our testing system uses to implement the test scenarios.
In that case, make sure that the testing system defines those macros in the command line of the compiler for every file being compiled.

An interesting thing about overriding linux kernel macro

In the following code, it is compiled success and print 1024
#include <stdio.h>
#define FD_SETSIZE 512
#include <sys/types.h>
int main()
{
printf("%d\n", FD_SETSIZE);
}
But in the following code, it is compiled failed and print
test.c:4:1: warning: "FD_SETSIZE" redefined
In file included from /usr/include/sys/types.h:220,
from test_fd.c:3:
/usr/include/sys/select.h:81:1: warning: this is the location of the previous definition
the code is
#include <stdio.h>
#include <sys/types.h>
#define FD_SETSIZE 512
int main()
{
printf("%d\n", FD_SETSIZE);
}
Can anbody explain this? Thanks!
But in the following code, it is compiled failed and print
In the question, both the programs were compiled, but while compiling first program you got warnings in preprocessor stage.
Preprocessor stage is responsible for the replacement of macros.
In this example the preprocessor is using the last defined macro and replacing it.
#include <stdio.h>
#define FD_SETSIZE 512
#include <sys/types.h>
Here the definition of FD_SETSIZE is there in both the .c file and also in header file sys/types.h.
After the file inclusion, then the replacing of macros will be done,so the latest defined macro is replaced.
So the final replacement FD_SETSIZE of will be same as defined in the sys/types.h file and vice-versa.
Hope this is helpful.
you can use the #undef directive to remove the defined macro and replace it later
#ifdef MACRO
#undef MACRO
#endif
#define MACRO

C #include sth dependency confused

I have the following code in main.c
#include "config.h"
#include "util.h"
and in config.h
#include "util.h"
and there is a normal function in util.h
as I have include the util.h twice, I expect it should be wrong
but unfortunately, it goes damn well when I type: gcc main.c config.c util.c
why is this right?
it should not cause an error
if you have next files:
inc.h, inc1.h and inc.h includes inc1.h and inc1.h includes inc.h, then it cause of error
in the header files made following agreement:
#ifndef HEADER_NAME_H
#defiene HEADER_NAME_H
//body of header file
#endif /*HEADER_NAME_H*/
where HEADER_NAME_H is "header_name.h" filename
C headers usually have header guards that prevent them to be included multiple times. For example:
#include <stdio.h>
#include <stdio.h> // multiple inclusion of stdio.h, that's fine
A header guard is placed at the top of the header:
#ifndef MYHEADER_H
#define MYHEADER_H
// content of the header file
#endif MYHEADER_H

Resources