Error calling a function in c

0

I have the .c file with the function I want to invoke

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "entrada_minishell.h"

void imprimir_prompt()
{
   printf("minishell> "); 
   fflush(stdout);        
}
void leer_linea_ordenes(char *buf)
{

   memset(buf, 0, sizeof(BUFSIZ));

   if (fgets(buf, BUFSIZ-1, stdin) == NULL) /* fgets almacena la orden leída introduciendo también el carácter de fin de línea */
   { 
      buf[0] = '
#ifndef ENTRADA_MINISHELL_H
#define ENTRADA_MINISHELL_H

void leer_linea_ordenes(char *cad);
void imprimir_prompt();

#endif
'; return; } eliminar_salto_linea(buf); }

and its .h file

  #include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "internas.h"
#include "entrada_minishell.h"
#include "ejecutar.h"


int main(int argc, char *argv[])
{

   char buf[BUFSIZ];

   while (1)
   {
    imprimir_prompt();   
    leer_linea_ordenes(buf);

    if (strcmp(buf,"exit")==0){
        break;   
    }   

    else   
    {   
        if(es_ord_interna(buf)){
            ejecutar_ord_interna(buf);   
        }
        else{
            ejecutar_linea_ordenes(buf);
        }
     }  
   }

   return 0;    

}

when I want to invoke the function from another file called minishell.c with the main ()

CFLAGS= gcc -Wall -g

minishell: minishell.o entrada_minishell.o 
    gcc -Wall -g minishell.o entrada_minishell.o  -o  minishell 

entrada_minishell.o: entrada_minishell.c entrada_minishell.h
    $(CFLAGS) entrada_minishell.c -o entrada_minishell.o

minishell.o: minishell.c  
    $(CFLAGS) minishell.c -o minishell.o

I get this error: En la función 'main':/home/.../minishell.c:24: referencia a 'imprimir_prompt' sin definir

to compile it

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "entrada_minishell.h"

void imprimir_prompt()
{
   printf("minishell> "); 
   fflush(stdout);        
}
void leer_linea_ordenes(char *buf)
{

   memset(buf, 0, sizeof(BUFSIZ));

   if (fgets(buf, BUFSIZ-1, stdin) == NULL) /* fgets almacena la orden leída introduciendo también el carácter de fin de línea */
   { 
      buf[0] = '
#ifndef ENTRADA_MINISHELL_H
#define ENTRADA_MINISHELL_H

void leer_linea_ordenes(char *cad);
void imprimir_prompt();

#endif
'; return; } eliminar_salto_linea(buf); }
    
asked by hugoboss 02.05.2018 в 12:19
source

1 answer

1

The problem is in Makefile .

When you compile a .c with the intention of creating a .o , you must pass to the compiler the option -c , for example like this:

gcc -c minishell.c

You can omit in this case the -o minishell.o , since by default it will create an object with the same name as the source.

Without the option -c , gcc will execute in sequence two steps: first the compilation (which would not give errors in your case) and then the link to create an executable (which in your case would carry the name, wrong, minishell.o , and can not be created because functions are missing). You do not want me to try to create an executable, but to do only the compilation phase. For that is the -c option.

Another detail, which has no relevance to the error you mention, but which should be done well, is that CFLAGS is normally used to contain the compilation options, but not the compiler's own name that is usually saved instead in the variable CC .

Therefore, a Makefile more correct would be:

CFLAGS=-Wall -g
CC=gcc

minishell: minishell.o entrada_minishell.o 
    $(CC) $(CFLAGS) minishell.o entrada_minishell.o  -o  minishell 

entrada_minishell.o: entrada_minishell.c entrada_minishell.h
    $(CC) $(CFLAGS) -c entrada_minishell.c

minishell.o: minishell.c  
    $(CC) $(CFLAGS) -c minishell.c

In fact, you can omit the command in all your rules, now, thanks to your implicit rules , make knows which command to run by default to create a .o from a .c , or an executable from several .o . It is enough therefore that you specify the dependencies and he will deduce the rest:

CFLAGS=-Wall -g
CC=gcc

minishell: minishell.o entrada_minishell.o 

entrada_minishell.o: entrada_minishell.c entrada_minishell.h

minishell.o: minishell.c  
    
answered by 02.05.2018 в 16:44