简体   繁体   English

如何使用头文件进行编译并创建一个 Makefile - C

[英]How to compile with headers and a create a Makefile - C

I am trying to compile and create a Makefile with my source code organized as per the following structure:我正在尝试使用按照以下结构组织的源代码编译和创建 Makefile:

|-- build
|-- include
| \-- functions.h
\-- src
|-- functions.c
\-- main.c

The code written there has been tested on an IDE, so I know it works.那里编写的代码已经在 IDE 上进行了测试,所以我知道它可以工作。 I just need to compile it but I don't know how to execute 2 files with extensions .c and the .h file.我只需要编译它,但我不知道如何执行 2 个扩展名为 .c 的文件和 .h 文件。 ALSO, I need to create a makefile for this.另外,我需要为此创建一个makefile。

How?怎么样?

A Makefile is just some textual file (where tab characters are significant at least when they start a command line).一个Makefile只是一些文本文件(其中,当它们启动命令行选项卡字符至少显著)。 Use some source code editor to create or improve it;使用一些源代码编辑器来创建或改进它; I prefer GNU emacs so recommend it, but you could use some other editor (eg vim , gedit , etc...), it is a matter of taste.我更喜欢GNU emacs所以推荐它,但你可以使用其他一些编辑器(例如vimgedit等...),这是一个品味问题。 BTW, don't forget to use some version control system (I recommend git ).顺便说一句,不要忘记使用一些版本控制系统(我推荐git )。

Before creating your Makefile take some time to read the documentation of make .在创建Makefile之前,花一些时间阅读make文档 Be aware of the numerous built-in rules (which you could obtain by running make -p ) and take advantage of them.请注意众多内置规则(您可以通过运行make -p获得)并利用它们。

how to execute 2 .c files and the .h file.如何执行 2 个 .c 文件和 .h 文件。

You don't execute .c files, but you feed them to your C compiler .您不执行.c文件,而是将它们提供给 C编译器 I recommend using GCC (or perhaps Clang/LLVM ) as your C compiler.我推荐使用GCC (或者Clang/LLVM )作为你的 C 编译器。

You probably should set the CC make variable (it gives the default C compiler) to gcc , and the CFLAGS variable (it gives default compiler flags).您可能应该将CC make 变量(它提供默认的 C 编译器)设置为gcc ,并将CFLAGS变量(它提供默认的编译器标志)。

So read documentation about Invoking GCC .所以请阅读有关Invoking GCC 的文档。 You surely want to compile with all warnings and debug info.您肯定希望使用所有警告和调试信息进行编译。

Hence, I recommend to have the following two lines in your Makefile因此,我建议在你的Makefile有以下两行

 CC= gcc
 CFLAGS= -Wall -Wextra -g

Of course, you need other lines in your Makefile .当然,您的Makefile还需要其他行。 I leave you to read more and experiment.我让你阅读更多并进行实验。 This and that should be inspirational.应该是鼓舞人心的。

You probably want to mention the dependency on your header file(s) in your Makefile .您可能想在Makefile提及对头文件的依赖。

So I leave you to write your Makefile .所以我让你你的Makefile Consider it as source code , so backup and version control it carefully.将其视为源代码,因此请谨慎备份和版本控制。

BTW, for a tiny program like yours, I don't think that having src/ and include/ directories is useful, and I believe that introducing a build/ directory is just adding unnecessary complexity.顺便说一句,对于像您这样的小程序,我认为拥有src/include/目录没有用,而且我相信引入build/目录只会增加不必要的复杂性。 YMMV.天啊。

The code written there has been tested on a IDE, so I know it works.那里编写的代码已经在 IDE 上进行了测试,所以我知道它可以工作。

This could be an illusion.这可能是一种错觉。 Sometimes you can have the bad luck of having some undefined behavior which simply "appears" to work but is a bug.有时,您可能会因为某些未定义的行为而倒霉,这些行为只是“看起来”有效,但实际上是一个错误。 See this answer and follow the links I gave there.请参阅此答案并按照我在那里提供的链接进行操作。

There are many free software projects built with make ;有许多使用make构建的免费软件项目; look on github and elsewhere to find some.github和其他地方寻找一些。 Sometimes Makefile s are themselves generated (eg using cmake , qmake , or autoconf ), but that is not worthwhile in your simple case.有时Makefile是自己生成的(例如使用cmakeqmakeautoconf ),但这在您的简单情况下是不值得的。

PS.附注。 Don't expect StackOverflow to do your homework.不要指望 StackOverflow 来做你的功课。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM