http://blog.csdn.net/shevsten/article/details/7049688
在AVR
Studio4/5的AVR/GCC默认设置下,未调用的函数也会被link进最终的镜像,从而增大image的大小,这会浪费flash资源.
以下是如何在AVR Studio4/5设置,不把未调用的函数link进image.
方法是在complier命令中增加-ffunction-sections,linker命令中增加-Wl,--gc-sections.
-ffunction-sections:不用此参数时,.o里代码部分只有.text段;使用此参数,则会使每个函数单独成为一段,比如函数func1()成为.text.func1段,但对链接后代码大小没影响。
--gc-sections:这是avr-ld的参数,通过-Wl,<option>由gcc把option里的参数传递给avr-ld。它使得链接器ld链接时删除不用的段。
这样,因为每个函数自成一段(即可以看作函数=段),如果有某个函数未被任何函数/段调用,则ld不会链接它。
AVR Studio 4:
Edit Configuration Options – Custom Options – [All Files] – add
-ffunction-sections
– [Linker Options] – add -Wl,--gc-sections
http://songzhangzhang.blog.163.com/blog/static/69401981201141321641323/
g++: error: unrecognized option ‘--gc-sections’
注意:若不添加这些选项的话,则默认是不链接未调用的函数的
testlib.cpp:
|
1
2
3
4
5
6
7
8
9
10
11
12 |
#include "testlib.h"void
MyFile::TestLibA(){? cout<<"In MyFile::TestLibA()"<<endl;}int my_add(int
x,int y){? return
x+y;} |
testlib.h
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
#include <stdlib.h>#include <iostream>using
namespace std;class
MyFile{? public:? ? static
void TestLib()? ? {? ? ? cout<<"In MyFile::TestLib()"<<endl;? ? }? ? void
TestLibA();};int
my_add(int
x,int y); |
main.cpp
|
1
2
3
4
5
6
7
8 |
#include "testlib.h"int main(){? MyFile::TestLib();? return
0;} |
libtestlib.a的编译:
|
1 |
g++ -c -ffunction-sections -fdata-sections testlib.cpp<br>yingc@yingc:~/gcyin/test/tmp/csdn$ !ar<br>ar crv libtestlib.a testlib.o<br>a - testlib.o |
g++ -c -ffunction-sections -fdata-sections main.cpp
|
1
2
3
4
5
6 |
yingc@yingc:~/gcyin/test/tmp/csdn$ g++ -Wl,--gc-sections main.o -L. -ltestlib -static
-o mainyingc@yingc:~/gcyin/test/tmp/csdn$ !nmnm libtestlib.a | grep my_add00000000 T _Z6my_addiiyingc@yingc:~/gcyin/test/tmp/csdn$ nm main | grep my_addyingc@yingc:~/gcyin/test/tmp/csdn$ |
编译为动态库的方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 |
yingc@yingc:~/gcyin/test/tmp/csdn$ g++ -shared -fPIC -o libtestlib.so testlib.oyingc@yingc:~/gcyin/test/tmp/csdn$ lslibtestlib.so main.cpp testlib.cpp testlib.h testlib.oyingc@yingc:~/gcyin/test/tmp/csdn$ vim main.cpp [1]+ Stopped vim main.cppyingc@yingc:~/gcyin/test/tmp/csdn$ yingc@yingc:~/gcyin/test/tmp/csdn$ yingc@yingc:~/gcyin/test/tmp/csdn$ lslibtestlib.so main.cpp testlib.cpp testlib.h testlib.oyingc@yingc:~/gcyin/test/tmp/csdn$ gcc -c main.cpp yingc@yingc:~/gcyin/test/tmp/csdn$ lltotal 48drwxrwxr-x 2 yingc yingc 4096 5月 28 10:49 ./drwxrwxr-x 17 yingc yingc 4096 5月 28 10:32 ../-rwxrwxr-x 1 yingc yingc 6990 5月 28 10:48 libtestlib.so*-rw-rw-r-- 1 yingc yingc 520 5月 28 10:49 main.cpp-rw-r--r-- 1 yingc yingc 12288 5月 28 10:49 .main.cpp.swp-rw-rw-r-- 1 yingc yingc 2176 5月 28 10:49 main.o-rw-rw-r-- 1 yingc yingc 546 5月 28 10:48 testlib.cpp-rw-rw-r-- 1 yingc yingc 635 5月 28 10:47 testlib.h-rw-rw-r-- 1 yingc yingc 1448 5月 28 10:38 testlib.oyingc@yingc:~/gcyin/test/tmp/csdn$ g++ -o main main.o -L. -ltestlibyingc@yingc:~/gcyin/test/tmp/csdn$ file mainmain: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for
GNU/Linux 2.6.24, BuildID[sha1]=0x02a9f9e7e9c76d2c9b277aff913bf5387a8d7f8d, not strippedyingc@yingc:~/gcyin/test/tmp/csdn$ lslibtestlib.so main main.cpp main.o testlib.cpp testlib.h testlib.oyingc@yingc:~/gcyin/test/tmp/csdn$ ./main In MyFile::TestLib()yingc@yingc:~/gcyin/test/tmp/csdn$ |
-ffunction-sections -Wl,--gc-sections,布布扣,bubuko.com
-ffunction-sections -Wl,--gc-sections
原文地址:http://www.cnblogs.com/jingzhishen/p/3759584.html