使用ICC在Linux中未定义对clock_gettime()的引用 [英] Undefined reference to clock_gettime() in Linux using ICC

查看:183
本文介绍了使用ICC在Linux中未定义对clock_gettime()的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使代码(请参阅下文)在Ubuntu上运行.该代码使用clock_gettime().我想我已经成功链接到librt.a:

I am trying to get the code (see far below) working on Ubuntu. The code uses clock_gettime(). I think I have successfully linked to librt.a:

**** Build of configuration Debug for project test ****

make -k all 
Building file: ../src/test.cpp
Invoking: Intel Intel(R) 64 C++ Compiler 
icpc -g -I/usr/include/boost -std=c++0x -MMD -MP -MF"src/test.d" -MT"src/test.d" -c -o "src/test.o" "../src/test.cpp"
Finished building: ../src/test.cpp

Building target: test
Invoking: Intel Intel(R) 64 C++ Linker
icpc  -l  /usr/lib/x86_64-linux-gnu/librt.a  -o "test"  ./src/test.o   
icpc: command line warning #10155: ignoring option '-l'; argument required
./src/test.o: In function `main':
/home/p/workspace/test/Debug/../src/test.cpp:12: undefined reference to `clock_gettime'
/home/p/workspace/test/Debug/../src/test.cpp:15: undefined reference to `clock_gettime'
make: *** [test] Error 1
make: Target `all' not remade because of errors.

**** Build Finished ****

但是,我仍然收到关于clock_gettime的未定义引用的错误.这是我的代码:

However, I still get the error about undefined reference to clock_gettime. This is my code:

#include <iostream>
#include <time.h>
using namespace std;

timespec diff(timespec start, timespec end);

int main()
{
    timespec time1, time2;
    int temp;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
    for (int i = 0; i< 242000000; i++)
        temp+=temp;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
    cout<<diff(time1,time2).tv_sec<<":"<<diff(time1,time2).tv_nsec<<endl;
    return 0;
}

timespec diff(timespec start, timespec end)
{
    timespec temp;
    if ((end.tv_nsec-start.tv_nsec)<0) {
        temp.tv_sec = end.tv_sec-start.tv_sec-1;
        temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
    } else {
        temp.tv_sec = end.tv_sec-start.tv_sec;
        temp.tv_nsec = end.tv_nsec-start.tv_nsec;
    }
    return temp;
}

有人可以帮忙吗?

推荐答案

您似乎根本没有链接librt.a,因为链接程序忽略了-l.也许您应该使用-lrt,并且可以选择通过-L给出路径.

It looks like you haven't linked librt.a at all since the linker is ignoring -l. Perhaps you were supposed to use -lrt and optionally give the path via -L.

icpc  -lrt -L/usr/lib/x86_64-linux-gnu -o "test"  ./src/test.o

注意-l及其参数之间没有空格.我也将"librt.a"仅列为rt;链接器将自己添加其余的内容.

Notice I have no spaces between the -l and its parameter. I also have listed "librt.a" as merely rt; the linker will add the rest on its own.

这篇关于使用ICC在Linux中未定义对clock_gettime()的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