Author Topic: Headers, libraries in MPLAB X  (Read 12792 times)

0 Members and 1 Guest are viewing this topic.

Offline iconTopic starter

  • Regular Contributor
  • *
  • Posts: 246
Headers, libraries in MPLAB X
« on: April 10, 2014, 02:57:04 pm »
Hi

I'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?

Thanks
John

 

Online hans

  • Super Contributor
  • ***
  • Posts: 1640
  • Country: nl
Re: Headers, libraries in MPLAB X
« Reply #1 on: April 10, 2014, 03:42:45 pm »
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?
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Headers, libraries in MPLAB X
« Reply #2 on: April 11, 2014, 10:56:59 am »
Quote
Any idea where I'm going wrong?

Understand what a header file is (vs. a source file) and what a library is and the whole thing will be crystal clear to you.
================================
https://dannyelectronics.wordpress.com/
 

Offline iconTopic starter

  • Regular Contributor
  • *
  • Posts: 246
Re: Headers, libraries in MPLAB X
« Reply #3 on: April 14, 2014, 07:13:15 pm »
Quote
Any idea where I'm going wrong?

Understand what a header file is (vs. a source file) and what a library is and the whole thing will be crystal clear to you.

Well, 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...

Regards
John
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Headers, libraries in MPLAB X
« Reply #4 on: April 15, 2014, 06:07:57 am »
-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.
================================
https://dannyelectronics.wordpress.com/
 

Offline iconTopic starter

  • Regular Contributor
  • *
  • Posts: 246
Re: Headers, libraries in MPLAB X
« Reply #5 on: April 15, 2014, 12:32:55 pm »
-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.

MPLAB 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
 

Online hans

  • Super Contributor
  • ***
  • Posts: 1640
  • Country: nl
Re: Headers, libraries in MPLAB X
« Reply #6 on: April 15, 2014, 12:48:39 pm »
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
MPLABX-2084
   

MPLAB XC8 does not have library support built into the IDE. Libraries of common source can be created using the command line as there cannot be mixed assembly and C. Review compiler guide for details.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf