【问题标题】:C palindrome program - undefined reference to mainC回文程序-未定义对main的引用
【发布时间】:2017-12-07 12:03:42
【问题描述】:

我写了一个函数,它检查 char 字符串是否为回文。

//pan.c

#include <stdbool.h>
#include "funs.h"
#include <stdio.h>
#include <string.h>

bool palindrom(char napis[])
{
    int begin, middle, end, length = 0;

    while(napis[length] != '\0')
      length++;

    end = length - 1;
    middle = length;

    for (begin = 0; begin < middle; begin++)
      {
        if(napis[begin] != napis[end])
        {
          return false;
          break;
        }
        end--;
      }
      if(begin == middle)
        return true;
}

我还创建了 funs.h

//funs.h
#include <stdbool.h>
bool palindrom();

现在,我正在尝试在我的主函数中使用这个函数

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "funs.h"


int main()
{
    char text[100];
    bool result;

    printf("Enter an char: ");
    scanf("%s", text);
    result = palindrom(text);
    if(result)
    {
        printf("Brawo!\n");
    }
    else
    {
        printf("Gówno!\n");
    }
    return 0;
}

我还创建了makefile:

# Makefile

all: main

main: main.o pan.o
    clang -o main main.o pan.o

main.o: main.c
    clang -c main.c

pan.o: pan.c
    clang -c pan.c

clean:
rm -f main *.o *~

一切似乎都很好,并且在单个文件中工作,但是当我尝试单独编译它们时,它们“看不到”对方。 Makefile 似乎也很糟糕,但我看不到任何错误。你能帮我修一下吗?

【问题讨论】:

  • bool palindrom(); 定义了一个可以接收任意数量参数的函数,默认为int。所以这是错误的......
  • 你在bool palindrom();声明中遗漏了一些东西,应该是bool palindrom(char*);
  • @MateuszTrzeciak:但是当我尝试单独编译它们时,它们“看不到”彼此,你在编译过程中遇到的错误是什么?
  • 我改了,还是有问题:(
  • Edit 将这些重要的诊断信息添加到您的帖子中。 “不起作用”不是问题描述。

标签: c linux makefile


【解决方案1】:

当我尝试“make”命令时,它返回“makefile:15: *** missing separator. Stop”。发表评论,什么也不做。

你真的看过你的makefile的第15行吗?请注意,它与边距齐平,而不是由制表符缩进。

当我使用“clang pan.c -Wall --pedantic -std=c11 -o pan”命令编译 pan.c 时:生成 1 个警告。 /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-l‌​inux-gnu/crt1.o:在函数_start中:(。 text+0x20): 对 main' 的未定义引用

确实,您的 pan.c 没有 main() 函数。所以不要试图自己编译它。怎么样

clang main.c pan.c -Wall --pedantic -std=c11 -o pan

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多