c - 未定义的函数引用和 ld 返回 1 退出状态错误。没有语法错误。

标签 c compiler-errors

我知道这个问题已经被问过很多次了,我已经尝试了所有已发布的解决方案,但对我来说,它似乎不起作用。不知道是编译器的问题还是我的方法不对。

我正在从我创建的“bin.txt”文件中获取数据。它仅包含 0 和 1 的 3x3 矩阵(两位数之间没有任何空格)。我想计算那里有多少个 0 和 1,据此,我想创建一个新的文本文件(在我的代码“encode.txt”中)并将这些计数的数字写入该文件。

我在 CodeBlocks 上使用 GNU GCC 编译器。这是我的代码,如果您发现拼写错误,我真的很抱歉。我不想在这上面浪费你的时间。对此的任何帮助和提示表示赞赏。

#include <stdio.h>
#include <stdlib.h>
#define row 3
#define col 3

int foo(int x, int y);

int main()
 {
   FILE *ptr;
   FILE *encode;
   int i,j,zero=0,one=0;
   int a[row][col];
   ptr=fopen("bin.txt","r");
   encode = fopen("encode.txt","w");

for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        fscanf(ptr,"%1d",&a[i][j]);
        if(a[i][j]==0)
        {
            zero++;
        }
        else
        {
            one++;
        }
    }
    foo(zero, one);
    zero=0;
    one=0;
}

/*----------------Print scaned file----------------------------------*/
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        printf("%1d",a[i][j]);
    }
    printf("\n");
}
/*----------- Foo function ----------*/

int foo(int x, int y)
 {
   fprintf(encode,"%d %d",x,y);
   fprintf(encode,"\n");
 }

/*----------- Closing file ----------*/
  fclose(ptr);
  fclose(encode);

  return 0;
  }

构建日志:

mingw32-g++.exe  -o bin\Debug\file_compress.exe obj\Debug\main.o   
obj\Debug\main.o: In function `main':
E:/Code/C/File compression/file_compress/main.c:31: undefined reference to 
`foo'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))

最佳答案

标准 C 中不允许嵌套函数(尽管 GCC 提供了 extension)。无论如何,您可能并不打算使用嵌套函数;将 foo() 函数的定义移到 main() 函数之外。

关于c - 未定义的函数引用和 ld 返回 1 退出状态错误。没有语法错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258710/

相关文章:

java - 如何使用构造函数来打印每本书的主题,颜色和页面?

jquery - 在初始化之前无法调用工具提示上的方法

c - recvfrom 调用时出现段错误

c - Linux 中进程的互斥量

C如何将结构数组序列化为char数组?

c - glibc中带有__NR_前缀的符号是什么?

c++ - 如何在 header 和 cpp 之间拆分静态/模板类?

visual-studio-2015 - CPLEX 12.8,Visual Studio 2015错误: Cannot open include file

c - 为什么伪随机数生成器只有 0.000000?

GCC 不同部分中的常量指针