I wrote myself a littly module I want to reuse. I got my header file bitstream.h
with a struct and function declarations and bitstream.c
with the implementations. Now I'd like to use this in my other programs, but without manually compiling bitstream.c
every time, like you don't have to compile stdio.h
every time you use it, but I don't get it to work. My files look like this:
bitstream.c
#include "bitstream.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
bit_stream_t *fbit_stream(const char *path) {
FILE *f;
/* open file for reading */
f = fopen(path, "rb");
...
bitstream.h
#ifndef BITSTREAM_H
#define BITSTREAM_H
#define EOS 0x0a
typedef struct {
int size;
int i_byte;
unsigned char i_bit;
unsigned char current_bit;
unsigned char *bytes;
} bit_stream_t;
extern bit_stream_t *fbit_stream(const char *path);
extern unsigned char bit_stream_next(bit_stream_t *bs);
extern void bit_stream_close(bit_stream_t *bs);
extern void print_bit_stream(bit_stream_t *bs);
#endif
I put these two files into /usr/local/include
(I'm on a Linux machine) and now I'd like to use this in main.c
(somewhere else, e.g. /home/foo/main.c
):
main.c
#include <bitstream.h>
#include <stdio.h>
int main(int argc, const char *argv[]) {
if (argc != 2) {
printf("Need 1 argument!\n");
return 1;
}
bit_stream_t *my_bs;
my_bs = fbit_stream(argv[1]);
while (my_bs -> current_bit != EOS) {
printf("%d", my_bs -> current_bit);
bit_stream_next(my_bs);
}
bit_stream_close(my_bs);
printf("\n");
return 0;
}
When I try gcc -Wall -o main.o main.c
I get
/tmp/ccgdq66W.o: In function `main':
main.c:(.text+0x35): Undefined reference to `fbit_stream'
main.c:(.text+0x63): Undefined reference to `bit_stream_next'
main.c:(.text+0x7b): Undefined reference to `bit_stream_close'
collect2: error: ld returned 1 exit status
What am I doing wrong? Thanks in advance for any help!
smuecke