Headers, libraries in MPLAB X - Page 1
Forum section: electronics / general

author: icon
author_rank: Regular Contributor
timestamp: April 10, 2014, 02:57:04 pm
content: HiI'm trying to tidy up C source files which are becoming unwieldy, so I'm puzzling out how to use headers and libraries.My initial thought was to stuff the various function prototypes and definitions in a .h file and #include them, but further reading persuades me that this is Poor Form - headers should not contain executable code.Ok, so now I have to create a library - MPLAB X allows the creation of library projects, which allow you to create and compile library files. Only trouble is, for some reason the wizard only allows me to use mpasm - it doesn't show me my compilers. If I create an ordinary empty project, I can select XC8 as the compiler. Even after creation of the library project I can't change the relevant project property to XC8 - it's not listed.Any idea where I'm going wrong?ThanksJohn

author: hans
author_rank: Super Contributor
timestamp: April 10, 2014, 03:42:45 pm
content: The .c files contain the source, the code for each particular function, or the actual definition of variables. (say like uint8_t counter = 0; as a global variabele)The header files just contain descriptions, like structs, unions, macro's, and function prototypes. They indeed should not contain code.You can look headers as a sort of blackbox principle: the headers are the outside of the box (what are my connections), the source is the inside of it (how does it work). With the headers you can use the code of the source file (or a library file for that matter) without knowing how it exactly is put together.Compilation exists of 2 steps: compiling to an intermediate format (object code) and linking that format to a final image. It doesn't compile all source files at once; it does this one at a time.Put simply, during compilation the compiler doesn't know where everything will end up absolutely, so it may mark stuff like: "use this variable here, call this function here". It can only do that if it knows how the function looks like (parameters) so it knows how to call it, and also what type exist etc.The linker puts the whole program together and "links" everything together.I haven't ever had the need to use library projects before. In most cases, the compiler & linker is pretty flexible. I would only use library files if I would want to hide the source code from the "user" (developer).In that case, also, you would need header files to describe what's in the library, and how each function is called.Why it won't allow you to select XPASM I don't know. At my installation (v2.05) I can select XC16 or XC8 for a library project.. Have you selected the right chip in the wizard?

author: dannyf
author_rank: Super Contributor
timestamp: April 11, 2014, 10:56:59 am
content: QuoteUnderstand what a header file is (vs. a source file) and what a library is and the whole thing will be crystal clear to you.

author: icon
author_rank: Regular Contributor
timestamp: April 14, 2014, 07:13:15 pm
content: Quote from: dannyf on April 11, 2014, 10:56:59 amWell, after using Code::Blocks to write code which calls functions prototyped in header files and defined in object code libraries, the crystalline nature of the thing failed to reveal itself.It turned out that it's just a limitation of the MPLAB X/XC8 combination; you can create libraries using a supplied command line utility, but not the IDE.Fortunately, I can do without...RegardsJohn

author: dannyf
author_rank: Super Contributor
timestamp: April 15, 2014, 06:07:57 am
content: -turned out that it's just a limitation of the MPLAB X/XC8 combination; you can create libraries using a supplied command line utility, but not the IDE.-I doubt that.

author: icon
author_rank: Regular Contributor
timestamp: April 15, 2014, 12:32:55 pm
content: Quote from: dannyf on April 15, 2014, 06:07:57 amMPLAB XC8 C Compiler User's Guide, Section 8.2.2, p.292: 'Using the Librarian' - "The librarian cannot be called from IDEs, such as MPLAB IDE."The compiler has command line options for building libraries, but they are not integrated into the IDE. Seems like a bit of an oversight.John

author: hans
author_rank: Super Contributor
timestamp: April 15, 2014, 12:48:39 pm
content: Could be due to the nature of the XC8 compiler. It's the old hi-tech compiler, which may work a lot differently. The XC16 and XC32 compilers are based on GCC, and probably a more widely supported compiler architecture, environment and integration into netbeans. (They are also open source).Additionally strange, I am pretty certain I could select XC8 on my Linux installation for a library project, but now on Windows I can't (both v2.05).Also, reading the release notes for v2.05 and the "pending issues", well yep..Quote