0
//main.h
#ifndef PRINTF                                                             
#define PRINTF                                                             
                                                                           
#include <stdio.h>                                                         
#include <limits.h>
                                                                           
typedef struct specifiers                                                  
{                                                                          
        char s;                                                            
        int (*func)(char *s, int i);                                       
} spec_t;                                                                  
                                                                           
int print_int(int);                                                        
int _putchar(char c);                                                      
int _puts(char *s);                                                        
int main(void);                                                            
                                                                           
#endif

//print_int.c
int print_int(int input)
{
        int tmp;
        int i;

        if (input == 0)
        {
                i = 0;
                return (i);
        }

        tmp = (input % 10) + 48;
        i = print_int(input / 10);
        putchar(tmp);
        i++;

        return (i);
}
//main.c
#include "main.h"                                                          
#include <limits.h>                                                        
#include <stdio.h>                                                        
                                                                           
int main(void)                                                             
{                                                                          
        int test = INT_MAX;                                                
                                                                           
        test = print_int(test);                                            
                                                                           
        printf("\n%i", test);                                              
                                                                           
        return (test);                                                     
}

These are compiled using: gcc -Wall -Wextra -Werror -pedantic -std=gnu89 -Wno-format main.h main.c print_int.c -o test

For some reason, these files won't link at compile time. Anyone got any ideas?

I tried putting the function prototype for int print_int(int); directly into the "main.c" file. This works, but I want all the prototypes to be inside "main.h". I don't know how to expand on this any more. Is there anything anyone can do?

Brad Brown
  • 11
  • 2
  • Why is this tagged `compiler-construction`? – Scott Hunter Jul 10 '23 at 17:54
  • How are you attempting to link this program? – Scott Hunter Jul 10 '23 at 17:55
  • I'm linking this program using gcc -Wall -Wextra -Werror -pedantic -std=gnu89 -Wno-format main.h main.c print_int.c -o test – Brad Brown Jul 10 '23 at 18:26
  • Why does `main.h` contain `#include "main.h"`? And why is there a forward declaration for `main`? AFAIK manually invoking main is UB, so there will never be a reason to link to it outside of the translation unit that defines it. – Brian61354270 Jul 10 '23 at 18:34
  • 2
    @BradBrown You should add your compilation command to your question with an [edit]. Comments are temporary. Also, do note that you don't compile header files. You can drop `main.h` from that command. – Brian61354270 Jul 10 '23 at 18:36
  • I fixed the first question @Brian61354270, that was a typo. Second, I'll drop main.h and see what happens – Brad Brown Jul 10 '23 at 18:44
  • 1
    @Brian61354270: Technically, it is legal in C (but not in C++) to call `main` recursively, see https://stackoverflow.com/questions/4238179/calling-main-in-main-in-c. But of course it would very rarely be useful or wise to do so. – Nate Eldredge Jul 10 '23 at 19:00
  • Oki, so it didn't work actually. I checked and the above code still doesn't work. It's still saying that i'm implicitly declaring print_int – Brad Brown Jul 10 '23 at 19:58

1 Answers1

1

[FIXED] Shoutouts to @Brian61354270, his suggestions of not compiling the header file (gcc -Wall -Wextra -Werror -pedantic -std=gnu89 -Wno-format main.h main.c print_int.c -o test) and dropping int main(void) fixed the issue!

Brad Brown
  • 11
  • 2