为什么我会收到一个错误消息:”隐式声明函数’print_int'”?

huangapple go评论69阅读模式
英文:

Why am I getting an: "Implicit declaration of function 'print_int'?

问题

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

<details>
<summary>英文:</summary>

//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(&quot;\n%i&quot;, 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&#39;t link at compile time. Anyone got any ideas?

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

</details>


# 答案1
**得分**: 1

修复:感谢 @Brian61354270,他建议不编译头文件(`gcc -Wall -Wextra -Werror -pedantic -std=gnu89 -Wno-format main.h main.c print_int.c -o test`)并且去掉 `int main(void)`,问题已解决!

<details>
<summary>英文:</summary>

[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!

</details>



huangapple
  • 本文由 发表于 2023年7月11日 01:48:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定