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:
我正在尝试让代码(见下文)在Ubuntu上工作。代码使用clock_gettime()。我想我已经成功地链接到了libra。
**** 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 ****
However, I still get the error about undefined reference to clock_gettime. This is my code:
但是,我仍然得到关于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;
}
can someone please help?
有人能帮忙吗?
2 个解决方案
#1
8
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
.
看起来你还没有连接librt。因为链接器忽略了-l。也许您应该使用-lrt并可选地通过-L给出路径。
icpc -lrt -L/usr/lib/x86_64-linux-gnu -o "test" ./src/test.o
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.
注意-l和它的参数之间没有空格。我还列出了“librt”。“只是rt;链接器将自己添加其余的。
#2
0
Besides adding -lrt to the linker flags, it's strongly recommended to add -Wl, -no-as-needed to the linker flags as well. Ref from man ld:
除了向链接器标志添加-lrt之外,强烈建议向链接器标志添加-Wl, -no。从男人ld裁判:
--as-needed
——需
--no-as-needed
——no-as-needed
This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the --as-needed option. Normally the linker will add a DT_NEEDED tag for each dynamic library mentioned on the command line, regardless of whether the library is actually needed or not. --as-needed causes a DT_NEEDED tag to only be emitted for a library that satisfies an undefined symbol reference from a regular object file or, if the library is not found in the DT_NEEDED lists of ther libraries linked up to that point, an undefined symbol reference from another dynamic library. --no-as-needed restores the default behaviour.
此选项将影响命令行中所提到的动态库的ELF dt_required标记,在“按需”选项之后。通常,链接器会为命令行中提到的每个动态库添加一个dt_required标记,而不管该库是否实际需要。——根据需要,只对满足常规对象文件的未定义符号引用的库发出dt_required标记,或者,如果在连接到该点的其他库的dt_required列表中没有找到该库,则从另一个动态库发出未定义符号引用。——不需要时恢复默认行为。
#1
8
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
.
看起来你还没有连接librt。因为链接器忽略了-l。也许您应该使用-lrt并可选地通过-L给出路径。
icpc -lrt -L/usr/lib/x86_64-linux-gnu -o "test" ./src/test.o
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.
注意-l和它的参数之间没有空格。我还列出了“librt”。“只是rt;链接器将自己添加其余的。
#2
0
Besides adding -lrt to the linker flags, it's strongly recommended to add -Wl, -no-as-needed to the linker flags as well. Ref from man ld:
除了向链接器标志添加-lrt之外,强烈建议向链接器标志添加-Wl, -no。从男人ld裁判:
--as-needed
——需
--no-as-needed
——no-as-needed
This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the --as-needed option. Normally the linker will add a DT_NEEDED tag for each dynamic library mentioned on the command line, regardless of whether the library is actually needed or not. --as-needed causes a DT_NEEDED tag to only be emitted for a library that satisfies an undefined symbol reference from a regular object file or, if the library is not found in the DT_NEEDED lists of ther libraries linked up to that point, an undefined symbol reference from another dynamic library. --no-as-needed restores the default behaviour.
此选项将影响命令行中所提到的动态库的ELF dt_required标记,在“按需”选项之后。通常,链接器会为命令行中提到的每个动态库添加一个dt_required标记,而不管该库是否实际需要。——根据需要,只对满足常规对象文件的未定义符号引用的库发出dt_required标记,或者,如果在连接到该点的其他库的dt_required列表中没有找到该库,则从另一个动态库发出未定义符号引用。——不需要时恢复默认行为。