c - "undefined reference to function"C 中的错误取决于定义的位置

标签 c linker-errors undefined-reference

我试图理解为什么 foo 函数只有当我将其定义放在列表的最后时才会触发链接器错误“对 foo 的 undefined reference ”,并且如果声明位于列表中的其他任何位置,则错误是已经不在那里了。 如果有帮助,我正在使用 DevC++ 5.3.0.4 创建一个 C 项目,并以此文件作为主文件。 所有其他函数都正常工作,我想了解为什么定义的位置如此重要,因为该函数不使用其他函数?

最初,确实如此,但在试图找出出现此错误的原因时,我消除了我认为可能的原因,从而消除了 foo 函数体内的所有其他函数调用。

我猜它可能与数组参数有关,但是这个函数有什么特别之处而不是其他函数?

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





void removeDuplicate(int position, int vector[], int n); 
int belongsToSet(int x, int v[], int vectorDimension);
int isDuplicate(int position, int vector[], int n);
void displaySet(int v[], int dimension);
int foo(int v[]);




int main(int argc, char *argv[]) {
    int m = -1, n = -1, i, j;
    int a[20], b[20], reunion[40], intersection[40], abDifference[20],
     baDifference[20];
    int reunionIndex = 0, intersectionIndex = 0, abDifferenceIndex = 0,
      baDifferenceIndex = 0;


    i = foo(a);
    getch();
    return 0;
}

int belongsToSet(int x, int v[], int vectorDimension){
    int i;
    for (i = 0; i < vectorDimension; i++){
        if (v[i] == x){
            return 1;
        }
    }
    return 0;
}



void removeDuplicate(int position, int vector[], int n){
    int i;
    for (i = position; i < n - 1; i++){
        vector[i] = vector[i + 1];
    }
}

int isDuplicate(int position, int vector[], int n){
    int i;
    for (i = 0; i < n; i++){
        if ((vector[i] == vector[position]) && (i != position)){
            return 1;
        }
    }
    return 0;
}


void displaySet(int v[], int dimension){
    printf("\n");
    if (dimension == 0){
        printf("\xed");
    }else{
        int i;
        printf("{");
        for (i = 0; i < dimension; i++){
            printf("%d, ", v[i]);
        }
        printf("\b\b}");
    }

int foo(int v[]){
    return 1;
}

}

最佳答案

底部的 foo,如所提供的代码中所示,位于 displaySet 的定义内。

关于c - "undefined reference to function"C 中的错误取决于定义的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19732537/

相关文章:

c - 我正在学习C语言所以希望他们指正我以提高

c - NOP 字符 0x90 的 printf 在 x86_64 和 i686 上编译时不同

c++ - LNK2019 和 LNK1120 具有外部文件中的功能

c++ - 为什么模板只能在头文件中实现?

捕获正在写入打开文件描述符的数据

c - Arduino:无法将 union 结构作为指针 ac 传递,我可以使用 gcc 编译器

c++ - 对 'vtable for xxx' 的 undefined reference

c - 使用链接器命令将 C 中的数组分配到特定位置

c - GCC:对 xxx 的 undefined reference

c++ - 在 C++ 中包含 header 时 undefined reference