user1323328
user1323328

Reputation: 119

how to make Makefile to compile the multiple c files and h files in one directory

I want to write a Makefile to compile multiple files in one directory. In the folder, I have 5-1.c 5-2.c 5-3.c and they are main programs. I also have the string.c string.h and types.h files. My Makefile

all: 5-1 5-2 5-3

5-1: 5-1.c getch.c
    gcc -Wall -v -g -o 5-1 5-1.c getch.c

5-2: 5-2.c getch.c
    gcc -Wall -g -o 5-2 5-2.c getch.c

// The issues happens here. I does not generate string.o
// Also, I feel the format of 5-3 does not looks correct.
// I do not know how to write it here.
5-3: 5-3.o string.o
    gcc -Wall -g -o 5-3 5-3.o string.o

string.o: string.c string.h
    gcc -Wall -g -o string.c string.h types.h

clean:
    rm -f 5-1 5-2 5-3
    rm -rf *.dSYM

When I run make from the terminal, it will create 5-1, 5-2 and 5-3 as the executed programs. The issue happens on 5-3. The main program is 5-3.c, it includes the string.c.

The header file string.h includes the types.h. How to modify the gcc for 5-3 to compiles this program.

My system is Mac OX. $ gcc --version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn) Target: x86_64-apple-darwin13.4.0 Thread model: posix

My string.c files

#include "string.h"

char *strcat(char *dst, const char *src)
{
    char* cp = dst;

    while(*cp) {
        cp++;
    }

    while(*src)
    {
        *cp++ = *src++;
    } 

    return dst;
}

My string.h file:

#ifndef _STRING_H
#define _STRING_H

#include <stdio.h>
#include "types.h"

#ifndef NULL
#define NULL 0
#endif

char   *strcat(char *, const char *);

#endif /* _STRING_H */

My types.h files:

#ifndef _SIZE_T_DEFINED
#define _SIZE_T_DEFINED
    typedef unsigned int uint32_t;
#endif

I feel so much difference the Mac OX with other linux system. Please advise if anyone can write a correct Makefile based on these. Thanks.

Upvotes: 1

Views: 1062

Answers (1)

John Bollinger
John Bollinger

Reputation: 181824

The problem is with your rule for making string.o:

string.o: string.c string.h
    gcc -Wall -g -o string.c string.h types.h

The recipe does not in fact create the target file. I suspect you wanted to use -c instead of -o, or possibly to use -c -o string.o.

Also, although it is perfectly reasonable to list headers as prerequisites, it is abnormal to list them in the compilation command.

Upvotes: 1

Related Questions