Home > Software engineering >  Why are these static functions working outside their file?
Why are these static functions working outside their file?

Time:10-30

I defined the next two static functions in a file named Grafico.h.

static inline interface_t * obtener_intf_por_nombre(nodo_t *nodo, char *nombre_if) {
    for (int i = 0; i < MAX_INTF_POR_NODO;   i)
    {
        if(!nodo->intf[i]) return NULL;
        if(strncmp(nodo->intf[i]->nombre_if, nombre_if, TAM_NOMBRE_IF) == 0) {
            return nodo->intf[i];
        }
    }
    return NULL;
}

static inline nodo_t * obtener_nodo_por_nombre(grafico_t *topologia, char *nombre_nodo) {
    return obtener_elemento(topologia->lista_nodos, nombre_nodo);
}

In my main file I called these functions without remembering they were static, and I didn't get any warning or error from the compiler related to it.

#include <stdio.h>
#include "Net.h"
#include "Grafico.h"
#include "Topologias.h"
#include "CommandParser/libcli.h"
#include "clired.c"

extern grafico_t *const_primera_topo();
grafico_t *topo = NULL;


int main(void) {
    inic_cli_red();
    topo = const_primera_topo();

    sleep(2);
    nodo_t *nodo_trans = obtener_nodo_por_nombre(topo, "R0");
    interface_t *interface_trans = obtener_intf_por_nombre(nodo_trans, "ethR0/0");
    char *mensaje = "Este es un mensaje de prueba\0";
    printf("Llegamos hasta aquí señores. Fue un honor.\n");
    enviar_paquete(mensaje, strlen(mensaje), interface_trans);
    
    start_shell();
    return 0;
}

In case it's useful, here is my makefile.

TARGET = $(BIN_DIR)/sim_tcp_ip
LIBS = -lpthread -L ./CommandParser -lcli -lrt
OBJS = $(OBJ_DIR)/prueba.o \
       $(OBJ_DIR)/ListaEnlazadaGenerica.o \
       $(OBJ_DIR)/Grafico.o \
       $(OBJ_DIR)/Net.o \
       $(OBJ_DIR)/Topologias.o \
       $(OBJ_DIR)/Com.o \
       $(OBJ_DIR)/Utiles.o \
       $(OBJ_DIR)/Capa2.o
BIN_DIR = ./bin
OBJ_DIR = ./obj
INC_DIR = ./inc
SRC_DIR = ./src
CFLAGS = -g -lpthread -Wall -I$(INC_DIR)

$(TARGET): $(OBJS) CommandParser/libcli.a
    mkdir -p $(BIN_DIR)
    gcc $(CFLAGS) $(OBJS) -o $(TARGET) $(LIBS)
    
$(OBJ_DIR)/%.o : %.c
    mkdir -p $(OBJ_DIR)
    gcc -c -MD $(CFLAGS) $< -o $@
    
CommandParser/libcli.a:
    (cd CommandParser; make)

-include $(OBJ_DIR)/*.d
    
.PHONY: clean
clean:
    rm -rf $(OBJ_DIR) $(BIN_DIR)
    (cd CommandParser; make clean)
all:
    make

Is there something wrong with the definition of these static functions?

CodePudding user response:

When you #include a file, it is equivalent to copypasting the contents of that file in place of that line. So the static functions are in the same compilation unit, and hence just as usable as any other static thing you define in the .c file itself.

CodePudding user response:

The problem is that you have declared them in your header file, Graphico.h!

The static keyword just means that if you compile a source C file prog.c into an object file prog.o, then any static functions in prog.c will be hard coded as memory addresses in prog.o. That is to say, whereas a normal function has an entry defined in the symbol table of prog.o, that is used when prog.o is linked with another object file, a static function does not.

  • Related