如何使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。应用程序/内容/开发/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
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
我觉得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
。
另外,虽然将头文件作为先决条件列出是完全合理的,但在编译命令中列出它们是不正常的。
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 挂起和取消挂起一个文件DLL
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何打开并写入一个名称取自C++中字符串的文件
- 构建一个由C和C++文件组成的库
- c++多进程编写一个唯一的文件
- 如何使用CLion在Mac上创建一个新的.txt文件
- 如何将class.cpp和class.hpp编译为一个.o文件
- 写入文件会产生一个空文件或根本没有文件
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- Pantheios 不使用 MFC 应用程序写入一个文件(一个 exe + 两个 DLL)
- 在C++中一次读取一个文件一个int
- 输入/输出文件(一个字母的大写)
- 如何使用两个预编译的头文件.一个用于托管代码,另一个用于非托管代码.xxx.PCH不是一个有效的预编译头文件