英文:
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("\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?
</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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论