我正在尝试让代码(见下文)在 Ubuntu 上运行。该代码使用clock_gettime()
. 我想我已经成功链接到 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 的错误。这是我的代码:
#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;
}
有人可以帮忙吗?