C语言 header.h:即使源和头在同一目录中,也没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28475125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
header.h: No such file or directory even though source and header are in same directory
提问by BRHSM
I have made a header and a source but I don't know how to link them up. I looked it up on the web but the commands provided didn't work (or I wouldn't be here :) ).
我已经制作了标题和来源,但我不知道如何将它们链接起来。我在网上查了一下,但提供的命令不起作用(或者我不会在这里:))。
To compile it (if you use GCC):
编译它(如果你使用 GCC):
Header:
标题:
$ gcc -c whatever.h -o whatever.o
Source:
来源:
$ gcc -c sample.c -o sample.o
To link the files to create an executable file:
要链接文件以创建可执行文件:
$ gcc sample.o whatever.o -o sample
What did I do wrong. I am using geany for writing (compile error is here) but the commands are executed on a terminal in the same directory. can anybody give me the build commands for geany so whenever I want to include a header I can just compile and run?
我做错了什么。我正在使用 geany 进行写入(此处是编译错误),但命令是在同一目录中的终端上执行的。任何人都可以给我 geany 的构建命令,所以每当我想包含一个头文件时,我都可以编译和运行吗?
回答by Gopi
Good and the right way would be to
好的和正确的方法是
sample.c
样本.c
#include "header.h"
and compile
并编译
gcc sample.c -o ob
回答by Sourav Ghosh
Thumb Rule:
拇指规则:
- header files [
.h] are for#includeing - source files [
.c] are for compiling and linking together to create the executable.
- 头文件 [
.h] 用于#includeing - 源文件 [
.c] 用于编译和链接在一起以创建可执行文件。
Once you've #included your header file in a .cfile, there's no need to compile the header file and produce an object file.
一旦#include将头文件放入.c文件中,就无需编译头文件并生成目标文件。
FYI, you can check the effect of #include-ing the header file by running
仅供参考,您可以#include通过运行检查-ing 头文件的效果
gcc -E sample.c
and hope you'll understand why you need notcompile and link the header file separately.
希望您能理解为什么不需要单独编译和链接头文件。
EDIT:
编辑:
if you have a sample.cand whatever.h, to produce and run the binary, simply do
如果您有sample.cand whatever.h,要生成和运行二进制文件,只需执行
#include "whatever.h"in the top ofsample.cgcc -o sample sample.c./sample
#include "whatever.h"在顶部sample.cgcc -o sample sample.c./sample

