My Header file test.h contains two variables that are deined as:
#ifndef TEST_H_
#define TEST_H_
#define APPS 6;
#define NODES 1;
#endif;
I use it another header file called test2.h as follows:
#include"test.h"
typedef struct {
uint8 State[APPS];
} AppState;
But I get an error in the test.h file saying
expected ']' before ';' token
Since there are no brackets I have no idea why I get this strange error. Could somebody please point out my mistake.Thank you
Do not define constants with ; at the end:
#define APPS 6
#define NODES 1
Otherwise this line:
uint8 State[APPS];
Become this:
uint8 State[6;];
With is obviously not compilying. You may think about #define as a "search and replace".
Related
I keep running into an issue while trying to include an enumeration from one header file in antoher.
The environment I am working in is embedded C using IAR Embedded Workbench.
I have a header file for dedicated enumerated types named "enums.h"
#ifndef ENUMS_H_
#define ENUMS_H_
typedef enum
{
SET,
SCHEDULE,
EXECUTE
}action_type_t;
#endif
and a header file for a parser named "parser.h"
#ifndef PARSER_H_
#define PARSER_H_
#include "enums.h"
#include <stdint.h>
typedef struct
{
action_type_t action;
uint16_t nbytes;
}Message;
#endif
In parser.c I include the header as
#include "parser.h"
When I compile this, I get the error "identifier action_type_t is undefined"
What am I doing wrong here? I am stumped at this point.
Thank you
Your enum definition is missing commas, your parser.h uses uint16_t while having failed to include <stdint.h> and, to be extra pedantic, your include guard macro is encroaching on the reserved namespace because it starts with _ and a capital letter.
This should work:
enums.h:
#ifndef ENUMS_H_
#define ENUMS_H_
typedef enum
{
SET,
SCHEDULE,
EXECUTE, /*the last comma is optional*/
}action_type_t;
#endif
parser.h:
#ifndef PARSER_H_
#define PARSER_H_
#include "enums.h"
#include <stdint.h>
typedef struct
{
action_type_t action;
uint16_t nbytes;
}Message;
#endif
Thank you to all who answered, I figured I would come back and close this one.
It turns out I had an identically named, but empty header file included in my project...
Next time i'll be better about looking in my own backyard first before asking others.
However PSkocik did provide a working example, and his code compiles perfectly for anyone who stumbles into this thread!
This question already has answers here:
What exactly do C include guards do?
(3 answers)
Why are #ifndef and #define used in C++ header files?
(5 answers)
Closed 3 years ago.
I am new in embedded code and I'm reading an example code of NXP, this example is written for FRDM-KL25Z. And in file main.h, I do not know the line:
#ifndef MAIN_H_
#define MAIN_H_
#endif /* MAIN_H_ */
is using for what? I think maybe it defines the name for main.h is MAIN_H_ ? But the purpose of this define is what? And in file main.c, it still include main.h as below:
#include "main.h"
Let's imagine I have a header file like so:
// foo.h
struct Foo
{
};
And then I accidentally include it twice:
#include "foo.h"
#include "foo.h"
This would end up attempting to compile the following, which would generate an error...
struct Foo
{
};
struct Foo //< error 'Foo' declared twice
{
};
One way to fix this is to get the pre-processor to remove the second occurance, and to do this we define a unique identifer per header file. e.g.
#ifndef FOO_H_
#define FOO_H_
struct Foo
{
};
#endif
And now if we accidentally include it twice...
#ifndef FOO_H_ //< not yet declared
#define FOO_H_ //< so declare it
struct Foo
{
};
#endif
#ifndef FOO_H_ //< this time FOO_H is defined...
#define FOO_H_ //< ... so DO NOT include this code.
struct Foo
{
};
#endif
Personally though I'd recommend achieving this same thing via the slighty non-standard (although supported by most, if not all, compilers).
#pragma once //< only ever include this file once
struct Foo
{
};
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.
So I have a header file let's say "header.h" which is protected as follows:
#ifndef __HEADER1_H
#define __HEADER1_H
//type and function def
typedef struct
{
float r; //process noise
float k; //process gain
}state_t;
int get_state(state_t* state, float b);
#endif
Now I have two other headers which I defined as follows:
#ifdef __HEADER2_H
#include "header.h"
//function def
#endif
Second header:
#ifdef __HEADER3_H
//function
//the reason it is done this way is for cnditional compiling such that if the caller
//defines __HEADER3_H t this file won't be included.
#include "header.h"
#endif
Now as I suspected the compiler complained that types and functions defined in header.h were not detected in the source implementation of header2 and header3. So I included header.h in the source files as well. Now the linker is complaining functions that are defined in header.h are multiply defined.
My understanding was since the header.h is protected by ifndef it will only be included once so I don't see the problem.
here is the error that I am getting:
Symbol get_state multiply defined(by kalman.o and dsp.o)
Is there any chance that I am doing something unusally wrong?
#ifndef __HEADER1_H
#define __HEADER_H
The problem is your guard (__HEADER_H) is different from what you are checking for (__HEADER1_H). Make these both the same value.
The typical "guard" for a header file is:
myheader.h:
#ifndef _MYHEADER
#define _MYHEADER
<do stuff>
#endif
Optionally where myheader.h is included, you can do:
#ifndef _MYHEADER
#include "myheader.h"
#endif
This is optional and basically is only to improve compile performance.
I'm trying to engage in some C-preprocessor-only templating efforts in order to type-specialize some code. I've tried to boil it down a bit, so this example seems trivial and pointless, but the real challenge is getting the "include" blocking.
Say I have a "template" file, that gets #included from other source files that define T_ELEMENT_TYPE before including the template.
// Template file...
#ifndef T_ELEMENT_TYPE
#error #define T_ELEMENT_TYPE
#endif
#define PASTER(x,y) x ## y
#define EVALUATOR(x,y) PASTER(x,y)
#define SYMBOLNAME EVALUATOR(SymbolFor, T_ELEMENT_TYPE)
#ifndef SYMBOLNAMEISDEFINED
#define SYMBOLNAMEISDEFINED EVALUTOR(DEFINEDFOR, T_ELEMENT_TYPE)
int SYMBOLNAME(T_ELEMENT_TYPE arg)
{
// do something with arg
return 0;
}
#endif // Guard #ifdef
Then I want to include that template from multiple instantiation sites, but I only want the templated function to be generated ONCE per unique T_ELEMENT_TYPE (so as not to create duplicate symbols.) Like, say this:
// Template-using file...
#define T_ELEMENT_TYPE int
#include "Template.c"
#undef T_ELEMENT_TYPE
#define T_ELEMENT_TYPE float
#include "Template.c"
#undef T_ELEMENT_TYPE
#define T_ELEMENT_TYPE int
#include "Template.c"
#undef T_ELEMENT_TYPE
int someOtherFunc()
{
int foo = 42;
foo = SymbolForint(foo);
float bar = 42.0;
bar = SymbolForfloat(bar);
return foo;
}
So I'm looking for something I can use in the template code. I imagined it might look something like this (although this does not work):
// Template file...
#ifndef T_ELEMENT_TYPE
#error #define T_ELEMENT_TYPE
#endif
#define PASTER(x,y) x ## y
#define EVALUATOR(x,y) PASTER(x,y)
#define SYMBOLNAME EVALUATOR(SymbolFor, T_ELEMENT_TYPE)
#ifndef SYMBOLNAMEISDEFINED
#define SYMBOLNAMEISDEFINED EVALUTOR(DEFINEDFOR, T_ELEMENT_TYPE)
int SYMBOLNAME(T_ELEMENT_TYPE arg)
{
// do something with arg
return 0;
}
#endif // Guard #ifdef
This particular incantation blocks ALL multiple instantiations of the template, not just for different values of T_ELEMENT_TYPE.
Is there a trick I can use to get this effect? Or am I just off the C-Preprocessor reservation, so to speak?
I think you're off the reservation. The first "argument" to #define, the macro name, isn't subject to macro-expansion. So I don't think the preprocessor can define a different symbol according to the value of T_ELEMENT_TYPE. Neither can the preprocessor construct a "list" of already-seen types and check for existence in that.
So I think the include-guard will have to be outside the file:
#ifndef included_mytemplatefile_h_int
#undef T_ELEMENT_TYPE
#define T_ELEMENT_TYPE int
#include "mytemplatefile.h"
#define included_mytemplatefile_h_int
#endif
Alternatively, if your template file header only declares the function SymbolFor_int, instead of defining it, then multiple inclusion isn't harmful. You could have a normal include guard around the parts of the file that don't depend on the current value of T_ELEMENT_TYPE, including the definitions of PASTER, EVALUATOR, SYMBOLNAME. You'd need a separate template file containing definitions, which the program (rather than each translation unit) needs to have exactly once:
template_%.c :
echo "#define T_ELEMENT_TYPE $*" > $#
echo "#include \"mytemplatedefinitions.c\"" >> $#
Then add template_int.o to the list of files linked into your program.