I'm trying to create a static library that will need many different classes. Each class has its own header and source file. I've read lots of online tutorials about making a static library, but they all use simple examples which don't address my needs for this project. So far I've used ar -crs 'lib.a' obj1.o obj2.o ...
to create the 'lib.a' file.
According to this tutorial, after doing that, I have to copy all the definitions from my previous headers into a single header, called 'lib.h'. This manual approach won't work for me, as I want to automate this process in my makefile.
I have gotten it to work by using a 'lib.h' that includes all of the headers in the same directory, but obviously the goal is to have it work outside of the directory, with just 'lib.a' and 'lib.h'.
Another thing that may make things difficult for me is that I'm using a library (SDL2) which requires an ldflag. It ran fine before, but I want to make sure that it also works when I package it into my own library. I don't want to need the ldflag when implementing my library in the future, but rather have it be pre-packaged.
CodePudding user response:
you should have a header file that the callers of your code #include (like stdio.h). This should contain
- function declarations
- constants
- structs
- typedefs
- macros
this file is not generated from other header files, its just your public interface
Then you compile all the .c files (and non public .h files) into .o files
Then you use ar to create the .a file, then ranlib it
CodePudding user response:
Broadly speaking, when you distribute libraries, you don't do so with a single header. You distribute multiple headers, and for the sake of convenience, one of these headers will include all the rest.
I mean, you can distribute a library with a single header. But that usually involves developing the library such that all interface declarations (and definitions for templates) are all in the same header.
You can craft an omnibus header from a set of headers just by textually concatenating all of the header files you want to distribute together into a single file. You can even hook this into your makefile if you like.