Suzana
Suzana

Reputation: 67

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 ?

#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;
}

Upvotes: 0

Views: 296

Answers (3)

Mike
Mike

Reputation: 49473

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). Then run make in that directory.

Note 1: white space is important in Makefiles, the command to build gcc ... should be 1 <tab> indented

Note 2: this is just a simple example, you can (should) modifiy the build command with your own flags. -Wall would be a good one to put in.

Note 3: Makefiles are a huge topic. Make sure you read up about them: http://www.gnu.org/software/make/manual/

Upvotes: 4

Sunil Bojanapally
Sunil Bojanapally

Reputation: 12676

Assuming no make configured settings prior,

# 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.

Upvotes: 2

Frerich Raabe
Frerich Raabe

Reputation: 94489

You don't need any Makefile at all for that. Assuming that your source file is stored as foo.c, just run

make foo

The default Makefiles will kick in, executing

cc foo.c -o foo

Upvotes: 5

Related Questions