简体   繁体   中英

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

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

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/

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.

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