Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

problem with header files and processors (redefined error)

Status
Not open for further replies.

sairfan1

Full Member level 1
Full Member level 1
Joined
Jun 12, 2010
Messages
97
Helped
4
Reputation
8
Reaction score
7
Trophy points
1,288
Location
Regina, Canada
Visit site
Activity points
2,384
Hi,

I'm having problem while working in MikroC, i want to break my code into library, see how can i make following code workable.

Code:
//--------------
//file test.h
//--------------

#ifndef _TEST_H_
 #define _TEST_H_

#define __TEST_DEF 1
unsigned char __chr = 'D';

void set_char(unsigned char c);
unsigned get_char(void);
unsigned do_access();
#endif
//--------------
// file test.c
//--------------

#include "test.h"
#include "access.h"

void set_char(unsigned char c){
  if (__chr == __TEST_DEF)
   __chr = 'T';
  else
   __chr = c;
}
unsigned get_char(void){
 return __chr;
}
unsigned do_access(){
 return acc;
}

//--------------
//file access.h
//--------------

#ifndef _ACCESS_H_
 #define __ACCESS_H_

unsigned char acc = 'S';

void setchar(unsigned char c);
unsigned getchar(void);
unsigned char do_test();

#endif

//--------------
// file access.c
//--------------
// accessing sourcing of other header and c files
// variables, functions and #define macros

#include "test.h"
#include "access.h"

void setchar(unsigned char c){
  if (__chr == __TEST_DEF)
   __chr = 'T';
  else
   __chr = c;
}
unsigned getchar(void){
 return __chr;
}
unsigned char do_test(){
 returne get_char();
}
//--------------
// file main.c
//--------------

#include "test.h"
#include "access.h"

void main(void){
 TRISB = 0;
 PORTB = 0;

 setchar('B');
 PORTB = get_char();
 Delay_ms(1500);

 set_char('A');
 PORTB = getchar();
 Delay_ms(1500);

 PORTB = do_test();
 Delay_ms(1500);

 PORTB = do_access();
 Delay_ms(1500);
 
 while(1);
}
 

in test.h
Code:
unsigned char __chr = 'D';
defines the variable __chr (in effect allocating storage for it)
if you include test.h in more than one .c file your with multiply definition or similar errors

in test.h declare it extern (in effect saying this variable is defined elsewhere in the project)
Code:
extern unsigned char __chr = 'D';
and in test.c define it
Code:
unsigned char __chr = 'D';
you only have one definition in the project and many .c can include the declaration as required
 
still having problem, lets make this example simple for a while. following is the simplified code. Please also advise what is the sequence of compiler to link these files, this could also help to understand. thanks

Code:
//--------------
//file test.h
//--------------

#ifndef _TEST_H_
 #define _TEST_H_

#define __TEST_DEF 1
unsigned char __chr = 'D';

void set_char(unsigned char c);
unsigned get_char(void);

#endif

//--------------
// file test.c
//--------------

#include "test.h"

extern unsigned char __chr = 'D';

void set_char(unsigned char c){
  if (__chr == __TEST_DEF)
   __chr = 'T';
  else
   __chr = c;
}
unsigned get_char(void){
 return __chr;
}

//--------------
// file main.c
//--------------

#ifndef __INCLUDES
 #define __INCLUDES
#include "test.h"
#endif

void main(void){

 TRISB = 0;
 PORTB = 0;


 set_char('A');
 PORTB = get_char();
 Delay_ms(1500);

 while(1);
}
 
Last edited:

put the declaration (using extern) of __chr in the test.h file and the definition (where memory is allocated) in the test.c file, e.g.
Code:
//--------------
//file test.h
//--------------

#ifndef _TEST_H_
 #define _TEST_H_

#define __TEST_DEF 1
extern unsigned char __chr;

void set_char(unsigned char c);
unsigned get_char(void);

#endif

//--------------
// file test.c
//--------------

#include "test.h"

unsigned char __chr = 'D';

void set_char(unsigned char c){
  if (__chr == __TEST_DEF)
   __chr = 'T';
  else
   __chr = c;
}
unsigned get_char(void){
 return __chr;
}

//--------------
// file main.c
//--------------

#ifndef __INCLUDES
 #define __INCLUDES
#include "test.h"
#endif

void main(void){

 TRISB = 0;
 PORTB = 0;


 set_char('A');
 PORTB = get_char();
 Delay_ms(1500);

 while(1);
}
 
Did you check every line for syntax or spelling errors?

there is not spelling mistakes, i guess while posting i made some mistake.

horace1; said:
it looks like you have removed the
#include "test.h"

if add #include "test.h" line to test.c file error message is

redefined __chr
 

if add #include "test.h" line to test.c file error message is

redefined __chr
have you left the initialisation of __chr in the extern declaration in test.h?
Code:
extern unsigned char __chr = 'D';

you should not initialize an extern (some compilers ignore it, others treat it has a definition and give various errors)- it should be a simple declaration
Code:
extern unsigned char __chr;
you should only initialise in a definition - it is initialized in the test.c file in your case
 

thanks, at last i over come the problem, now only two things left.

1) below macro is defined in test.c file and accessible, but it is not accessible from main function.
Code:
#define __TEST_DEF 1


2) i added more code to main.c file to control the buttons, BTN7 is not being accessed by test.c file.

Code:
sbit BTN7 at RB7_bit;
sbit BTN6 at RB6_bit;
sbit BTN5 at RB5_bit;
sbit BTN4 at RB4_bit;
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top