2012-11-28 122 views
1

我正在编写创建树和时间不同的创建树的方法的代码。不过,我似乎无法让rdtsc正常工作。未定义的引用rdtsc

这里是我的代码:

#include <stdio.h> 
    #include <stdlib.h> 
    #define SIZE 10 
    struct tnode { 
     int val; 
     struct tnode *left; 
     struct tnode *right; 
    }; 
    struct tnode *addnode(struct tnode *p, long n); 
    void treeprint(struct tnode *p); 

    main() { 
     long data[SIZE]={6, 3, 8, 1, 7, 5, 2, 9, 0, 4}; 
     int i; 
     struct tnode *node, *root; 
     unsigned long long rdtsc(); 
     unsigned long long a, b; 

     printf("size of tnode = %d\n", sizeof(struct tnode)); 
     printf("size of *node = %d\n", sizeof *node); 
     printf("size of &node = %d\n", sizeof &node); 
     printf("size of root = %d\n", sizeof root); 
     printf("size of *root = %d\n", sizeof *root); 
     printf("size of &root = %d\n", sizeof &root); 

     a = rdtsc(); 
     root = NULL; 
     for (i = 0; i < SIZE; i++) 
      root = addnode(root, data[i]); 
     b = rdtsc(); 
     treeprint(root); 
     printf("It took %llu to make this tree.\n", b-a); 
    } 

假设上面列出的所有功能都采取的(除RDTSC,当然)照顾。

当我尝试编译,我得到这个错误:

/tmp/cccnojMf.o: In function `main': 
tree.c:(.text+0xd9): undefined reference to `rdtsc' 
tree.c:(.text+0x120): undefined reference to `rdtsc' 
collect2: ld returned 1 exit status 

任何为什么我得到这个未定义的引用错误的想法?

+0

'rdtsc'是一种机器语言指令。你有一个提供'rdtsc()'库函数的库吗? –

+0

@Greg Hewgill我做了一个图书馆,忘了编译它。主要的brainfart。谢谢! – Greg

回答

2

添加此功能并将其用作功能。

__inline__ uint64_t rdtsc(void) 
    { 
uint32_t lo, hi; 
__asm__ __volatile__ (
     "xorl %%eax,%%eax \n  cpuid" 
     ::: "%rax", "%rbx", "%rcx", "%rdx"); 
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); 
return (uint64_t)hi << 32 | lo; 
}