由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教有关header file的几个问题
相关主题
关于C++ STL编译的疑问最初级的白痴C++问题
Question about friend in C++C语言重复定义问题
ask a question about compile shared library using libtool基础问题:在header里面define function
一道面试怪题C++. (转载)C语言教材求推荐
forward declaration问个 c++ include 问题
关于Makefile的一个问题Qt Creator
怎样include一个函数加拿大是码工的祖国,Java,Php,Unix之父都是加拿大人 (转载)
Question about Intel C++ compiler 求教
相关话题的讨论汇总
话题: file话题: header话题: include话题: c++话题: compile
进入Programming版参与讨论
1 (共1页)
s********1
发帖数: 581
1
请教有关header file的几个问题
假设sub.h 和 sub.c 是header file 和与其对应的source file.
(1) 在sub.c开始都有 #include "sub.h"
我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h"
在sub.c中,#include "sub.h" 是否多余??
(2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其
对应的source file 名字可以完全不一样?
(3) 又试着去掉.h, 并把heade file 的名字改成:
sub.h ----〉 subbbbb.c
再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。
.c 和 .h file 到底有没有不同?
(4) 将#include "subbbbb.c" (或者#include "subbbbb.h", #include "sub.h")
c*****t
发帖数: 1879
2
You really should get a C/C++ book (such as K&R) and read it. This
board is not a good place for learning the extreme basics. It is a
place to ask questions after you get at least some basic understandings
of the languages.
p.s. It is rude and in violation the BBS rule to spam your post in
more than 3 boards.


题。

【在 s********1 的大作中提到】
: 请教有关header file的几个问题
: 假设sub.h 和 sub.c 是header file 和与其对应的source file.
: (1) 在sub.c开始都有 #include "sub.h"
: 我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h"
: 在sub.c中,#include "sub.h" 是否多余??
: (2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其
: 对应的source file 名字可以完全不一样?
: (3) 又试着去掉.h, 并把heade file 的名字改成:
: sub.h ----〉 subbbbb.c
: 再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。

s********1
发帖数: 581
3
You should not respond to other people's question in this manner. You may
well ignore it if you are not interested. My question is carefully proposed.
I asked some engineers about it they were not sure either. It is not a spam
as you called it.

【在 c*****t 的大作中提到】
: You really should get a C/C++ book (such as K&R) and read it. This
: board is not a good place for learning the extreme basics. It is a
: place to ask questions after you get at least some basic understandings
: of the languages.
: p.s. It is rude and in violation the BBS rule to spam your post in
: more than 3 boards.
:
: 其
: 题。

h***z
发帖数: 233
4
Including a file A in another file B is equivalent to copying the contents
of A and pasting it in B. All included files are treated the same, no
matter what their names are.


题。

【在 s********1 的大作中提到】
: 请教有关header file的几个问题
: 假设sub.h 和 sub.c 是header file 和与其对应的source file.
: (1) 在sub.c开始都有 #include "sub.h"
: 我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h"
: 在sub.c中,#include "sub.h" 是否多余??
: (2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其
: 对应的source file 名字可以完全不一样?
: (3) 又试着去掉.h, 并把heade file 的名字改成:
: sub.h ----〉 subbbbb.c
: 再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。

k**f
发帖数: 372
5
I think your questions are follow-ups to your earlier post.

Yes. sub.h is not necessary for sub.c to compile.

Yes.
题。
The extension of the header file can be anything. In fact, C++ standard
header files have no extension, e.g.
Now the real answer to your questions. Header files usually contain the
prototype of functions and definitions of data types that are shared among
source files.
Declaring function prototype is necessary in ANSI C. Using the same header
file allows the compiler

【在 s********1 的大作中提到】
: 请教有关header file的几个问题
: 假设sub.h 和 sub.c 是header file 和与其对应的source file.
: (1) 在sub.c开始都有 #include "sub.h"
: 我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h"
: 在sub.c中,#include "sub.h" 是否多余??
: (2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其
: 对应的source file 名字可以完全不一样?
: (3) 又试着去掉.h, 并把heade file 的名字改成:
: sub.h ----〉 subbbbb.c
: 再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。

y****e
发帖数: 23939
6
其实他说得没错,你的这些问题确实非常那个基本,
名字没有关系的,有的平台上还把头文件叫 .hpp
你说的那些engineers是做C/C++的吗,如果是的话,他们都应该被fired

proposed.
spam

【在 s********1 的大作中提到】
: You should not respond to other people's question in this manner. You may
: well ignore it if you are not interested. My question is carefully proposed.
: I asked some engineers about it they were not sure either. It is not a spam
: as you called it.

O******e
发帖数: 734
7
Take coconut's advice, spend the money and get a C or C++ book. Given that
you know nothing about programming, any book with a lot of example would
do. Kernighan and Ritchie (suggested by coconut) is the standard reference
for all C programmers, but judging from the type of questions you have been
asking, it might not be suitable for you because it really is a reference
book with few examples and not a textbook with many illustrative examples.
Without a book, you are trying to learn how to pro

【在 s********1 的大作中提到】
: You should not respond to other people's question in this manner. You may
: well ignore it if you are not interested. My question is carefully proposed.
: I asked some engineers about it they were not sure either. It is not a spam
: as you called it.

O******e
发帖数: 734
8

If the contents of sub.h are not needed by sub.c, then #include "sub.h"
is redundant.

You can call the header file by any name you want.
题。
You can call the header file by any name you want, although some
preprocessors might make certain assumptions regarding the .h or .c
file extensions.
(4) 将#include "subbbbb.c" (或者#include "subbbbb.h", #include "sub.h")
完全从main program里除去, 只compile main program 和sub.c。
在linux 上compile,run 一切正常。 visual studio 2005 上也只在compile 时给

warning, run

【在 s********1 的大作中提到】
: 请教有关header file的几个问题
: 假设sub.h 和 sub.c 是header file 和与其对应的source file.
: (1) 在sub.c开始都有 #include "sub.h"
: 我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h"
: 在sub.c中,#include "sub.h" 是否多余??
: (2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其
: 对应的source file 名字可以完全不一样?
: (3) 又试着去掉.h, 并把heade file 的名字改成:
: sub.h ----〉 subbbbb.c
: 再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。

t****t
发帖数: 6806
9
别人回你就不错了, 你还挑三拣四的
刚才有人说得不错, 你问的那些"engineers", 如果是C/C++的程序员, 那他们都该被炒
鱿鱼

proposed.
spam

【在 s********1 的大作中提到】
: You should not respond to other people's question in this manner. You may
: well ignore it if you are not interested. My question is carefully proposed.
: I asked some engineers about it they were not sure either. It is not a spam
: as you called it.

1 (共1页)
进入Programming版参与讨论
相关主题
Intel C++ compiler 求教forward declaration
弱问C++一个问题 一直不解关于Makefile的一个问题
求助:怎么能support头文件#include 怎样include一个函数
which header file declares ntohs() in c++ ?Question about
关于C++ STL编译的疑问最初级的白痴C++问题
Question about friend in C++C语言重复定义问题
ask a question about compile shared library using libtool基础问题:在header里面define function
一道面试怪题C++. (转载)C语言教材求推荐
相关话题的讨论汇总
话题: file话题: header话题: include话题: c++话题: compile