[英]How can I create a makefile for my C project?
this is my c project it is exactly simple linux shell I run this program in linux I want make makefile for my program .I want simple makefile learn me how can i make it ? 这是我的c项目,它是完全简单的linux shell,我在linux中运行此程序,我想要为我的程序制作makefile。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#define BUFFER_SIZE 1<<16
#define ARR_SIZE 1<<16
void sig_had(int signo)
{
puts ("This is my signal handling ..!");
}
void parse_args(char *buffer, char** args,
size_t args_size)
{
char *buf_args[args_size];
char **cp;
char *wbuf;
size_t i, j;
wbuf=buffer;
buf_args[0]=buffer;
args[0] =buffer;
for(cp=buf_args; (*cp=strsep(&wbuf, " \n\t")) != NULL ;){
if ((*cp != NULL) && (++cp >= &buf_args[args_size]))
break;
}
for (j=i=0; buf_args[i]!=NULL; i++){
if(strlen(buf_args[i])>0)
args[j++]=buf_args[i];
}
}
int main(int argc, char *argv[], char *envp[]){
char buffer[BUFFER_SIZE];
char *args[ARR_SIZE];
int status;
size_t nargs;
pid_t child_pid;
signal(SIGCHLD,sig_had);
while(1){
printf("COMMAND ");
fgets(buffer,BUFFER_SIZE,stdin);
parse_args(buffer, args, ARR_SIZE);
child_pid = fork();
if (child_pid){
child_pid = wait(status);
} else {
execvp(args[0], args);
}
}
return 0;
}
You don't need any Makefile at all for that. 您根本不需要任何Makefile。 Assuming that your source file is stored as foo.c
, just run 假设您的源文件存储为foo.c
,只需运行
make foo
The default Makefiles will kick in, executing 默认的Makefile将启动并执行
cc foo.c -o foo
OK... if you want to generate a makefile for this then type: 确定...如果要为此生成一个生成文件,请键入:
all: yourfilename.c
gcc yourfilename.c -o yourexename
Into a file named "Makefile" (no extension) placed the same as your .c
file(s). 放入与您的.c
文件相同的名为“ Makefile”的文件(无扩展名)。 Then run make
in that directory. 然后在该目录中运行make
。
Note 1: white space is important in Makefiles, the command to build gcc ...
should be 1 <tab>
indented 注1:空白在Makefiles中很重要,构建gcc ...
的命令gcc ...
应该缩进1 <tab>
Note 2: this is just a simple example, you can (should) modifiy the build command with your own flags. 注意2:这只是一个简单的示例,您可以(应该)使用自己的标志修改build命令。 -Wall
would be a good one to put in. -Wall
将是一个不错的选择。
Note 3: Makefiles are a huge topic. 注意3: Makefile是一个巨大的话题。 Make sure you read up about them: http://www.gnu.org/software/make/manual/ 确保您已阅读有关它们的信息: http : //www.gnu.org/software/make/manual/
Assuming no make
configured settings prior, 假设之前没有make
配置的设置,
# gcc to compile source files.
CC = gcc
# linker is also "gcc". may be something else with other compilers.
LD = gcc
# Compiler flags go here.
CFLAGS = -g -Wall
# Linker flags go here.
LDFLAGS =
# list of generated object files.
OBJS = hello.o
# program executable file name.
EXEC = exec
all: $(EXEC)
# rule to link the program
$(EXEC): $(OBJS)
$(LD) $(LDFLAGS) $(OBJS) -o $(EXEC)
hello.o: hello.c
$(CC) $(CFLAGS) -c hello.c
As long as you have only .c file creating one executable binary you no need anything more. 只要您只创建一个可执行二进制文件的.c文件,您就不再需要其他任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.