简体   繁体   中英

Creating a makefile for C program

Can anybody please help me to create a makefile for this program? I've tried to to one but it does not work... Thank you!

#include <string.h>
#include <stdlib.h>
#include "MakeArg.c"

int main()   
{   
char cmdline[256];   
char **argv;   

printf("doit# ");   

for (;;)   
{      
    scanf("%s",cmdline);   
    if (strcmp(cmdline,"q")==0)   
        break;   
    else   
    {   
        printf("doit# ");    
        if (fork()==0)   
        {   
            if (MakeArg(cmdline," ",&argv)>0)   
                execvp(argv[0],argv);   
        }   
        wait(NULL);   
    }   
 }   
 exit(0);   
}   

Here is what I have for now, but it does not work:

CC         = gcc 
CFLAGS     = ­-c -­Wall -­ansi ­-pedantic 
SOURCES    = MakeArg.c doit.c 
OBJECTS    = $(SOURCES:.c=.o)  
EXECUTABLE = doit
all: $(EXECUTABLE) 
$(EXECUTABLE): $(OBJECTS) 
$(CC) -­o $@ $(OBJECTS) 
%.o: %.c $(HEADER) 
$(CC) $(CFLAGS) -­o $@ $< 
clean: 
rm ­f *.o $(EXECUTABLE)

Just a few errors:

1) You will also need to include the following headers in the file you have posted:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

2) In your clean target in your makefile, you are missing the '-' in front of 'f'. Should be rm -f.

3) Also, make sure you have a tab character(instead of spaces) before each recipe in the Makefile. It's hard to tell if you did(or didn't do) this from your post.

4) Also, it's not recommended to include a .c file.

Try something like this:

CC         = gcc
CFLAGS     = ­-­Wall -­ansi ­-pedantic 
SOURCES    = MakeArg.c doit.c
OBJECTS    = $(SOURCES:.c=.o)  
EXECUTABLE = doit

all: $(EXECUTABLE) 

$(EXECUTABLE): $(OBJECTS) 
    $(CC) -o $@ $^

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@

clean: 
    $(RM) ­-f $(OBJECTS) $(EXECUTABLE)

//

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "MakeArg.h"

int main()   
{   
char cmdline[256];   
char **argv;   

printf("doit# ");   

for (;;)   
{      
    scanf("%s",cmdline);   
    if (strcmp(cmdline,"q")==0)   
        break;   
    else   
    {   
        printf("doit# ");    
        if (fork()==0)   
        {   
            if (MakeArg(cmdline," ",&argv)>0)   
                execvp(argv[0],argv);   
        }   
        wait(NULL);   
    }   
 }   
 exit(0);   
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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