How to create libraries ?
Bean
Posts: 8,129
Since I know very little about C, I was waiting for the beta to be released before I jump in.
But now I'm here and ready to get to work.
Can you explain step-by-step how I create a library of functions ? I know there is a .h file and a .c file, but I can't figure out how to get my code to use them ?
For example take a program with a function in it and show how to break that function into a seperate library.
Thanks,
Bean
But now I'm here and ready to get to work.
Can you explain step-by-step how I create a library of functions ? I know there is a .h file and a .c file, but I can't figure out how to get my code to use them ?
For example take a program with a function in it and show how to break that function into a seperate library.
Thanks,
Bean
Comments
It's great to hear that you're interested in trying Propeller GCC! Can you say how you'd like to create and use these libraries? Do you want to do this using SimpleIDE or command line tools? I can help with using the command line tools but Steve will have to say how you go about creating libraries using SimpleIDE. I'm pretty sure that it is easy to *use* libraries with SimpleIDE but I don't know what needs to be done to create them.
Welcome aboard!
David
Yeah, I'm pretty excited about learning C after all this time.
From what I understand, you put the function prototypes into a *.h file, and the complete function code into a *.c file. But there is more to than that. I guess you have somehow compile the *.c file or something ???
I'm just not familiar with it at all. I'd like to do it with the SimpleIDE if it is possible.
I have translated some code to C and I have gotten everything to work by putting all the code in one file. But I'd like to make libraries for myself and others to use.
Bean
Ray
A C Library has two possible meanings.
- a collection of functions in one or more source files
- a binary archive of objects containing functions
SimpleIDE supports creating and using functions in one or more source files, but only allows using binary archives. It may be possible at some point to create a binary archive library in a a "simple" way in future release, but not at this time. I've posted an enhancement issue for this.As to the first meaning, files can be added to a project either by copying them to the project folder or by linking to the files in a separate folder. Please see the Project Manager section of the user guide.
Best practices for creating C libraries that can be used in/with a binary archive:
- A binary archive library is a collection of objects built from sources.
- A library header file is used for compile-time function type checking for the library.
- For every .c file there should be a .h file - a source module.
- The .c file should contain the implementation of a single public function
- The .h file should contain the public signature (and struct types if necessary)
- Single .c modules should be as light weight as possible.
- Linking with a function in an object will include the entire object.
- A summary library .h file can include all the other .h files.
- A library .c file should not include the summary .h file.
Hope this helps.--Steve
You can create a library from a number of object files by the following sequence of commands:
This will create a library called libmine.a which you can include in your program using the propeller-elf-gcc command line option "-lmine".
The easiest way to create a source code library is to put all the functions for that particular library into one .c file (this is exactly the opposite of best practice for binary files, but makes managing the source code easier). Put declarations for all the functions or variables that you want users to be able to access into a .h file. Let's assume your library is called "foo", so it has foo.c and foo.h. To use the library in a project called "blah" put into your main source code file blah.c, and add foo.c and foo.h to the project. Steve's user guide discusses how to do this: