【问题标题】:Undefined reference to rdtsc [duplicate]未定义对 rdtsc 的引用 [重复]
【发布时间】:2012-11-15 18:19:55
【问题描述】:

我正在编写创建树的代码并计算不同的创建树的方法。不过,我似乎无法让 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

知道为什么我会收到这个未定义的引用错误吗?

【问题讨论】:

  • rdtsc 是机器语言指令。你有提供rdtsc() 库函数的库吗?
  • @Greg Hewgill 我创建了一个库,却忘了用它编译。主要脑残。谢谢!

标签: c rdtsc


【解决方案1】:

添加这个并作为函数使用。

 __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;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-27
    • 2013-06-20
    • 2012-02-05
    • 2014-12-27
    • 2022-01-16
    • 2017-01-26
    • 2016-10-11
    相关资源
    最近更新 更多