s********1 发帖数: 581 | 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");
}
************************ | k**f 发帖数: 372 | 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. | s********1 发帖数: 581 | 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.
| t****t 发帖数: 6806 | 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
| k**f 发帖数: 372 | 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
|
|