How to invoke function from external .c file in C?

前端 未结 8 1805
清酒与你
清酒与你 2020-12-23 19:31

My files are

// main.c  

#include \"add.c\"

int main(void) {
    int result = add(5,6);
    printf(\"%d\\n\", result);
}  

and

         


        
8条回答
  •  自闭症患者
    2020-12-23 20:23

    you shouldn't include c-files in other c-files. Instead create a header file where the function is declared that you want to call. Like so: file ClasseAusiliaria.h:

    int addizione(int a, int b); // this tells the compiler that there is a function defined and the linker will sort the right adress to call out.
    

    In your Main.c file you can then include the newly created header file:

    #include 
    #include 
    #include 
    
    int main(void)
    {
        int risultato;
        risultato = addizione(5,6);
        printf("%d\n",risultato);
    }
    

提交回复
热议问题