Home C palindrom program - undefined reference to main
Reply: 1

C palindrom program - undefined reference to main

Mateusz Trzeciak
1#
Mateusz Trzeciak Published in 2017-12-07 12:03:42Z

I wrote an function, wich checks, if string of char is palindrom or not.

//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;
}

I also created funs.h

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

Now, I'm trying to use this function in my main function

#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;
}

I also created 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 *~

Everything seems fine and works in single file, but when I try to compile them separately they "don't see" each other. Makefile also seems to work badly, but I cant see any mistakes. Can you help me fix it?

hymie
2#
hymie Reply to 2017-12-07 14:15:26Z

When I try "make" command it returns "makefile:15: *** missing separator. Stop." comment and do nothing.

Did you actually look at line 15 of your makefile? Notice that it's flush against the margin instead of indented by a tab character.

When I'm compiling pan.c with "clang pan.c -Wall --pedantic -std=c11 -o pan" command: 1 warning generated. /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-l‌​inux-gnu/crt1.o: In function _start': (.text+0x20): undefined reference to main'

Indeed, your pan.c does not have a main() function. So don't try to compile it by itself. How about

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303895 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO