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.

How to make a proper Header file in C that contains only my functions????

Status
Not open for further replies.
No, your header file will be the same.

You just need the command line to compile your lcd code into a library as Mplab does not support the Hi-Tech 'libr.exe' program.
Then you add the library file to your project in the project window.

Library files are used all the time, such as 'stdlib.h', 'string.h', stdio.h' to name a few.
When using a library, the linker will only link in the code for the functions that are called by your program.


Mplab does support the Mplab C30 librarian, so you can make a library from within Mplab.
 
This is the error log that i am getting

Code:
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\lcd.p1".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.p1".
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.cof" doesn't exist.
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.hex" doesn't exist.
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.sym".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.map".
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.hxl" doesn't exist.
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\startup.lst".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\startup.rlf".
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\doprnt.p1" doesn't exist.
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\doprnt.pre" doesn't exist.
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.obj".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.lst".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.rlf".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.sdb".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.mcs".
Clean: Done.
Build C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd for device 16F877A
Using driver C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe

Executing: "C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe" --pass1 C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\lcd.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Executing: "C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe" --pass1 C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Warning [361] C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.c; 12.1 function declared implicit int
Executing: "C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe" -otest_program_lcd.cof -mtest_program_lcd.map --summary=default --output=default lcd.p1 main_prog.p1 --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode)  V9.81
Copyright (C) 2010 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)
[COLOR="#FF0000"]Error   [500] ; 0. undefined symbols:
	_lcd_init(test_program_lcd.obj) _lcd_clear(test_program_lcd.obj) ___delay_ms(test_program_lcd.obj) 
[/COLOR]
********** Build failed! **********

The above error highlighted in red, may indicate you have not added the lcd.h header file and lcd.c C file to the project window. To do so right click on the project window and select "Add files..", make you have "All files *.*" selected in the Select Files Dialog, add both the lcd.h and lcd.c to the project window.

If you are still have issues, zip up the entire project directory and upload.



Means I will not be able to make my own header file without using the Command Line Option

Btbass is referring to the creation on an actual code library. Which require the use of HiTech's Librarian Utility, (LIBR.EXE). Which creates a true p-code or object code reusable library which can be then linked to a new program without recompiling the library code.

Reference: HI-TECH C® for PIC10/12/16 User’s Guide, Section: 6.2 LIBRARIAN, pg 155 for compiler v9.83

6.2 LIBRARIAN

The librarian program, LIBR, has the function of combining several files into a single file known
as a library. The reasons you might want to use a library in a project are:
• there will be fewer files to link
• the file content will be accessed faster
• libraries uses less disk space
The librarian can build p-code libraries (.lpp extension) from p-code files (.p1 exten- sion), or
object code libraries (.lib extension) from object files (.obj extension).
P-code libraries should be only created if all the library source code is written in C. Object code
libraries should be used for assembly code that is to be built into a library.
With both library types, only those modules required by a program will be extracted and included in
the program output.

6.2.1 The Library Format

The modules in a library are simply concatenated, but a directory of the modules and symbols in the
library is maintained at the beginning of a library file. Since this directory is smaller than the
sum of the modules, on the first pass the linker can perform faster searches just reading the
directory, and not all the modules. On the second pass it need read only those modules which are
required, seeking over the others. This all minimizes disk I/O when linking.
It should be noted that the library format is not a general purpose archiving mechanism as is used
by some other compiler systems. This has the advantage that the format may be optimized toward
speeding up the linkage process.
may be optimized toward speeding up the linkage process.

BigDog
 
yeah i have tried that

and its giving much more error than your suggested one...
 

Thank you bigdogguru.

Yes,its quite easy.
If your lcd code file is called 'lcd.c', after compiling it in mplab you should have a file called 'lcd.p1'.

Then copy 'libr.exe' file to your source directory where 'lcd.p1' resides.(To save typing in a long path name)
Just open a command line window there and type libr
You should get
libr>

Then type:
R lcd.lib lcd.p1

This should create the lcd.lib file.

Then add the file to your project.
 
My New Error log;
I had done whatever you told me.

Code:
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\lcd.p1".
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.p1" doesn't exist.
Clean: Done.
Build C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd for device 16F877A
Using driver C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe

Executing: "C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe" --pass1 C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\lcd.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Executing: "C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe" --pass1 C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Warning [361] C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.c; 6.1 function declared implicit int
Warning [361] C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.c; 8.1 function declared implicit int
Warning [361] C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.c; 10.1 function declared implicit int
Executing: "C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe" -otest_program_lcd.cof -mtest_program_lcd.map --summary=default --output=default lcd.p1 main_prog.p1 C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\lcd.lib --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode)  V9.81
Copyright (C) 2010 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)
Error   [380] ; . unknown record type (114)

********** Build failed! **********

Capture.png
 

The main issue was conflicting prototype and function definitions for:

Code:
void lcd_init([COLOR="#FF0000"]void[/COLOR]);

void lcd_init([COLOR="#FF0000"]unsigned char value[/COLOR])
	{
		lcd_cmd(0x38);
		lcd_cmd(0x0E);
		lcd_cmd(0x01);
		lcd_cmd(0x06);
		lcd_cmd(0x80);			
	}

