C - Function has internal linkage but is not defined

16,373

Explanation of the error:

The compilation error occurs because:

  1. You have declared a function with static keyword (i.e. with internal linkage) inside lagrange.h
  2. And you have included this file lagrange.h in main.c file different from lagrange.c, where the function is not defined.

So when the compiler compiles main.c file, it encounters the static declaration without any associated definition, and raises logically the error. The error message is explicit.

Solution:

In your case the solution is to remove the static keyword, because static means that the function can be called only in the .c file where it is defined, which is not your case.
Moreover, a good practice may be to declare any static function in the same .c file where it is defined, and not in .h file.

Share:
16,373
th3g3ntl3man
Author by

th3g3ntl3man

¯\_(ツ)_/¯

Updated on June 28, 2022

Comments

  • th3g3ntl3man
    th3g3ntl3man almost 2 years

    I have this folder structure:

    structure

    I wrote a simple function to test the folder structure, for seen if all header file but when I compile with the make command, I have this error:

    warning: function 'stampa' has internal linkage but is not defined

    I the lagrange.h file I have this:

    #ifndef LAGRANGE_H
    
    static void stampa(int i);
    
    #endif /* LAGRANGE_H */
    

    and in the lagrange.c file I have this:

    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
    #include <stdio.h>
    
    void stampa(int i){
        printf("%d", i);
    }
    

    in the end, int main.c I simply call the function stampa passing them a number.

  • Jesse Chisholm
    Jesse Chisholm almost 5 years
    Don't forget that putting things in the anonymous namespace also makes it file static. e.g., namespace { ... } is only visible to that code translation unit.
  • Nimrod Dayan
    Nimrod Dayan over 2 years
    @JesseChisholm that's only relevant to C++, but the OP is talking about C