Undeclared identifiers in .H files after program tidy-up?

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

Undeclared identifiers in .H files after program tidy-up?

#1 Post by nigelmercier » 03 Jan 2010 18:58

I've started to split my code into .H modules, plus a .C file containing:

#includes to above .H files as "filename.h"
#define macros
variables
function prototypes
functions used directly
main{}

I have also added the .H files to the project.

Now I get a load of Undeclared identifier... in the .H files.

What am I doing wrong?

PeterA
Posts: 127
Joined: 02 Jul 2008 11:01

#2 Post by PeterA » 03 Jan 2010 19:58

C file include in your project if not you get Undeclared identifier..
Project->Add File To Project

Code: Select all

#include "HW_Init.h"
//---------------------------------------------------------------------------
unsigned int var1;
//---------------------------------------------------------------------------
void Init_HW() {

  T0CON.TMR0ON= 1;     // Enable Timer0
  T0CON.T08BIT= 0;     // 0 = Timer0 is configured as a 16-bit timer/counter
  T0CON.T0CS= 0;       // 0 = Internal instruction cycle clock (CLKO)
  T0CON.PSA= 0;        // 0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.
  T0CON.T0PS0= 0;      // Prerscaler
  T0CON.T0PS1= 1;      // Prerscaler
  T0CON.T0PS2= 0;      // Prerscaler

  PORTA= 0;            // Clear PORTA
  TRISA= 0;            // Designate PORTA pins as output
  PORTB= 0;            // Clear PORTB
  TRISB= 0x00;         // Designate PORTB pins as output
  PORTC= 0;            // Clear PORTC
  TRISC= 0x83;         // Designate PORTC pins
  PORTD= 0;            // Clear PORTD
  TRISD= 0;            // Designate PORTD pins as input

  PORTG= 0;            // Clear PORTG
  TRISG= 0xf4;         // Designate PORTG pins

  INTCON.GIE= 1;       // Global interrupt enabel
  INTCON.PEIE= 1;      // Peripheral interrupt enable
  INTCON.TMR0IE= 1;    // Enable Timer0 interrupt

}
//--------------------------------------
Header file include using #include "name.h"

Code: Select all

// HW_Init.h

#ifndef __HW_Init_H__        // use this to prevent same header to be included more than once  
#define __HW_Init_H__

//---------------------------------------------------------------------------
extern void Init_HW();   // this function is accessible where #include "name.h is used
//---------------------------------------------------------------------------
extern unsigned int var1;     // this variable is accessible where #include "name.h is used   
//---------------------------------------------------------------------------
#endif

nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

#3 Post by nigelmercier » 03 Jan 2010 20:22

PeterA wrote:C file include in your project if not you get Undeclared identifier..
Project->Add File To Project
Like I said:

#includes to above .H files as "filename.h"
...
I have also added the .H files to the project.


Is this what you mean Peter?

PeterA
Posts: 127
Joined: 02 Jul 2008 11:01

#4 Post by PeterA » 03 Jan 2010 20:55

No
Look in the project manager an verify that you have the c file there.
Check the syntax in your header files and that you included the header in your c file

nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

#5 Post by nigelmercier » 04 Jan 2010 11:22

PeterA wrote:No
Look in the project manager an verify that you have the c file there.
Check the syntax in your header files and that you included the header in your c file
I think I have done this OK, but then I am a novice!

I don't really understand the differences between .C and .H files. Perhaps I should be splitting my code into a number of .C files - is this acceptable?

PeterA
Posts: 127
Joined: 02 Jul 2008 11:01

#6 Post by PeterA » 04 Jan 2010 12:50

Yes try to get specific function code in each one like,

Lets say that we do a program that will listen to a keyboard and send keystrokes using a uart.

If you do this program in C it's usually easy to put all code regarding a uart in one file and all code for the keyboard in another and then you connect the to together in main.c.

The MyUart.c will contain some public functions "void SendString(char *byte,int len)" and "void InitUart()" there might also be private functions like "void sendbyte(char byte)" and a public variable "short TxEmpty".
It's always nice to make it easy to change some parameters like baud rate so lets make it a definition in the header.

the header file for this will look like this

Code: Select all


#ifndef c_uart.h
#define c_uart.h

#define BAUD_RATE 9600 

extern short TxEmpty;

extern void SendString(char *byte,int len);
extern void InitUart();   

#endif
  


And the c file will contain all the code for the uart

Code: Select all


#include "MyUart.h"

short TxEmpty;

void sendbyte(char byte)
{
  UartSend(byte);
}

void SendString(char *byte,int len)
{
  unsined int i;

  TxEmpty= 0;
  for( i=0; i<=len; i++)
    sendbyte(byte[i]);

  TxEmpty= 1;
} 

void InitUart()
{
  InitUart( BAUD_RATE);
} 

then you do the same for the keyboard, one H file containing the public functions and defs and a C file containing the code.
In main you connect the functions together and make your program.
If you manage to divide functionality like this it's easy to reuse the code next time.

Hope it helps

nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

#7 Post by nigelmercier » 04 Jan 2010 15:56

PeterA wrote:then you do the same for the keyboard, one H file containing the public functions and defs and a C file containing the code.
I think I understand now: the .H files are only for "public functions and defs", and the modules go in .C files?

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#8 Post by anikolic » 06 Jan 2010 13:43

I think I understand now: the .H files are only for "public functions and defs", and the modules go in .C files?
Yes, exactly!
Web Department Manager

nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

#9 Post by nigelmercier » 08 Jan 2010 12:53

aleksandar.nikolic wrote:
I think I understand now: the .H files are only for "public functions and defs", and the modules go in .C files?
Yes, exactly!
Right, I've done my best to sort this out, now I need an expert! For example, in one of the .C files I have a function clsLCD(). This is defined and declared as a prototype in the associated .C file, plus declared as a prototype in my definitions.h file. However, when I build the project I get Undeclared identiifier 'clsLCD' in expression relating to another .C file that calls the function.


Could someone have a look at these files and explain how to get rid of these errors.

TIA

Nigel

nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

#10 Post by nigelmercier » 13 Jan 2010 15:27

nigelmercier wrote:
Could someone have a look at these files and explain how to get rid of these errors.

TIA

Nigel
Pretty please?

nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

#11 Post by nigelmercier » 22 Jan 2010 14:03

nigelmercier wrote:

Could someone have a look at these files and explain how to get rid of these errors.
All sorted now, files removed.

Post Reply

Return to “mikroC PRO for PIC General”