NO

Author Topic: Minimal library example  (Read 4914 times)

jwzumwalt

  • Guest
Minimal library example
« on: January 24, 2016, 08:09:24 AM »
What is a working minimal library file example?

I tried several examples I found on the internet and none worked.
Thanks for the help :)

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Minimal library example
« Reply #1 on: January 24, 2016, 09:19:01 PM »
What is a working minimal library file example?

I tried several examples I found on the internet and none worked.
Thanks for the help :)
Well, first of all, let's make sure we are talking about the same thing. You are talking about creating and using a .LIB file, right?
Or are you talking about a "shared library" as in .DLL file? Those are two different things but none are really rocket science.

A .LIB file is nothing more as a collection of .OBJ files, which are then linked to your project at compile/link time, while a DLL file is core that is "linked"/connected to at runtime.

Beside that clarification, please post an example of what you have tried, as that would make it easier for us to see where you went stray and how we can help you...

Ralf

jwzumwalt

  • Guest
Re: Minimal library example
« Reply #2 on: January 25, 2016, 10:58:05 AM »
Thanks for the reply.

What I am looking for is an example of using an #include with xxx.h header file.
I'm really programming a PIC controller but I wanted a working template in C
before I switched to the peculiarities of a Microchip.

UPDATED: The code below has been updated with the suggestions given and has been tested.

Code: [Select]
// sos.c (test include files)
#include "morse.h"

int main(void)  {
  dot(); dot(); dot();     // S
  printf("\r\n");

  dash(); dash(); dash();  // O
  printf("\r\n");

  dot(); dot(); dot();     // S
  printf("\r\n");
  // delay(3000);
return 0;
}


Code: [Select]
// morse.h

#ifndef Morse_h
#define Morse_h
    void dot(void);
    void dash(void);
#endif


Code: [Select]
// morse.c

void dot (void) {
  printf("dot ");
};

void dash (void) {
  printf("dash ");
};


« Last Edit: January 28, 2016, 07:09:39 PM by jwzumwalt »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Minimal library example
« Reply #3 on: January 25, 2016, 11:49:43 AM »
here is static library example project from your modified files
May the source be with you

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Minimal library example
« Reply #4 on: January 26, 2016, 02:21:33 AM »
here is static library example project from your modified files
Well, almost... ;-)

In the header file morse.h, the function dash() and dot() should properly define that their argument is "void" and the println() function should also be included...  ;)
Code: [Select]
// morse.h

#ifndef Morse_h
#define Morse_h

    void dot(void);
    void dash(void);
    void println (char *s);
#endif

jwzumwalt

  • Guest
Re: Minimal library example
« Reply #5 on: January 28, 2016, 07:12:31 PM »
Sooo... I updated the code and tested it - it runs fine.
Why is there no #include "morse.c", does the compiler automatically include a ".c" file with the same .h name?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Minimal library example
« Reply #6 on: January 29, 2016, 11:51:34 AM »
those functions are in morse.c, so include-file not needed,
but could use to keep function definitions be equivalent.
compiler don't include that morse.h automatically.
« Last Edit: January 29, 2016, 02:41:45 PM by TimoVJL »
May the source be with you

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Minimal library example
« Reply #7 on: January 31, 2016, 02:42:31 AM »
Sooo... I updated the code and tested it - it runs fine.
Why is there no #include "morse.c", does the compiler automatically include a ".c" file with the same .h name?
NEVER include source code (.c)! Always only include .h (header file)!

If you add the source file to the project, then those .c files will be compiled to object code (.obj)) and those subsequently being linked together with the default libraries (which are pretty much just a number of pre-compiled .obj files merged into one .lib file).

The header files (.h) like stdio.h or in your case, morse.h, contain just information about how types, variables and functions in such an "external" code/object file are defined, so that the compiler can produce the appropriate calls to those functions and knows about global types and variables defined in them.

You (should) have in C always one .h file for every .c file but the one that contains the main() function (morse.c->morse.h) or one per library file (.lib), like stdio.h, math.h, etc...

jwzumwalt

  • Guest
Re: Minimal library example
« Reply #8 on: January 31, 2016, 10:01:42 AM »
Thanks for explaining that.
I haven't programmed in C since I wrote code for Unix systems in the early 80's and I have forgotten everything I knew. Growing old is not as fun as I imagined it would be!

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Minimal library example
« Reply #9 on: February 01, 2016, 06:08:06 AM »
Thanks for explaining that.
I haven't programmed in C since I wrote code for Unix systems in the early 80's and I have forgotten everything I knew. Growing old is not as fun as I imagined it would be!
Welcome to my world!
Not that I actually feel that old, unless I look in the strange stare in the face of a 20 something year old telling them I am programming for 40 years now...  ;)

Ralf  8)