Why i must to include my own lib archive when i´m compiling

Viewed 33

I have a problem including my own library.

I have created some functions in a few archives .c, after that i have created the header file .h, but when i try to compile a main.c that call a function that was in my own lib i must to type gcc ownlib.a main.c -o prog.out and include the lib ownlib.a archive as a parameter of gcc.

In this case i was trying to call my own strlen() called ft_strlen() function that is in my own lib.

I know when i compile any program using a function that is in a compiler library i only must include the header in the code for example: #include <string.h>, but when i call my own function i add #include "ownlib.h".

If i compile the main.c calling system function strlen() i have to compile only the main.c archive and it works. When i create a lib with my own ft_strlen() and in the main.c i include my own lib #include "ownlib.h" it doesn´t work if i do this gcc main.c -o program.out but it works if i add my lib archive like this gcc main.c ownlib.a -o program.out

I want to know why when i use a system library function i don´t need to compile with the library archive, for example gcc main.c string.c -o program.out, only whith the #include <string.h> it works, and why i must to include my ownlib.a in the compiation gcc libft.a main.c -o program.out. I don't know if I explained myself well


LIBNAME = ownlib.a

HEADERNAME = ownlib.h

SRCS = ft_strlen.c

OBJS        = $(SRCS:.c=.o)

CC      = gcc

CFLAGS      = -Wall -Wextra -Werror

AR      = ar

ARFLAGS     = -rcs

$(LIBNAME): $(OBJS) $(HEADERNAME)
    @$(AR) $(ARFLAGS) $(LIBNAME) $(OBJS)

all: $(LIBNAME)

clean:
    $(RM) $(OBJS)

fclean: clean
    $(RM) $(LIBNAME)

re: fclean all

%.o: %.c $(HEADERNAME)
    @${CC} ${CFLAGS} -c $< -o ${<:.c=.o}
   

.PHONY: all clean fclean re

one of the functions is like this:

    
    #include "ownlib.h"

    size_t  ft_strlen(const char *str)
    {
        int     i;

        i = 0;
        while (str[i] != '\0')
                i++;
        return (i);
    }

Th main function is:

#include <stdio.h>
#include "ownlib.h"


int     main(void)
{
        char    *str;
        
        str = "How many characters";
        
        printf("%i", ft_strlen(str);

        return (0);
}

If i compile gcc main.c -o program.out don´t know what is ft_strlen()

Thanks so much

0 Answers
Related