【问题标题】:Sepia Filter undefined reference to main?棕褐色过滤器未定义对主要的引用?
【发布时间】:2020-04-27 22:27:15
【问题描述】:

我正在做一个项目,但由于某种原因,我的代码无法编译并打印错误消息。

''' /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../x86_64-linux-gnu/crt1.o: In function 
`_start': (.text+0x20): undefined reference to `main' clang-7: error: linker command failed 
with exit code 1 (use -v to see invocation) <builtin>: recipe for target 'helpers' failed 
make: *** [helpers] Error 1 '''

强调

undefined reference to 'main'. 

我没有发现任何问题,这是我的代码,我将不胜感激。

我的代码

void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    // over height
    for (int h = 0; h < height; h++)
    {
        // over width
        for ( int w = 0; w < width; w++)
        {
            int sepiaRed = .393 *  image[h][w].rgbtRed + .769 *  image[h][w].rgbtGreen + .189 *  image[h][w].rgbtBlue;
            int sepiaGreen = .349 *  image[h][w].rgbtRed + .686 *  image[h][w].rgbtGreen + .168 *  image[h][w].rgbtBlue;
            int sepiaBlue = .272 *  image[h][w].rgbtRed + .534 *  image[h][w].rgbtGreen + .131 *  image[h][w].rgbtBlue;
            // space
            if (sepiaRed > 255 || sepiaGreen > 255 || sepiaBlue > 255)
            {
                sepiaRed = 255;
                sepiaGreen = 255;
                sepiaBlue = 255;
            }

            image[h][w].rgbtRed = (sepiaRed);
            image[h][w].rgbtBlue = (sepiaBlue);
            image[h][w].rgbtGreen = (sepiaGreen);
        }
    }
    return;
}

【问题讨论】:

  • 如果你的程序是一个独立的可执行文件,它需要一个main 例程,而你的代码没有。如果您的代码要与提供main 例程的其他代码一起使用,您必须链接到该代码。为了帮助您,我们必须知道您想要实现什么以及如何构建您的程序。
  • (不相关的旁注:您应该单独检查组件的范围溢出。现在的方式是,如果三个组件中的任何一个超过最大值,您的代码将显示一个白色像素。)跨度>

标签: c


【解决方案1】:

您已经确定了主要问题:undefined reference to _main_ 每个构建到应用程序(独立可执行文件)中的 C 程序根据定义都需要一个。这个简单的可以让你在这里成功构建你的可执行文件:

鉴于结构定义为:

typedef struct {
    double rgbtRed;
    double rgbtGreen;
    double rgbtBlue;
}RGBTRIPLE;  

RGBTRIPLE image[3][4];//due to the design of sepia, this must be globally defined.

那么最简单的可以编译而不会出错的主函数是:

int main(void)
{
    sepia(3, 4, image);

    return 0;
}

如 cmets 中所述,需要对函数 sepia 进行一些调整。比如那句话:

int sepiaRed = .393 ... 

使用int 类型来存储double 值。这将编译,但会导致运行时错误。将所有这些语句的 int 更改为 double 是一个开始。但也有其他问题。

如果您需要更多帮助,请发表评论,或使用调试器浏览代码以查看错误并根据需要进行调整。

例如,对于一个未初始化的image 结构,这个语句的结果是什么:

double sepiaRed = .393 *  image[h][w].rgbtRed + .769 *  image[h][w].rgbtGreen + .189 *  image[h][w].rgbtBlue;

【讨论】:

    猜你喜欢
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多