簡體   English   中英

如何使Makefile編譯一個目錄中的多個c文件和h文件

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

我想編寫一個Makefile來在一個目錄中編譯多個文件。 在該文件夾中,我有5-1.c 5-2.c 5-3.c,它們是主程序。 我也有string.c string.h和types.h文件。 我的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

當我從終端運行make時,它將創建5-1、5-2和5-3作為執行的程序。 問題發生在5-3。 主程序是5-3.c,其中包含string.c。

頭文件string.h包含types.h。 如何為5-3修改gcc以編譯該程序。

我的系統是Mac OX。 $ gcc --version配置為:--prefix = / Applications / Xcode.app / Contents / Developer / usr --with-gxx-include-dir = / usr / include / c ++ / 4.2.1 Apple LLVM 6.0版(clang -600.0.57)(基於LLVM 3.5svn)目標:x86_64-apple-darwin13.4.0線程模型:posix

我的string.c文件

#include "string.h"

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

    while(*cp) {
        cp++;
    }

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

    return dst;
}

我的string.h文件:

#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 */

我的types.h文件:

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

我覺得Mac OX與其他linux系統有很大的不同。 請告知是否有人可以根據這些內容編寫正確的Makefile。 謝謝。

問題在於您制作string.o的規則:

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

配方實際上不會創建目標文件。 我懷疑您想使用-c而不是-o ,或者可能是使用-c -o string.o

另外,盡管將頭文件列為先決條件是完全合理的,但是在編譯命令中列出它們是異常的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM