由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - gcc 编译的时候要包括 header source file 吗?
相关主题
另一个Fortran 问题最基本的C语言编程问题请教
forward declarationWhy should i include .cpp instead of .h
问低级问题怎样include一个函数
【请教】mpicc 和 mpiCC编译问题弱问c++里有没有NULL这个keyword?
C code参数传递出错可能的原因is there any lib can read .Z file in c++/c program?
怎么样最好的编译不同文件在同一个VC project里面?弱问一个ifdef 和ifndef
global variable usage question in C++c++,这种做法不行?
两个class的交叉引用问题又一个初级问题: C++中多如牛毛的#define格式
相关话题的讨论汇总
话题: pphh话题: file话题: include话题: void话题: source
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
又一个初级问题: C++中多如牛毛的#define格式C code参数传递出错可能的原因
问个两个.h文件互相include的问题怎么样最好的编译不同文件在同一个VC project里面?
弱问C++一个问题 一直不解global variable usage question in C++
C 里面有办法永久改变一个指针的属性吗?两个class的交叉引用问题
另一个Fortran 问题最基本的C语言编程问题请教
forward declarationWhy should i include .cpp instead of .h
问低级问题怎样include一个函数
【请教】mpicc 和 mpiCC编译问题弱问c++里有没有NULL这个keyword?
相关话题的讨论汇总
话题: pphh话题: file话题: include话题: void话题: source