导航

« 大学四年的变化GNU make笔记 »

GCC编程笔记

$gcc hello.c -o hello

$gcc -E hello.c -o hello.cpp /* -E选项表示预处理后停止编译*/
$gcc -x cpp-output -c hello.cpp -o hello.o /*编译成目标代码*/
$gcc hello.o -o hello /*链接*/

$gcc 1.c 2.c -o file /*链接,-o选项默认生成file为a.out*/

$gcc myapp.c -I /home/fred/include -o myapp
$gcc myapp.c -L/home/fred/lib -lnew -o myapp
/*GCC默认使用共享库连接,如果需要静态库用-static参数*/

/*-ansi,-pedantic,-pedantic-errors,-Wall用于让编译器能够报错*/
/*-O选项进行对代码的优化*/

$gcc -g hello.c -o hello /* -g,-ggdb调试*/


内联函数:

使用内联函数需要用关键字inline.还要用-O优化选项,如:
inline void swap(int *a,int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}


函数和变量属性:

使用关键字attribute,如:
声明:void die_on_error(void) _attribute_ ((noreturn));/*表示无返回类型*/
定义:void die_on_error(void) /*定义和平常一样*/
int int_var _attribute_ ((aligned 16)) = 0; /*让变量int_var的边界按16字节对齐*/
float big_salary _attribute_ ((unused)); /*关闭对未用变量发出的所有警告*/


构造函数名称:

GCC预先定义_FUNCTION_为当前函数.在调试代码难以确定出错位置时很有用.
如:
在swap()函数的代码中加入一句:
printf("The current function is %s\n",_FUNCTION_);
则显示结果为:
The current function is swap.

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By SunK

Copyright 2005-2006 sunk.cn 蜀ICP备06007105号