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.

Function in new header

Status
Not open for further replies.

venkates2218

Full Member level 6
Joined
Sep 30, 2016
Messages
354
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
4,306
Different header files.png

I'm using LCD,External EEPROM in my project.
1.External EEPROM,LCD,DELAY everything are written under an separate header file for easy access and debugging purpose.
2.External EEPROM,LCD,Delay are included in main file by #include "delay.h" #include "lcd.h" #include "external_eeprom.h"
3.For example:LED1 is blinking based on some conditions. Everything is working fine. I tried to make the LED2 blink base on same condition which used for LED1.
4. So I created an another header file for LED2 and copied the function from the LED1 to new header file.
5.In new header file I included everything by #include "delay.h" #include "lcd.h" #include "external_eeprom.h" and pasted the code copied from LED1.Then changed the function name and variable which should work for LED2 only independent from LED1.
6.When tried to execute the program it showing conflicting function, variable error.
7.I checked everything like variable and function name on both LED1 and LED2 there is no same function or variable with same name.
I need to create an separate header file for LED2.Please provide the solution to solve this issue...
 

The most usual, not sure wether it is the suited, is to place the headers at the main.h and then adding it at each <file>.c. This way you don't need to update each file at the inclusion of a new feature/function/structure that you want to use on the files of the code.
 

Duplicate variable names sounds like you are trying to declare variables in the header file then including them every time you use that header. It duplicates the variable each time the header file is used and causes the error you describe.

There are two ways to deal with this, the first is to make sure you make ONE declaration of the actual variables, then in the header file you share among other source files, use the "extern" keyword to tell the compiler the variable you refer to is from another file. For example:
in the first source file "int MyVariable = 123;" and in the header use "extern int MyVariable;" That makes files using that header look elsewhere for MyVariable being established.

The other method, which is more tedious and I would not recommend it in your instance, is to add "#ifdef" around each variable in the header. Like this:

#ifndef MyVariable
int Myvariable = 123;
#endif

There are times when it is useful to do it the second way but you can easily run into problems because it doesn't necessarily tell the compiler the variable exists somewhere else, it just makes sure you don't declare it twice.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top