0

My Code : A Program to Print from a to z
My file name : ft_print_alphabet.c

#include <unistd.h>

int ft_putchar(char c)
{
    write(1, &c, 1);
}

void ft_print_alphabet(void)
{
    char c;
    c = 'a';
    while (c <= 'z')
    {
        ft_putchar(c);
        c++;
    }
}

My Issue:

I use the MinGW & Vim editor and I get the following error while I compile in MinGW.

$ gcc ft_print_alphabet.c
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

I understand that the compiler doesn't know from where it should start the program since it couldn't locate void main() in the code.

My code works fine when I replace ft_print_alphabet(void) with void main(void) but please note this is the standard way of coding where I am going to apply for a c coding camp.

This code and the function ft_print_alphabet(void) 'must' be used. I know there are other simple ways to do the same code but please note I'm not looking for using the function ft_print_alphabet(void).

How can I execute the program without adding void main() instead of void ft_print_alphabet(void).

I need my program to start executing from the function void ft_print_alphabet(void). How can I do that please? Is there any command like:

gcc ft_print_alphabet.c | ft_print_alphabet ?

gcc ft_print_alphabet.c ft_print_alphabet ?

Something like this?

Lundin
  • 195,001
  • 40
  • 254
  • 396
Ajo Mathew
  • 101
  • 7
  • "the function `ft_print_alphabet(void)` 'must' be used" - AFAIK, `main` also _must_ be defined. In fact, your libC is most likely hard-coded to call the `main` function when launching any executable. – ForceBru Feb 04 '21 at 10:12
  • also note that it is not `void main()`, it is `int main()`. – mch Feb 04 '21 at 10:14
  • I tested that the answer by Ciro Santilli on the linked thread works in mingw-w64 – M.M Feb 04 '21 at 10:31
  • Have you tried `gcc -Dft_print_alphabet=main ft_print_alphabet.c`? – ssbssa Feb 04 '21 at 11:36

0 Answers0