You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CS50第4周Filter-less代码编译报错:undefined reference to main

编译错误解决及代码逻辑修正

一、编译错误原因与修复

错误解析

你看到的undefined reference to main错误,是因为helpers.c作为功能模块文件,本身没有程序入口main()函数——CS50 Filter-less作业中,主程序逻辑(命令行参数处理、图片读写)在课程提供的filter.c文件里,helpers.c只负责实现图像处理的核心函数。

修复步骤

  1. 确认作业目录中存在课程提供的filter.c、helpers.h和Makefile文件
  2. 直接在终端执行make命令,Makefile会自动将filter.c和helpers.c链接编译成完整的可执行文件,无需单独编译helpers.c

二、代码逻辑错误修正

你的代码存在两个关键逻辑错误,会导致功能完全失效:

  1. sepia与reflect函数实现颠倒:当前sepia函数写的是水平翻转逻辑,reflect函数写的是sepia滤镜逻辑,需互换两者代码
  2. blur函数计算位置错误:RGB平均值计算代码被放在内层循环中,每次循环都会覆盖结果,需移到内层循环外

修正后的完整代码

#include "helpers.h"
#include <math.h>

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float r = image[i][j].rgbtRed;
            float g = image[i][j].rgbtGreen;
            float b = image[i][j].rgbtBlue;
            float grey = round((b + g + r)/3.0);
            image[i][j].rgbtRed = image[i][j].rgbtGreen = image[i][j].rgbtBlue = grey;
         }
    }
    return;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
             int b = image[i][j].rgbtBlue;
             int g = image[i][j].rgbtGreen;
             int r = image[i][j].rgbtRed;
             int sepiaRed = round((0.393 * r) + (0.769 * g) + (0.189 * b));
            int sepiaGreen = round((0.349 * r) + (0.686 * g) + (0.168 * b));
            int sepiaBlue = round((0.272 * r) + (0.534 * g) + (0.131 * b));

            if (sepiaRed > 255) sepiaRed = 255;
            if (sepiaGreen > 255) sepiaGreen = 255;
            if (sepiaBlue > 255) sepiaBlue = 255;

            image[i][j].rgbtBlue = sepiaBlue;
            image[i][j].rgbtGreen = sepiaGreen;
            image[i][j].rgbtRed = sepiaRed;
        }
    }
    return;
 }

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
     for (int i = 0; i < height; i++)
     {
         for (int j = 0; j < width / 2; j++)
         {
             int w = width - (j + 1);
             RGBTRIPLE tmp = image[i][j];
             image[i][j] = image[i][w];
             image[i][w] = tmp;
         }
     }
     return;
 }

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE tmp[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            tmp[i][j] = image[i][j];
        }
    }
    for (int i = 0; i < height; i++)
    {
         for (int j = 0; j < width; j++)
         {
             int totalRed = 0;
             int totalGreen = 0;
             int totalBlue = 0;
             float counter = 0.00;

             for (int x = -1; x < 2; x++)
             {
                 for (int y = -1; y < 2; y++)
                 {
                     int X = i + x;
                     int Y = j + y;

                     if (X < 0 || Y < 0 || X > (height - 1) || Y > (width - 1))
                     {
                         continue;
                     }

                     totalRed += image[X][Y].rgbtRed;
                     totalGreen += image[X][Y].rgbtGreen;
                     totalBlue += image[X][Y].rgbtBlue;

                     counter++;
                 }
             }
             // 将计算移至内层循环外
             tmp[i][j].rgbtRed = round(totalRed / counter);
             tmp[i][j].rgbtGreen = round(totalGreen / counter);
             tmp[i][j].rgbtBlue = round(totalBlue / counter);
         }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
             image[i][j].rgbtRed = tmp[i][j].rgbtRed;
             image[i][j].rgbtGreen = tmp[i][j].rgbtGreen;
             image[i][j].rgbtBlue = tmp[i][j].rgbtBlue;
        }
     }
     return;
 }

三、功能测试

编译成功后,可通过以下命令测试各滤镜功能:

./filter -g infile.bmp outfile.bmp  # 灰度滤镜测试
./filter -s infile.bmp outfile.bmp  # Sepia滤镜测试
./filter -r infile.bmp outfile.bmp  # 水平翻转测试
./filter -b infile.bmp outfile.bmp  # 模糊滤镜测试

内容的提问来源于stack exchange,提问作者John Zhang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 13:10:26