RDTSC on VisualStudio 2010 Express - C++ does not

2019-01-28 14:17发布

I tried to test rdtsc on VisualStudio 2010. Heres my code:

#include <iostream>
#include <windows.h>
#include <intrin.h>
using namespace std;

uint64_t rdtsc()
{
    return __rdtsc();
}

int main()
{
    cout << rdtsc() << "\n";
    cin.get();
    return 0;
}

But I got errors:

------ Build started: Project: test_rdtsc, Configuration: Debug Win32 ------
  main.cpp
c:\documents and settings\student\desktop\test_rdtsc\test_rdtsc\main.cpp(12): error C2146: syntax error : missing ';' before identifier 'rdtsc'
c:\documents and settings\student\desktop\test_rdtsc\test_rdtsc\main.cpp(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\student\desktop\test_rdtsc\test_rdtsc\main.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\student\desktop\test_rdtsc\test_rdtsc\main.cpp(14): warning C4244: 'return' : conversion from 'DWORD64' to 'int', possible loss of data
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What should I do? I dont want to change uint64_t into DWORD64. Why VisualStudio doesnt understand uint64_t?

3条回答
做个烂人
2楼-- · 2019-01-28 14:33

You have not included stdint.h/cstdint at the top apparently. This will work:

#include <iostream>
#include <windows.h>
#include <intrin.h>
#include <stdint.h>
using namespace std;

uint64_t rdtsc()
{
    return __rdtsc();
}

int main()
{
    cout << rdtsc() << "\n";
    cin.get();
    return 0;
}
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-28 14:49

To have this working you have to include cstdint :

#include <cstdint> // Or <stdint.h>

cstdint is the C++-style version of the C-style header stdint.h. Then it is better in your case to use the first one even if both are working in C++.

It is said here that those headers are shipped with visual studio since the 2010 version.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-28 14:57

You have to #include <stdint.h>. Or (better) #include <cstdint>.

Visual Studio started shipping those headers with the 2010 version.

查看更多
登录 后发表回答