C语言 如何在具有头文件的 gcc 中编译 C 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5165548/
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
How to compile a C program in gcc which has header files?
提问by Ritesh Banka
I want to compile a C program in gcc which has my 2 header files.
我想在 gcc 中编译一个 C 程序,它有我的 2 个头文件。
I am using the command:
我正在使用命令:
gcc UDP_Receive.c -o UDP_Receive -lm
gcc UDP_Receive.c -o UDP_Receive -lm
to compile it but I get an error stating "UDP_Data.h: No such file or directory"
编译它,但我收到一条错误消息,指出“UDP_Data.h:没有这样的文件或目录”
How can I tell the compiler to include these header files?
如何告诉编译器包含这些头文件?
Header Files:
头文件:
#include "UDP_Data.h"
#include "Crypt.h"
#include "UDP_Data.h"
#include "Crypt.h"
Thanks, Ritesh
谢谢,瑞特什
回答by Erik
Use -Idirectoryto add include paths, or make your #includestatement use relative paths.
使用-Idirectory添加包含路径,或者使你的#include声明使用相对路径。
EDIT:
Also be aware that #includefilenames are case sensitive on many platforms.
编辑:另请注意,#include文件名在许多平台上都区分大小写。
EDIT2:
Use #include "UDP_Data.h"not #include <UDP_Data.h>
EDIT2:#include "UDP_Data.h"不使用#include <UDP_Data.h>
回答by Skizz
You have told the compiler to include that file, with a line like this:
您已经告诉编译器包含该文件,如下所示:
#include "UDP_Data.h"
the problem is that the compiler can't find that file, and don't forget that some platforms are case sensitive when it comes to filenames so "UDP_data.h" is not the same file as "UDP_Data.h". The compiler will serach in a few places by default, but you will need to add extra directories to its search by using command line options. The exact option will depend on the compiler, for gcc it's:
问题是编译器找不到那个文件,不要忘记一些平台在文件名方面区分大小写,所以“UDP_data.h”与“UDP_Data.h”不是同一个文件。默认情况下,编译器会在几个地方进行搜索,但您需要使用命令行选项向其搜索添加额外的目录。确切的选项将取决于编译器,对于 gcc,它是:
-I<directory>

