This question already has answers here:
How do I use extern to share variables between source files?
(19 answers)
Closed 2 years ago.
I've been trying to program an atmega328p in Atmel Studio (it's fun), but when I tried to separate my keypad code into it's own c file, I got an error.
Error 'keypad' undeclared (first use in this function) invisible_alarm C:\Users\kenkr\Documents\Atmel Studio\7.0\invisible_alarm\invisible_alarm\main.c 39
keypad is defined in a separate file, and I've ruled a few things out:
I didn't forget the h file (Using a symbol defined in keypad.h works)
The compiler isn't ignoring keypad.c (Errors in keypad.c appear in the compile output)
I didn't forget to add the .c/.h file to the project (They appear in the solution explorer)
I suspect the build order is messed up, but I'm not sure where build order is set in Atmel Studio, but that's just a guess. I put a sample of my files below. The only errors are from main.c referencing keypad.c.
// main.c
#define F_CPU 16000000
#define NULL 0
#include <util/delay.h>
#include <avr/io.h>
#include "lcd.h"
#include "keypad.h"
void example() {
int header_example = KEY_1; // From keypad.h, no error
uint16_t c_example = keypad; // from keypad.c, error
}
// ...
// keypad.h
#ifndef _KEYPAD_H_
#define _KEYPAD_H_
#define KEY_1 0
#define KEY_4 1
// ...
#endif
// keypad.c
#include <avr/io.h>
#include <util/delay.h>
#include "keypad.h"
#define NULL 0
uint16_t keypad = 0x0000;
// ...
You need to declare the variable in the header file:
// keypad.h
#ifndef _KEYPAD_H_
#define _KEYPAD_H_
#include <stdint.h> // commonly needed for uint16_t
#define KEY_1 0
#define KEY_4 1
// ...
extern uint16_t keypad;
#endif
Related
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;
...
I'm trying to write definitions for AVR C code so that i can access pins by simmple macro like
STATUS_LED_OUT =1;
in GENET_HW_DEF.h file, included to main C file. You can reproduce this bug by including this file into any C project.
I'm using avr studio 6.2 and 7 - both give the same result. I cannot compile getting werid macro unfold message like below. (CPU ATMega1284p)
D:\_SVN\Compass\AVR\Compass_IO_proto\Compass IO_proto\GENET_HW_DEF.h(19,49): error: expected ')' before '&' token
#define REGISTER_BIT(rg,bt) ((volatile _io_reg*)&rg)->bit##bt
^
D:\_SVN\Compass\AVR\Compass_IO_proto\Compass IO_proto\GENET_HW_DEF.h(42,25): info: in expansion of macro 'REGISTER_BIT'
#define STATUS_LED_OUT REGISTER_BIT(PORTB,7)
^
D:\_SVN\Compass\AVR\Compass_IO_proto\Compass IO_proto\GENET_HW_DEF.h(46,2): info: in expansion of macro 'STATUS_LED_OUT'
STATUS_LED_OUT=1;
^
Interesting enough, copied to fresh project with just only one or two declarations compiles fine, until one makes any changes in the declarations - like adding another macro reference. Then it becomes stuck again.
Also - if i comment all macro usages like
STATUS_LED_DIR=1;
STATUS_LED_OUT=1;
then I'm able to compile project and then after uncommenting usage lines it still compiles fine.. untill clean is executed. I'm probably messing with macro quirks but I have no idea where.
typedef struct
{
unsigned int bit0:1;
unsigned int bit1:1;
unsigned int bit2:1;
unsigned int bit3:1;
unsigned int bit4:1;
unsigned int bit5:1;
unsigned int bit6:1;
unsigned int bit7:1;
} _io_reg;
#define REGISTER_BIT(rg,bt) ((volatile _io_reg*)&rg)->bit##bt
#ifndef GENET_HW_DEF
#define GENET_HW_DEF
// define functionalities and flags - comment/uncomment apropriate lines
#define GENET_USART_0 256
#define GENET_USART_1 256
#define F_CPU 20000000UL
#define STATUS_LED 7
#define ERROR_LED 4
#define ADC_GLOBAL_ENABLE A
#define ADC_CHANNEL_0 0
#define ADC_CHANNEL_1 4
#define ADC_CHANNEL_2 2
#define ADC_CHANNEL_3 3
#define ADC_CHANNEL_4 1
#define ADC_CHANNEL_5 5
#define ADC_CHANNEL_6 6
#define ADC_CHANNEL_7 7
// actual definitions and initialization
#ifdef STATUS_LED
#define STATUS_LED_OUT REGISTER_BIT(PORTB,STATUS_LED)
#define STATUS_LED_DIR REGISTER_BIT(DDRB,STATUS_LED)
#define STATUS_LED_PIN REGISTER_BIT(PINB,STATUS_LED)
STATUS_LED_DIR=1;
STATUS_LED_OUT=1;
#endif
#ifdef ERROR_LED
#define ERROR_LED_OUT REGISTER_BIT(PORTC,ERROR_LED)
#define ERROR_LED_DIR REGISTER_BIT(DDRC,ERROR_LED)
ERROR_LED_DIR=1;
ERROR_LED_OUT=1;
#endif
#ifdef GENET_USART_0
#define USART0_ENABLED
#define UART_RX0_BUFFER_SIZE GENET_USART_0
#define UART_TX0_BUFFER_SIZE GENET_USART_0
#endif
#ifdef GENET_USART_1
#define USART1_ENABLED
#define UART_RX1_BUFFER_SIZE GENET_USART_1
#define UART_TX1_BUFFER_SIZE GENET_USART_1
#endif
#endif
I reproduced your problem.
You cannot call STATUS_LED_DIR=1; outside code execution flow. This must be inside a function (for example main()).
Now you will end with other compilation errors but this was the main mistake.
Second correction, you need 2 level for concatenation
#define CONCAT(bt) bit##bt
#define REGISTER_BIT(rg,bt) ((volatile _io_reg*)&rg)->CONCAT(bt)
I have the following source named lcd.c.
#include <stdio.h>
#include "lcd.h"
void print_mode(void)
{
printf("%d\n",LCD_MODE);
}
The header lcd.h contains the definition for LCD_MODE as follows.
#ifndef LCD_H
#define LCD_H
#include "util.h"
#ifndef LCD_MODE
#define LCD_MODE LCD_MODE_8BIT
#endif
void print_mode(void);
#endif /* LCD_H */
The file util.h contains
#ifndef UTIL_H
#define UTIL_H
#define LCD_MODE_8BIT 1
#define LCD_MODE_4BIT 0
#endif /* UTIL_H */
lcd.c will be compiled separately as part of some library. I want to use it with an application main.c as follows.
#include "util.h"
#define LCD_MODE LCD_MODE_4BIT
#include "lcd.h"
int main(void)
{
print_mode();
return 0;
}
The desired outcome is to print 0 as per the definition of LCD_MODE_4BIT in main.c. However, 1 is printed because the header file sees that LCD_MODE is not defined during the preprocessing for lcd.c. How should I go about passing the LCD_MODE option to print_mode() through the main application?
if you cannot recompile lcd.c you cannot use a macro in another source file, because lcd.o already has the value hardcoded.
You could create a static variable (which defaults to LCD_MODE) that you can change using a setter:
#include <stdio.h>
#include "lcd.h"
static int the_mode = LCD_MODE;
void print_mode(void)
{
printf("%d\n",the_mode);
}
void set_mode(int new_mode)
{
the_mode = new_mode;
}
lcd.h should contain the prototype for the new configuration function BTW:
void set_mode(int new_mode);
then in your main, you can:
set_mode(LCD_MODE);
(or drop that LCD_MODE macro everywhere because it solves nothing and adds to the confusion)
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.
I have a weird problem with my C-Code that I don't really understand.
I have two header files os_memory.h and os_mem_drivers.h.
os_memory.h
#ifndef OS_MEMORY_H_
#define OS_MEMORY_H_
#include "lcd.h"
#include "os_mem_drivers.h"
static const MemAddr gui_alloc_table_start = 0x1C8;
#endif /* OS_MEMORY_H_ */
os_mem_drivers.h
#ifndef OS_MEM_DRIVERS_H_
#define OS_MEM_DRIVERS_H_
#include "os_memory.h"
#include "defines.h"
#include "os_core.h"
typedef uint16_t MemAddr;
#endif
If I try to compile this code the compiler gives me the error unknown type name 'MemAddr'. I don't get it because I included the right header files in each .h file so there shouldn't be any error.
Is there anything that I'm missing here?
I'm using AtmelStudio 6.1 and the C language for this project.
You should move the definition for type MemAddr before including "os_memory.h":
os_mem_drivers.h:
#ifndef OS_MEM_DRIVERS_H_
#define OS_MEM_DRIVERS_H_
#include <stdint.h>
typedef uint16_t MemAddr;
#include "os_memory.h"
#include "defines.h"
#include "os_core.h"
#endif
But a more important problem is the circular inclusion of "os_memory.h" and "os_mem_drivers.h". Each one includes the other: include guards prevent recursive inclusion but make it difficult to understand what is really going on. You should try and fix this issue.