The project now compiles without errors or warnings.

See attached project.

After testing your code, you could then attempt to create a library as Btbass suggested.

BigDog
 

Attachments

  • ArunSharma.zip
    66.2 KB · Views: 81
If you have made lcd.c into a lib, then you should remove lcd.c from the project as it is now in a lib.
Also your zip file was corrupt, I could not open it?
 
I would also recommend removing the following #define from lcd.h and place it within its own header file or simply place it at the very top of main.c:

Code:
#define _XTAL_FREQ 20000000

The reason being if left in the lcd.h, it could possible override any other crystal frequency settings you actually intended for main.c and routines utilizing this particular #define.

I usually create a separate header file with contain this #define and other settings which apply to the current program development only.

Such as main.h or settings.h.

BigDog
 
I am so sorry..
Why things are not working my way...

The Zip file u given to me is not working means when i try to build the project nothing happens..

This is for BigDogGuru

First Snap

**broken link removed**


Second Snap

**broken link removed**


Third Snap

**broken link removed**

and the mistake told by you..

I am really very sorry for such a silly mistake...

But even after fixing that mistake nothing works for me..

Same error..

and i have used the method suggested by btbass
i am getting the same error

I am again uploading my project directory
View attachment LCD_Header_File_Test.zip

and here is the snapshot again
Capture.png


tell me what to do now...
 

I merged your two postings and in doing so the system deleted the "snaps" or thumbnails.

However, my guess is the compiler window was actually open but out of view. I'm using a very large resolution display, 1920x1200, so when I open your project I rearranged the windows and mistakenly saved the workspace before zipping up the project and uploading it.

I suggest getting your project to compile first with lcd.c and then create the lcd.lib file.

Can you reopen the project I uploaded and select "Cascade" or "Tile..." under the "Window" menu to see if the compiler/build window appears?

BigDog
 
Hi Arun,

couple of issues you would need to address:
1. Always #ifndef defined alexan_e mail to avoid multiple inclusion.
2. If you want to define macros in header file, do it inside the #ifndef to avoid multiple definition. the best practice would be to have it in c file.
3. Use extern key word for every function declaration in the header file. this is required as for a file which uses it understands that its not defined in its scope.
4. The function selection macros has to be defined in one common place, i.e the header file or make file(if you have any) which must be included by both the lcd.c and main_program.c. For now I suggest to use the lcd.h, but later when you have learned, you can use write a makefile.
5. Use static keyword for function which is local to a file. example lcd_data and lcd_cmd in lcd.c file no need to declare it in header file.

Here is the content you would need,

lcd.h
Code:
// Comment macro to ignore a function from the build
#define _LCD_INIT
#define _LCD_CLEAR
#define _LCD_DATA_STRING
#define _LCD_FIRST_ROW
#define _LCD_SECOND_ROW

#ifndef  __LCD_H__
#define __LCD_H__ 

//This is not good practice. I defined them here to keep your format.
#define _XTAL_FREQ 20000000
#define LCD_DATA PORTB
#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2

#ifdef _LCD_INIT
extern void lcd_init(void);
#endif

#ifdef _LCD_CLEAR
extern void lcd_clear(void);
#endif

#ifdef _LCD_DATA_STRING
extern void lcd_data_string(unsigned char msg[]);
#endif

#ifdef _LCD_FIRST_ROW
extern void lcd_first_row(void);
#endif

#ifdef _LCD_SECOND_ROW
extern void lcd_second_row(void);
#endif

#endif // __LCD_H__

lcd.c
Code:
/*All LCD Functions Definitions*/
#include<htc.h>
#include<string.h>
#include"lcd.h"

static void lcd_cmd(unsigned char value)
{
	LCD_DATA = value;
	RS = 0;
	RW = 0;
	EN = 1;
	__delay_ms(1);
	EN = 0;	
}

static void lcd_data(unsigned char value)
{
	LCD_DATA = value;
	RS = 1;
	RW = 0;
	EN = 1;
	__delay_ms(1);
	EN = 0;
}

#ifdef _LCD_INIT
void lcd_init(unsigned char value)
{
	lcd_cmd(0x38);
	lcd_cmd(0x0E);
	lcd_cmd(0x01);
	lcd_cmd(0x06);
	lcd_cmd(0x80);			
}
#endif

#ifdef _LCD_CLEAR
void lcd_clear(void)
{
	lcd_cmd(0x01);		//Clears The LCD
}
#endif

#ifdef _LCD_DATA_STRING
void lcd_data_string(unsigned char msg[])
{
	unsigned int j,len;	
	len = strlen(msg);
	for(j=0;j<len;j++)
	{	
		lcd_data(msg[j]);
	}
}
#endif

#ifdef _LCD_FIRST_ROW
void lcd_first_row(void)
{
	lcd_cmd(0x80);
}
#endif

#ifdef _LCD_SECOND_ROW
void lcd_second_row(void)
{
	lcd_cmd(0xC0);
}
#endif

lcd_main.c
Code:
#include<htc.h>
#include"lcd.h"

void main()
{
	lcd_init();
	__delay_ms(100);
	lcd_clear();
	while(1);
//	lcd_data_string("Arun Sharma");
}
 
Thanks to all of you...
Finally my lcd header file is working properly...

But still there are many questions...

I had read that function declaration are by default of extern type
means
void lcd_init();
is same as
extern void lcd_init();

am i right...

now second question if for bigdogguru
special thanks to you..
But my question is
you and btbass has told me to create a library file
libr> R lcd.lib lcd.p1

i am not using this right now and my code still works fine so where is the use of this lcd.lib file..
I had not added this file to my project...

and final and the most important

in the starting of this thread
i want that...

i want a header file in such a way...
that

if i call only a single function then only code size related to that will include...
but at last all functions are included in this...

Is there any solution for that...

i mean to say that

if i am calling lcd_init()
function then code related to that must include in my code memory...
but this is not the case all the functions are included as

we have use
Code:
#define _LCD_INIT
#define _LCD_CLEAR
#define _LCD_DATA_STRING
#define _LCD_FIRST_ROW
#define _LCD_SECOND_ROW

at the starting of the program


if someone has any suggestion regarding this

then pls tell me....


And thanks to you all guys..
thanks for your help..

Regards
Arun Sharma
 

arunsharma0731 said:
void lcd_init();
is same as
extern void lcd_init();

am i right...
Not exactly. The first case is the function prototype. If you are using strict ANSI C compiler, then without this an error will be thrown. The second case is not compiler dependent. It is obligatory to write this line if you want to call that function from another line. However, there is a more optimum way to achieve this. If you have a header file where the prototype is placed, then this header could be included by the files that intend to call this function. Then there is no need for external call, just include the header file and call this function freely.


arunsharma0731 said:
if i am calling lcd_init()
function then code related to that must include in my code memory...
I wouldn't go with this macro method you are using. I would just comment out whatever is not needed, like I pointed out in post #9. You are maybe enthousiastic about it at the moment because it finally worked (congrats by the way), but you will soon find out that for bigger projects this will be a dirty solution. Today you have your lcd functions. Tomorrow you will write functions for relay, LEDs etc. For each new function you will create, a corresponding macro will be introduced. This is messy, your header files will be difficult to read, and their size will be way bigger than they should be. If you insist in the macro method, then you will just comment out the functions that are not needed for this project.

Code:
#define _LCD_INIT
#define _LCD_CLEAR
#define _LCD_DATA_STRING
#define _LCD_FIRST_ROW
//#define _LCD_SECOND_ROW

In the above example the lcd_second_row() function will not be compiled in the project, because _LCD_SECOND_ROW is not defined. So the code will not get bigger because of the lcd_second_row() function. Its code will be there, but its size will not be added in the total code size.

Hope that hepled,
Alexandros
 
Error [380] ; . unknown record type (114)

This error is caused by using the wrong type of library. (My Mistake) it sholuld be a .lpp not a .lib if it is C code.

libr> R lcd.lpp lcd.p1

This will create the correct librbary type which will link in correctly.
 

Thanks btbass....

My Header File Works and I am happy...

But i want to ask something

I had used MikroC compiler earlier...
these compilers have many functions for various devices...

And We are not able to see the code behind each functions ...

and let us suppose i am using LCD Library of that Compiler

suppose i need to just include Lcd_Init() functions
So it means that

when i call that function it means that all the code associated with that library will gets included

i am saying this because we have not to comment out any statement or any macros there..

I don't know...

Or there is some other way to do so...
I am confused..

Anyways Thanks to all of you..
I am very happy that my header file is working
:)


Regards
Arun Sharma
 

The built-in libraries of mikroC are automatically linked to your application if you call respective functions. The normal method is to add the sources for a function to your project or compile them to a library before.
 

I had used MikroC compiler earlier...
these compilers have many functions for various devices...

And We are not able to see the code behind each functions ...

One of many major drawbacks with the use of any MikroC Compiler. You can't easily pull the curtain back to see the wizard with MikroC.


and let us suppose i am using LCD Library of that Compiler

suppose i need to just include Lcd_Init() functions
So it means that

when i call that function it means that all the code associated with that library will gets included

i am saying this because we have not to comment out any statement or any macros there..

I don't know...

Or there is some other way to do so...
I am confused..

MikroC uses libraries, similar, but not as transparent, to the libraries created with HiTech's Librarian LIBR.EXE.

Some of the advantages of the creation of a library is the following:

With both library types, only those modules required by a program will be extracted and included in the program output.

Which requires breaking your routines into separate modules/files when building your library.

As Btbass commented, the P-Code libraries (.lpp) are used with the C code generator and the Relocatable Object Code libraries (.lib) with the linker.

At this point you'll most likely want to generate P-Code libraries (.lpp).

By carefully dividing your various routines in separate modules/files, only those required by the program will be extracted and loaded into the final code.

BigDog
 

Thanks Big Dog Guru..
Thanks

At present will follow the method taught by you...
if in future some problem exist.. then i will contact u..

Thanks a lot..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top