NO

Author Topic: Linking error with _rdtsc() function in Pelles C 10  (Read 5346 times)

Offline nex42

  • Member
  • *
  • Posts: 3
Linking error with _rdtsc() function in Pelles C 10
« on: October 31, 2020, 12:04:19 PM »
Hi, I am new here. I sometimes use Pelles C on Windows.

The program below uses the _rdtsc() function. It builds and runs well on one laptop with Pelles C 9, but not on another laptop with Pelles C 10 because of a linker error. It looks like version 10 does not have _rdtsc()?
Or do I have to install some extra library?

(this is just a little test program to find where the problem is)

/* rdtsc */

#include <stdio.h>
#include <time.h>
#include <math.h>

int main(void)
{
   unsigned long long start, stop;
   double x;

   start = _rdtsc();
   for (int i = 0; i < 1000000; i++)
      x = sqrt(i);
   stop = _rdtsc();
   printf("delta t = %llu\n", stop - start);
}

C:\Users\nex42\Documents\Pelles C Projects\rdtsc\main.c(12): warning #2018: Undeclared function '_rdtsc' (did you mean: rintl?); assuming 'extern' returning 'int'.
Building rdtsc.exe.
POLINK: error: Unresolved external symbol '_rdtsc' - referenced from 'C:\Users\nex42\Documents\Pelles C Projects\rdtsc\output\main.obj'.
POLINK: fatal error: 1 unresolved external(s).

Offline algernon_77

  • Member
  • *
  • Posts: 33
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #1 on: October 31, 2020, 02:43:44 PM »
Hello,

It seems that, for some reason, in v. 10 this function is commented in time.h:

Code: [Select]
/* #if __POCC_TARGET__ == 1 || __POCC_TARGET__ == 3
extern unsigned long long __cdecl _rdtsc(void);
#endif */

If you remove the comments, your example will compile and run just fine

Offline nex42

  • Member
  • *
  • Posts: 3
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #2 on: October 31, 2020, 07:18:57 PM »
Thanks, that worked! :)

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #3 on: November 01, 2020, 05:06:00 PM »
If you remove the comments, your example will compile and run just fine
Congratulations. Worst. Advice. Ever.

The program below uses the _rdtsc() function. It builds and runs well on one laptop with Pelles C 9, but not on another laptop with Pelles C 10 because of a linker error. It looks like version 10 does not have _rdtsc()?
Or do I have to install some extra library?

The _rdtsc function has been an intrinsic since ~v2.90, so the correct thing to do is add:
Code: [Select]
#include <intrin.h>
/Pelle

Offline algernon_77

  • Member
  • *
  • Posts: 33
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #4 on: November 01, 2020, 06:53:00 PM »
I didn't say that my advice is "best ever" either, just that ,without the comments, that code compiles and works.
And, I quote from the help file:

Code: [Select]
Purpose:
Reads the processors Time-Stamp Counter.

Syntax:
unsigned long long _rdtsc(void);

Declared in:
<time.h>

Description:
The _rdtsc function reads the processors Time-Stamp Counter through the X86/X64 RDTSC instruction.

Note! The name __rdtsc may also be used when compiling with Microsoft extensions (the /Ze option is used).

Returns:
The current Time-Stamp Counter value.

See also:
The __rdtscp intrinsic.


If it's the worst thing ever to use that from time.h, maybe it should be more clear in the docs, or in the header comments, so less gifted people like me would get it.
But, we all have bad days, so I'm ok with it, will take the advice and move on.

Regards.
 

Offline nex42

  • Member
  • *
  • Posts: 3
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #5 on: November 01, 2020, 10:08:08 PM »
I changed time.h back to original and added intrin.h to my program and it worked well.
It makes sense to keep a standard header file like time.h clean from implementation specific stuff like _rdtsc(), but I agree with Algernon77 that the documentation could be better on this point.
Anyway, thanks to you both.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #6 on: November 01, 2020, 11:18:08 PM »
I would like to express differently the point made by Pelle, with the hope that it clarify the issue and the misunderstanding that, in my opinion, has originated here.
The point is that compiler header files have to be considered as read-only objects. They contains definitions that directly interacts with code generated from the compiler, and any modification could possibly interfere with correct working.
If a definition is missed, as first workaround you could add it in your .h or .c files. Then you could eventually investigate if it is defined somewhere. The modification of system headers normally is a no-option.
That was the core of the apparently rude comment from Pelle.
Then he forgot to update help (he was updating a whole compiler system after all), and simply commented out the definition in time.h instead of remove it.
The _rdtsc() intrinsic function was present since version 2.90, and, even being an intrinsic function, still declared in time.h (not standard place, but intrin.h doesn't existed at that time). Now has been moved to where it should have already reside. This generates an incompatibility with existent code, that need to include the new header.

I hope that now reading again the answer it should sound a little different.
« Last Edit: November 15, 2020, 11:19:22 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 189
    • Bcx Basic to C/C++ Translator
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #7 on: November 02, 2020, 02:05:42 AM »
When I read :

Quote from: algernon_77 on October 31, 2020, 02:43:44 pm
    If you remove the comments, your example will compile and run just fine


my next thought was, " why doesn't he ask Pelle why it was blocked out?"


I'll admit it -- I laughed when I read Pelles' reply and I'd bet I'm not the only one
on this forum who appreciates a bit of harmless sarcasm from time to time.

Lessons have been learned .. it's time to get back to work.   8)
Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline algernon_77

  • Member
  • *
  • Posts: 33
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #8 on: November 02, 2020, 09:38:35 AM »
Ok :)

So I'm well past my prom night, and I like Monty Python, but let's not mistake sarcasm for something else :)

The question was about _rdtsc();
In the help file, says that _rdtsc() is declared in time.h, and the intrinsic (from intrin.h) is _rdtscp();
Seeing it commented in time.h, I assumed that, being a one man show, it somehow slipped scrutiny; over the years, I've encountered hiccups in Borland C++, Watcom, Microsoft headers, and they had way more resources with respect to QA.
I assumed wrong and it certainly wasn't my intention to suggest a hackfest on the v10 headers :)

As said, lessons were learned, both ways, hopefully.
This does not change a bit the respect for the work of one man.
I shall refrain from suggesting any such "improvements" in the future :)

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #9 on: November 15, 2020, 08:53:19 PM »
Thanks frankie, for clearing up what I was unable to say myself...
I will clean up the help file for the next release, it is confusing ...  :(
/Pelle

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Linking error with _rdtsc() function in Pelles C 10
« Reply #10 on: November 16, 2020, 02:00:50 PM »
 :) :) :)
It's wonderful when we understand each other...  8)
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide