Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

c - Undefined reference to functions called from the static library

I got this error when i launched make "undefined reference to this functions" : How can i solve the problem ?

tvi-stwunt@es7-stkw-dev-workstation:/home/tvi/projets/RabbitMQ/test_amqp> make gcc -L/home/tvi/projets/RabbitMQ/libamqp -lrabbitmq -lamqp amqp_consommateur.o -o amqp_consommateur amqp_consommateur.o: In function main': /home/tvi/projets/RabbitMQ/test_amqp/amqp_consommateur.c:48: undefined reference to amqp_connexion' /home/tvi/projets/RabbitMQ/test_amqp/amqp_consommateur.c:49: undefined reference to amqp_consommateur' /home/tvi/projets/RabbitMQ/test_amqp/amqp_consommateur.c:50: undefined reference to amqp_deconnexion' collect2: error: ld returned 1 exit status make: *** [amqp_consommateur] Error 1

This is my c function which contain the main

    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include <amqp.h>
    #include <amqp_tcp_socket.h>
    
    #include "libamqp.h"
    #include "amqp_outils.h"
    
    int main(int argc, char const *const *argv) {
        amqp_connection_state_t conn;
        char const *cle_liaison;
        char const *echange;
        char const *nom_hote;
        int port;
        char const *queue;
    
        nom_hote = argv[1];
        port = atoi(argv[2]);
        queue = argv[3];
        echange = "amq.direct";   /* argv[3]; */
        cle_liaison = "test queue"; /* argv[4]; */
    
        conn = amqp_new_connection();
        amqp_connexion(conn, nom_hote, port, echange, cle_liaison, queue);
        amqp_consommateur(conn, cle_liaison, echange);
        amqp_deconnexion(conn);
    
    
    return 0;
    } 

This is the Makefile

CC := gcc
CFLAGS := -Wall -Werror -g
INCLUDES := -I/usr/local/include/ -I/home/tvi/projets/RabbitMQ/libamqp/dlo 
LFLAGS := -L/home/tvi/projets/RabbitMQ/libamqp
LDLIBS := -lrabbitmq
SRCS := amqp_consommateur.c amqp_producteur.c
#LIBS := -L. -lamqp

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

.PHONY: all
all: $(PROGS)
    @echo "$(MAKE) : Tout est généré"

$(PROGS) : % : %.o Makefile 
    $(CC) $(LDLIBS) $(LFLAGS) $< -o $@ 

clean:
    rm -f $(PROGS) $(OBJS)

%.o: %.c Makefile
    $(CC) $(CFLAGS) $(INCLUDES) -c $< 

enter image description here

My goal is to generate the executable for the two files amqp_consommateur and amqp_producteur

question from:https://stackoverflow.com/questions/65919791/undefined-reference-to-functions-called-from-the-static-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
CC := gcc
CFLAGS := -Wall -Werror -g
INCLUDES := -I/usr/local/include/ -I/home/tvi/projets/RabbitMQ/libamqp/dlo 
LFLAGS := -L/home/tvi/projets/RabbitMQ/libamqp
LDLIBS := -lrabbitmq -lamqp
SRCS := amqp_consommateur.c amqp_producteur.c
#LIBS := -L. -lamqp

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

.PHONY: all
all: $(EXECS)
    @echo "$(MAKE) : Tout est généré"

$(EXECS) : % : %.o Makefile 
    $(CC) $(LFLAGS) $< -o $@ $(LDLIBS) 

clean:
    rm -f $(EXECS) $(OBJS)

%.o: %.c Makefile
    $(CC) $(CFLAGS) $(INCLUDES) -c $< 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...