STM32.循序渐进Makefile(1)

 

首先找教程,以前我印象里有一个文章,找到了,如下:

如何系统地学习 Makefile 相关的知识(读/写)?

而我用的是STM32,当然用的是armcc编译器(为什么不用gcc呢?原因是我刚开始用gcc编译一个C文件出现问题了,哦  刚才怎么又好了!)

现在就按照上面的这个链接在gcc和armcc上来一把。

首先把例子中的链接稍微简化一下,就是把printf这个给删掉,因为涉及到库

---------------------------------------------------------------------------------

 

第一版Makefile


//fun1.c 
void fun1() 

//printf("this is fun1\n"); 

//fun2.c 
void fun2() 

//printf("this is fun2\n"); 
}


//main.c 
//#include "stdio.h"
int main(void) 

//    printf("hello world\n"); 
    fun1(); 
    fun2(); 
    return 0;

john@DESKTOP-AT0PVKR MINGW64 /e/microPython/Makefile
$ armcc
ARM C/C++ Compiler, 5.03 [Build 76] [MDK-ARM Standard]

Usage:         armcc [options] file1 file2 ... filen
Main options:

--arm          Generate ARM code
--thumb        Generate Thumb code
--c90          Switch to C mode (default for .c files)
--cpp          Switch to C++ mode (default for .cpp files)
-O0            Minimum optimization
-O1            Restricted optimization for debugging
-O2            High optimization
-O3            Maximum optimization
-Ospace        Optimize for codesize
-Otime         Optimize for maximum performance
--cpu <cpu>    Select CPU to generate code for
--cpu list     Output a list of all the selectable CPUs
--device <dev> Set the target device type
--device list  Output a list of all the selectable devices
-o <file>      Name the final output file of the compilation
-c             Compile only, do not link
--asm          Output assembly code as well as object code
-S             Output assembly code instead of object code
--interleave   Interleave source with disassembly (use with --asm or -S)
-E             Preprocess the C source code only
-D<symbol>     Define <symbol> on entry to the compiler
-g             Generate tables for high-level debugging
-I<directory>  Include <directory> on the #include search path
Software supplied by: ARM Limited


john@DESKTOP-AT0PVKR MINGW64 /e/microPython/Makefile
$ armcc main.c -o app
"main.c", line 9: Warning:  #223-D: function "fun1" declared implicitly
        fun1();
        ^
"main.c", line 10: Warning:  #223-D: function "fun2" declared implicitly
        fun2();
        ^
Error: L6218E: Undefined symbol fun1 (referred from main.o).
Error: L6218E: Undefined symbol fun2 (referred from main.o).
Finished: 0 information, 0 warning and 2 error messages.
main.c: 2 warnings, 0 errors

john@DESKTOP-AT0PVKR MINGW64 /e/microPython/Makefile
$
 

仅仅生成了main.o

 

哦,原来是忘记编译fun1.c  fun2.c了。


john@DESKTOP-AT0PVKR MINGW64 /e/microPython/Makefile
$ armcc main.c fun1.c fun2.c -o app
"main.c", line 12: Warning:  #223-D: function "fun1" declared implicitly
        fun1();
        ^
"main.c", line 13: Warning:  #223-D: function "fun2" declared implicitly
        fun2();
        ^
main.c: 2 warnings, 0 errors

john@DESKTOP-AT0PVKR MINGW64 /e/microPython/Makefile
$ armcc main.c fun1.c fun2.c -o app
"main.c", line 12: Warning:  #223-D: function "fun1" declared implicitly
        fun1();
        ^
"main.c", line 13: Warning:  #223-D: function "fun2" declared implicitly
        fun2();
        ^
main.c: 2 warnings, 0 errors

john@DESKTOP-AT0PVKR MINGW64 /e/microPython/Makefile
$
 

在main.c添加两个函数的函数原型,并且 参数填上void 如下:

//main.c 


//#include "stdio.h"
extern void fun1(void);
extern void fun2(void);
int main(void) 

//    printf("hello world\n"); 
    fun1(); 
    fun2(); 
    return 0;

//fun1.c 
void fun1(void) 

//printf("this is fun1\n"); 

//fun2.c 
void fun2(void) 

//printf("this is fun2\n"); 
}

armcc 编译后生成 app  fun1.o fun2.o main.o

gcc 编译后生成app.exe

下面用Makefile写,首先要知道Makefile的规则,如下:

哈哈 竟然找到良许linux的 博客了 https://blog.csdn.net/yychuyu/article/details/80013264

Makefile的基本规则为:

目标:依赖

(tab)规则

 

目标 --> 需要生成的目标文件

依赖 --> 生成该目标所需的一些文件

规则 --> 由依赖文件生成目标文件的手段

tab --> 每条规则必须以tab开头,使用空格不行

 

例如我们经常写的gcc test.c -o test,使用Makefile可以写成:

 

test: test.c

gcc test.c -o test

 

其中,第一行中的test就是要生成的目标,test.c就是依赖,第二行就是由test.c生成test的规则。

Makefile中有时会有多个目标,但Makefile会将第一个目标定为终极目标。
--------------------- 
作者:yychuyu 
来源:CSDN 
原文:https://blog.csdn.net/yychuyu/article/details/80013264 
版权声明:本文为博主原创文章,转载请附上博文链接!

然后

john@DESKTOP-AT0PVKR MINGW64 /e/microPython/Makefile
$ make
Makefile:2: *** missing separator.  Stop.
 

查找一下原因,https://blog.csdn.net/hanjuefu5827/article/details/77661095

于是改为TAB键 不用空格 就好了,如下:

john@DESKTOP-AT0PVKR MINGW64 /e/microPython/Makefile
$ make
armcc main.c fun1.c fun2.c -o app
--------

------

-----------

-----

再次看一下Makefile

app: main.c fun1.c fun2.c  
    arm-none-eabi-gcc main.c fun1.c fun2.c -o app 
    #armcc main.c fun1.c fun2.c -o app 
    #gcc main.c fun1.c fun2.c -o app

用arm-none-eabi-gcc 这个makefile出现:

exit.c:(.text.exit+0x2c): undefined reference to `_exit'
collect2.exe: error: ld returned 1 exit status
make: *** [app] Error 1
查看 《  解决arm-none-eabi-gcc交叉编译问题

折腾了一会没弄出来。。。。。。。。。。。。。。。。。

到 第一版Makefile到此为止

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值