【问题标题】:how to make Makefile to compile the multiple c files and h files in one directory如何使Makefile编译一个目录下的多个c文件和h文件
【发布时间】:2015-10-08 07:11:21
【问题描述】:

我想编写一个 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 --版本 配置: --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。谢谢。

【问题讨论】:

  • “我觉得 Mac OX 与 other linux 系统有很大的不同”。 OS-X 不是 Linux 系统!

标签: c++ c macos gcc makefile


【解决方案1】:

问题在于你的规则是string.o

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

配方实际上并没有创建目标文件。我怀疑你想使用-c 而不是-o,或者可能使用-c -o string.o

另外,虽然将 headers 列为先决条件是完全合理的,但在编译命令中列出它们是不正常的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多