• AVR Freaks

AnsweredHot!Header file required both by a Library and my App

Author
vibemac.1
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2016/05/31 07:39:32
  • Location: 0
  • Status: offline
2019/04/02 01:38:13 (permalink)
0

Header file required both by a Library and my App

Goodmorning,
I'm using a XC16 1.35 and MPLABX 5.00, and it's the first time to generate a library in MPLABX.
As stated in the title I've generated a Library which requires some consts in a header file. Then I have an app that include that Library and needs the same consts of the Library. My question is: What is the best approach?
1. Should I put that consts in the Library and export them to the app with library header file used for the functions?
2. Should I put the library project inside the app project, so that they can both reach that file?
Or have you got other ideas?
 
Thank you.
Davide Brunelli
#1
Mysil
Super Member
  • Total Posts : 3326
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Header file required both by a Library and my App 2019/04/02 03:09:58 (permalink) ☄ Helpfulby vibemac.1 2019/04/24 00:24:07
0
Hi,
The point of using a header file,
is that source text that shall be the same in several places, should come from the same file.
This may be: function argument list prototype declarations,
constants that shall be the same in library and application,
typedefs that shall be used by application when using library functions,
struct declarations that are used both by application and library code,
and Enumeration declarations ...
 
Then about 'consts's :
These are Data that may be stored in Read-only memory. 
may be tables, textstrings, menus, images for graphic display ...
These are data that should be Defined, that means allocated and stored in one place only.
That means in one of the .c files, like:
const int mybigtable[] = { 0x004D, 0x0069, 0x0063, 0x0072, 0x006F, 0x0063, 0x0068, 0x0069, 0x0070, 0x000A,
                           0x0047, 0x0072, 0x0061, 0x0070, 0x0068, 0x0069, 0x0063, 0x0073, 0x000A,
                           /* keep going with more data ... */
                         };

In the header file, place an external declaration for the table:
extern const int mybigtable[];     

then, the table may be used anywhere the headerfile is included.
 
For some other types of constant parameters, it may be more practical to define macro symbols in the header file.
 
If there are functions or variables that shall be private internal in the library,
then these may be kept away from the public header file.
Some libraries use a separate header file for such variables and functions.
 
    Mysil
post edited by Mysil - 2019/04/02 03:48:53
#2
vibemac.1
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2016/05/31 07:39:32
  • Location: 0
  • Status: offline
Re: Header file required both by a Library and my App 2019/04/23 08:38:47 (permalink)
0
Hello Mysil,
thank you for your answer and sorry for the delay but I've been busy with other projects.
I understand what you are saying, I've just one question more:
My purpose is to have a specific application that uses a bunch of methods contained in the library.
 
I'd like that, when I make a specific application, I don't need to recompile the library, but only the application so that the debugging phase could be faster.
 
The fact is that for each specific application, I've got to define some constants (#define) used both by library and application. Hence my question is: Since for every application, I change the value of those constants, do I have to recompile the library, too? Is it right?
 
Thank you for your support.
 
Regards,
Davide Brunelli
#3
scrungy
Senior Member
  • Total Posts : 133
  • Reward points : 0
  • Joined: 2016/02/02 09:56:54
  • Location: 0
  • Status: offline
Re: Header file required both by a Library and my App 2019/04/23 11:21:41 (permalink)
+1 (1)
Using #define to create constants, simply does a text substitution at compile time.  Under that scenario, you will have to recompile the library every time.  This is because the constant has to change.
 
However:  if the library references a constant, which you want in your code, or a variable, then the extern modifier comes into play.
 
#defines have to be in each file.
i.e. if you have two files     source1.c and source2.c and you wish to use a define like   #define ETX 03    then you have to do that in both the C files because those are text substitution macros.
 
Your library can reference external variables, of course, by tagging them extern in your library.  Then you must define them in your file, and the linker can stitch them together.
So lets say the library is source1.c
extern int application_variable;
foo()
  {
  int a;
        a=application_variable;
  whatever...
}
You have to define application_variable somewhere, so you could define it in a header file, say   lib.h
int application_variable;
 
Then the application would include lib.h, and it would need an extern int application_variable 
line.  both the library and the application would reference an extern variable, which is located in a header file.
 
#4
Mysil
Super Member
  • Total Posts : 3326
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Header file required both by a Library and my App 2019/04/23 13:10:59 (permalink)
+1 (1)
Hi,
If you have values that shall be different for each application, but never change when program is running,
then you may define those as const variables in main program, or in application code,
and as extern const in the header file for the library.
 
Example, in header file:  myLibrary.h
extern const  uint32_t Configuration_Constant; /* Value that will be used by library. */ 


In Application code:
#include "myLibrary.h"
const uint32_t Configuration_Constant = 0xC0123456; /* Constant value defined here. */


Then code in the library may use the value when header file have been included.
#include "myLibrary.h"

void  LibraryFunction (...)
{
    if (Configuration_Constant ...)
    {   ...  

If done like this, then there should be good description in header file, explaining what is expected.
 
    Mysil
#5
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Header file required both by a Library and my App 2019/04/23 14:14:07 (permalink)
+2 (2)
Do be aware that a "constant" defined with extern is not really a constant, and cannot be used for things like the size in an array declaration.
 

Nearly there...
#6
vibemac.1
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2016/05/31 07:39:32
  • Location: 0
  • Status: offline
Re: Header file required both by a Library and my App 2019/04/24 01:42:37 (permalink)
0
Goodmorning,
thank you all for your help. I've made a very simple example in attachment that contains both the library and the application projects thanks to your suggestions.
I've one last doubt:
if I look in microchip example http://microchipdeveloper.com/mplabx:libraries, it makes two simple functions without even making an header file for them inside the library (they are declared in MyLibrary.h in the App).
In my case, I often include in a library source file the header of other library source files, for using their functions. For keeping on doing that, do I have to leave the header files of my library and make a second declaration inside MyLibrary.h?
Thank you.
 
Regards,
DAvide Brunelli
#7
Mysil
Super Member
  • Total Posts : 3326
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Header file required both by a Library and my App 2019/04/24 04:43:33 (permalink) ☼ Best Answerby vibemac.1 2019/04/24 05:20:16
0
Hi,
As said in my first message, the point of using a header file, is that stuff that shall be identical shall come from the same place.
You may perfectly well #include  the header file for your library,
in source files for the same library, and I recommend that you do so.
 
It is O.K. to have the header file with prototype declaration of a function,
or extern declaration of global variables, #include   in the same file where the variable is defined,
and where the body of the library function is coded.
 
When it is done this way, the Compiler will check that declaration (in the header file),
is correct when compared with the actual function code (in the .c code file).
The same is the case with global variables with 'extern' specifier in the header file, whether 'const' is specified, or not.
 
Some of the coding standards for quality critical software have rules about such practices.
Also quality checking software tools, may check that such practices have been followed.
 
    Mysil
#8
Mysil
Super Member
  • Total Posts : 3326
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Header file required both by a Library and my App 2019/04/24 07:16:42 (permalink)
+1 (1)
Hi,
Have downloaded the test code from message #7, and will look into it.
 
While a reasonable effort, there are still details that may be improved.
 
The header file have include guard.
Good, but the comment at the end of the include guard block,
is different from the macro at the startof the block.
 
Header file also have C linkage block for eventual use with C++.
Good, but I suspect that also the  declaration of: extern const int ciaone;,
should be inside the C linkage block. 
Any problem because of this will not appear until an actual C++ compiler is used.
 
In the header file, my suggestion is that the library function in test.c ,
should be declared in the header file test.h:
int c(void);   

While 'c' is a good enough function name for use in a minimal test,
it isn't a particular good name for a library function.
If making a library for use in a BIG code system, it may be worth putting some planning into naming of libraries and library functions.
It is quite common to make every function name within the same library, to have some part of the name in common.
E.g. 
int test_c(void);   

 
But then in the Application project source code,
there is a header file with different name and different content, from what is in the library project.
This is a grave mistake, and break with the fundamental idea of using header files in C code:
Header file used when compiling the library,
and when compiling and linking the application, shall be the same file or an identical copy!
During development, it is definitely best to use the same header file both in library development,
and in development of the application.
If making a copy of the library archive file: BasicLibrary.X.a
is made for use in another project, then an identical copy of the header file: BasicLibrary.h, should be distributed at the same time.
 
In the header file: BasicLibrary.h,
there is Definition of the const variable: const int ciaone = 3; with initialization value.
This is contrary to what I tried to explain and demonstrate in message #5.
Definition of const variables, or other global variables with initialization value,
should be in one of the .c source files, and in one file only.
Either in one of the library source files, or in one .c source file in each application project.
 
Prototype declaration of the c library function in BasicLibrary.h is also incomplete.
Should be:
int c(void); 

 
    Mysil
 
#9
Jump to:
© 2019 APG vNext Commercial Version 4.5