文件內容
[kou@localhost makefile]$ cat 1.c
#include "3.h"
int main()
{key_t key = ftok(".",1);printf("%d\n",add(1,2));return 0;
}[kou@localhost makefile]$ cat 2.c
#include "3.h"
int add(int a, int b)
{return a + b;
}
[kou@localhost makefile]$ cat 3.h
#pragma once
#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>int add(int a,int b);
正常情況編譯
Makefile文件
all:1.o 2.ogcc 1.o 2.o -o main
1.o:1.cgcc -c 1.c -o 1.o
2.o:2.cgcc -c 2.c -o 2.o
.PHONY:clean
clean:rm -rf *.o
makfile編寫
gcc hello.c -o hello -I/home/hello/include -L/home/hello/lib -lworld
- -I /home/hello/include,表示將/home/hello/include目錄作為第一個尋找頭文件的目錄,尋找的順序是:/home/hello/include–>/usr/include–>/usr/local/include找不到的話查找默認目錄
-L /home/hello/lib,表示將/home/hello/lib目錄作為第一個尋找庫文件的目錄, 尋找的順序是:/home/hello/lib–>/lib–>/usr/lib–>/usr/local/lib
-l word , 表示**尋找動態鏈接庫文件**libword.so(也就是文件名去掉前綴和后綴所代表的庫文件)
多文件gdb
每一個gcc 后面都要-g
all:1.o 2.ogcc -g -o all 1.o 2.o
1.o:1.cgcc -c 1.c -o 1.o -g
2.o:2.cgcc -c 2.c -o 2.o -g
clean:rm -rf *orm -rf output.crm -f all