Search code examples
clinuxlinkermakefiledynamic-library

Makefile: Linking .*a library


DESCRIPTION: I have a library libshell.a, inside of it is the function ord_interna that i'm attempting to use, however it seems i linked it wrong, could you guys fix my error, so i dont make it in the future? Cheers,

Error:

/tmp/ccn5lbmJ.o: In function `main':
minishell.c:(.text+0x4e): undefined reference to `ord_interna'
collect2: error: ld returned 1 exit status
make: *** [minishell.o] Error 1

Makefile:

CC=gcc 
CFLAGS=-Wall -pedantic -c

all: microshell

microshell: minishell.o
    gcc minishell.o -o microshell

minishell.o: minishell.c
    gcc minishell.c minishell.h entrada_minishell.c entrada_minishell.h ejecutar.c ejecutar.h libshell.a

clean:
    rm -rf *o microshell

Solution

  • From your makefile, I'm guessing you have these source files:

    minishell.c
    entrada_minishell.c
    ejecutar.c
    

    And that you want to compile them, and then link them all together with libshell.a to create an executable called microshell. In that case, you want something like:

    CC=gcc
    CFLAGS=-Wall -pedantic
    
    all: microshell
    
    microshell: minishell.o entrada_minishell.o ejecutar.o
        $(CC) -o $@ $^ -L. -lshell
    

    You can add a clean target if you want, but just that should get you going.

    Editorial notes:

    1. it's really weird to put header files on the compilation line; I assumed you didn't actually want to do that.

    2. You should look into gcc's -MMD flag to do automatic dependency generation.