makefile中使用变量

一 编译需要的文件

1 file1.h

#ifndef FILE1_H_
#define FILE1_H_
#ifdef __cplusplus
    extern "C" {
       #endif
       void File1Print();
       #ifdef __cplusplus
    }
    #endif
#endif

2 file1.cpp

#include <iostream>
#include "file1.h"
using namespace std;
void File1Print(){
    cout<<"Print file1**********************"<<endl;
}

3 file2.cpp

#include <iostream>
#include "file1.h"
using namespace std;
int main(){
    cout<<"Print file2**********************"<<endl;
    File1Print();
    return 0;
}

二 在makefile中使用变量

1 makefile

OBJS = file1.o file2.o
XX = g++
CFLAGS = -Wall -O -g

helloworld : $(OBJS)
    $(XX) $(OBJS) -o helloworld

file2.o : file2.cpp file1.h
    $(XX) $(CFLAGS) -c file2.cpp -o file2.o

file1.o : file1.cpp file1.h
    $(XX) $(CFLAGS) -c file1.cpp -o file1.o

clean:
    rm -rf *.o helloworld

2 编译

[root@localhost 0404]# ll
total 16
-rw-r--r--. 1 root root 140 May  1 08:52 file1.cpp
-rw-r--r--. 1 root root 170 May  1 08:52 file1.h
-rw-r--r--. 1 root root 167 May  1 08:52 file2.cpp
-rw-r--r--. 1 root root 290 May  1 08:52 Makefile
[root@localhost 0404]# make
g++ -Wall -O -g -c file1.cpp -o file1.o
g++ -Wall -O -g -c file2.cpp -o file2.o
g++ file1.o file2.o -o helloworld
[root@localhost 0404]# ll
total 100
-rw-r--r--. 1 root root   140 May  1 08:52 file1.cpp
-rw-r--r--. 1 root root   170 May  1 08:52 file1.h
-rw-r--r--. 1 root root 26488 May 10 21:58 file1.o
-rw-r--r--. 1 root root   167 May  1 08:52 file2.cpp
-rw-r--r--. 1 root root 24136 May 10 21:58 file2.o
-rwxr-xr-x. 1 root root 31520 May 10 21:58 helloworld
-rw-r--r--. 1 root root   290 May  1 08:52 Makefile

3 说明

要设定一个变量,只要在一行的前端写下这个变量的名字,后面跟一个"="号,后面跟要设定的这个值即可。以后要引用这个变量,只写一个"$"符合,后面在括号中写上变量名。

-Wall:输出所有的警告信息。

-O :编译时进行优化。

-g:表示编译debug版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值