collect2: ld returned 1 exit status错误.

 程序中用到 math.c,使用 gcc 编译,需要加上 -lm 选项。

-l表示使用库,m为数学库。
=================
Linux程序设计中的一个例子:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> #include  < sys / types.h >
#include 
< sys / resource.h >
#include 
< sys / time.h >
#include 
< unistd.h >
#include 
< stdio.h >
#include 
< math.h >

///  向一个临时文件写一个字符串10000次,然后进行一些数学运算,目的是制造一些CPU负荷
void  work()
{
    FILE 
* f;
    
int  i;
    
double  x  =   4.5 ;

    f 
=  tmpfile();  //  创建临时文件
     for (i  =   0 ; i  <   10000 ; i ++ ){
        fprintf(f, 
" Do some output\n " );
        
if (ferror(f)){  // TODO ferror?
            fprintf(stderr,  " Error writing to temporary file\n " );
            exit(
1 );
        }
    }

    
for (i  =   0 ; i  <   1000000 ; i ++ )
        x 
=  log(x * +   3.21 );
}

///  main 函数先调用 work, 再调用 getrusage 函数查看它使用了多少 CPU 时间。把这些资料显示在屏幕上。
int  main()
{
    
struct  rusage r_usage;
    
struct  rlimit r_limit;
    
int  priority;

    work();
    getrusage(RUSAGE_SELF, 
& r_usage);  //  只返回当前程序的 CPU 占用时间

    printf(
" CPU usage: User = %ld.%06ld, System = %ld.%06ld\n " ,
           r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,    
//  r_usage.ru_utime 程序本身执行它的指令所消耗的时间
           r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec);    //  r_usage.ru_stime OS由于这个程序而消耗的 CPU 时间

    
///  调用 getpriority 和 getrlimit 函数分别查出自己的当前优先级和文件长度限制
    priority  =  getpriority(PRIO_PROCESS, getpid());  //  PRIO_PROCESS 表示后面是进程标识符
    printf( " Current priority = %d\n " , priority);
    getrlimit(RLIMIT_FSIZE, 
& r_limit);  //  读取以字节计的文件长度限制到 r_limit 中
    printf( " Current FSIZE limit: soft = %ld, hard = %ld\n " ,
              r_limit.rlim_cur, r_limit.rlim_max);  
//  rlim_cur 软限制, rlimt_max 硬限制

    
///  通过 setrlimit 函数设置了一个文件长度限制。
    r_limit.rlim_cur  =   2048 ;     //  软限制2M
    r_limit.rlim_max  =   4096 ;     //  硬限制4M
    printf( " Setting a 2K file size limit\n " );
    setrlimit(RLIMIT_FSIZE, 
& r_limit);

    
///  然后再次调用 work 失败,因为它尝试创建的文件尺寸过大。
    work();

    exit(
0 );

}


编译:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> [green@colorfulgreen environ]$ gcc limits.c  - o limits


报错:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> / tmp / ccxW94yi.o: In function `work ' :
limits.c:(.text + 0xaf ): undefined reference to `log '
collect2: ld returned  1  exit status


编译时加上 -lm 选项:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> [green@colorfulgreen environ]$ gcc limits.c  - lm  - o limits


Done.
============
PS:为嘛这个编辑器插入代码时最前面不能有下面这几行T_T
/**
  * 资源限制
**/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值