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.

syntax error in header file

Status
Not open for further replies.

avov

Newbie level 3
Newbie level 3
Joined
Apr 30, 2013
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,315
Hello,

Can some one explain me how to sort out the fault - LED.h:3:Error: syntax error. I can't see any syntax error in LED.h file:-(
In my LED.h file is the following code:
Code:
extern void InitialiseLED(void);
extern void LED_Off(void);
extern void LED_On(void);

here is LED.c file
Code:
#include <p18cxxx.h>


void InitialiseLED(void)
{
	TRISB = 0xF0; // In binary 0b1111000 four higher bits - input; four lower bits - output
}

void LED_Off(void)
{
	PORTB&=(~0x01); //Turn LED0 off; first bitwise NOT (11111110); second bitwise AND (00000000 AND 11111110); result - 00000000
}

void LED_On(void)
{
	PORTB|=0x01; // Turn LED0 on; bitwise OR need to add (00000000 OR 00000001); result - 00000001
}

And here is main.c file:
Code:
#include <LED.h>

void main(void)
{
	InitialiseLED();
	LED_Off();
	LED_On();
	while(1){};// pause execution here
}

Thank you in advance.
 

Hello!

I'm not an expert in compiler technology, but I have never declared a function as extern.
Beside that, as Armoric said, the #ifndef ----#endif would probably be useful.

Dora.
 

Thanks for yours answers.
If I use #indef --endif:
Code:
#ifdef __18F452
#include <p18cxxx.h>
#endif
void InitialiseLED(void)
{
	TRISB = 0xF0; // In binary 0b1111000 four higher bits - input; four lower bits - output
}

void LED_Off(void)
{
	PORTB&=(~0x01); //Turn LED0 off; first bitwise NOT (11111110); second bitwise AND (00000000 AND 11111110); result - 00000000
}

void LED_On(void)
{
	PORTB|=0x01; // Turn LED0 on; bitwise OR need to add (00000000 OR 00000001); result - 00000001
}

The result is:
:Error[1105] symbol 'TRISB' has not been defined
:Error [1101] lvalue required

Can you please give me example how declare function in header file no as extern?

Thanks in advance.
 

Usually project source files are compiled separately and linked to the final application. Some embedded C compilers are including source files to a single compilation unit, it's e.g. the standard mode of CCS C (it supports multiple compilation units nevertheless).

There's no indication in the above posts that the source file led.c is missing in the project. The reported problems are wrong syntax of function prototype and missing include file with processor specific defines.

There's also no problem of missing #ifdefs around an include file. They are sometimes used to avoid multiple inclusion of the same header. Of course it can only work, if a respective constant like __18F452 is defined inside the header file, which apparently isn't the case here.
 

Hello!

The function definitions are in LED.c file. If LCD.c file is not included then how will the functions work?

In recent years, with all these fully integrated development environments, people tend to forget how multi file
projects work.

Usually (as said above by FvM), a compiler starts compiling all its files separately and make a .o for each .c
All these files need to know is the prototype of the functions they use.
For instance in the above program, there will be a main program defined in, say, main.c.
The compilation directive cc -o main.o -cmain.c only needs the .h files for the function used in main.c.

Then once all the .o are generated, they are linked together (using a linker), and this tells the main program
where exactly it should jump to find the required functions.

Now you can indeed use #include "led.c"

However, if you do this, everytime you compile main.c, you will recompile led.c together.
On a small demo program, you may not notice it. If you are compiling a large program the compilation may
take a vey long time. For instance, if you play with linux on ARM, the first time you configure and compile
the code tree (kernel and apps), it depend very much on the computer, but in my case, with a Core I7 and
plenty of RAM, it takes, say, 15 minutes. Then if you modify a few lines in an application and recompile,
only what you changed will be recompiled and it will take a few seconds at most.

Here is a small example I made.
1. The library file myfunc.h

Code C - [expand]
1
2
3
4
5
6
#ifndef _MY_FUNC_H_
#define _MY_FUNC_H_
 
void myfunc(void);
 
#endif


2. The implementation file myfunc.c

Code C - [expand]
1
2
3
4
5
6
#include <stdio.h>
#include "myfunc.h"
 
void myfunc(void) {
  printf("This is a sample function\n");
}



3. The main program

Code C - [expand]
1
2
3
4
5
6
#include <stdio.h>
#include "myfunc.h"
 
int main(void) {
  myfunc();
}



4. Compilation
4.1. Compilation of the main program
cc -o main.o -c main.c
4.2 Compilation of the lib function
cc -o myfunc.o -c myfunc.c
4.3 Link everything together
cc -o myprog main.o myfunc.o

5. Execution

./myprog

That's about it. Now you know how to do a multifile program. Next step: how to do a makefile.
Are there candidates for writing the makefile example?

Dora.
 

so if I understood your explanation, the OP forgot to include the led.h in his led.c file?
 

Hello!

so if I understood your explanation, the OP forgot to include the led.h in his led.c file?

First, what is OP?
Oregon Police?
Orgy Producer?
Occidental painter?

anything else?

Anyway, including led.h in led.c is necessary but not enough.
In the above example, if you just add myfunc.h in myfunc.c and don't compile it, then
it's useless, you will have a link error.
Now in a development enviornment, if you want a file to be compiled, you have to add it
to the project.

Dora.
 
Last edited:

It may be the use of the limited compiler version that depend on destrict and time era . Or this is from open source of compiler, so some register refer function is not support. Haha !
 

Thank you for your answers.
So now my code is that:
main.c
Code:
#include <LED.h>

void main(void)
{
	InitialiseLED();
	LED_Off();
	LED_On();
	while(1){};// pause execution here
}

LED.c
Code:
#ifdef __18F452
#include <p18cxxx.h>
#elif _16F877A
#endif
#include <LED.h>
void InitialiseLED(void)
{
	TRISB = 0xF0; // In binary 0b1111000 four higher bits - input; four lower bits - output
}

void LED_Off(void)
{
	PORTB&=(~0x01); //Turn LED0 off; first bitwise NOT (11111110); second bitwise AND (00000000 AND 11111110); result - 00000000
}

void LED_On(void)
{
	PORTB|=0x01; // Turn LED0 on; bitwise OR need to add (00000000 OR 00000001); result - 00000001
}

LED.h
Code:
void InitialiseLED(void);
void LED_Off(void);
void LED_On(void);

But now I have fault code saying: Error [1027] unable to locate 'LED.h'



Maybe u have any other ideas how sort it out?
Regards.
 

Hi,
Just change string
Code:
#include <LED.h>
on
Code:
#include "LED.h"
and compiler will search it in your project directory
 

...and maybe put a delay between the on and off if you want to see it blink.
(I'm assuming you wouldnt want to hide such a delay in an extenal file and switch on and off in this one)
 

Thank you everyone for help.
It is sorted now.

Regards,
Avov.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top