Redian新闻
>
gcc 编译的时候要包括 header source file 吗?
avatar
gcc 编译的时候要包括 header source file 吗?# Programming - 葵花宝典
s*1
1
gcc 编译的时候要包括 header source file 吗?
main.c 的程序中引用了sub.h 和 sub.c 中定义的function pphh(),
***************************************************************
//File: main.c
#include
#include "sub.h"
void main()
{
pphh();
}
*************************************************
//File: sub.h
#ifndef _sub_h
#define _sub_h
void pphh();
#endif
************************************************
//File: sub.c
#include
#include "sub.h"
void pphh()
{
printf("pphh\n");
}
************************
avatar
k*f
2
sub.c is not a header file. sub.h is.
You need to understand that your executable depends on two source files:
main.c and sub.c. This allows you to separate your work into smaller chunks,
as well as to build reusable libraries.
The error message you got is not a compiler error, but a linker error.
Just wonder what makes you not want to put both source files on the cc line.
If you have more files, you should consider at least use a shell script, or
better, use a make file.
avatar
s*1
3
Thanks for your reply.
为什么在main.c用standard library 中的函数,例如printf时,只需在main.c 中
#include , 在compile 时完全不必提及printf 所在的 .c source file???

chunks,
line.
or

【在 k**f 的大作中提到】
: sub.c is not a header file. sub.h is.
: You need to understand that your executable depends on two source files:
: main.c and sub.c. This allows you to separate your work into smaller chunks,
: as well as to build reusable libraries.
: The error message you got is not a compiler error, but a linker error.
: Just wonder what makes you not want to put both source files on the cc line.
: If you have more files, you should consider at least use a shell script, or
: better, use a make file.

avatar
t*t
4
there is a default library called libc

【在 s********1 的大作中提到】
: Thanks for your reply.
: 为什么在main.c用standard library 中的函数,例如printf时,只需在main.c 中
: #include , 在compile 时完全不必提及printf 所在的 .c source file???
:
: chunks,
: line.
: or

avatar
k*f
5

As pointed out by thrust, the implementation of printf is in a binary
library, which is implicitly included in the link stage. Had you compile sub
.c separately into sub.o, you can use the following to create main:
cc main.c sub.o -o main
Here the implementation of the functions in sub.c is provided in binary
format. You can even build your own library (.a files) and link them into
main.

【在 s********1 的大作中提到】
: Thanks for your reply.
: 为什么在main.c用standard library 中的函数,例如printf时,只需在main.c 中
: #include , 在compile 时完全不必提及printf 所在的 .c source file???
:
: chunks,
: line.
: or

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。