I figured I'd look into this myself. I wrote a little test program and used the VS compiler to test.
- Via Start, I launched a VS developer prompt (among other ways of doing that like in the answer to Run cl.exe from cmd ).
- I wrote up some simple programs to test the various cases.
The answer
My experimentation showed that the answer to this question is not necessarily straightforward:
- Do I link to
winmm.lib
and winmm.dll
?
You may link/consume winmm
, but windowsapp.lib
is also sufficient, even though it is not documented that the time API functions are part of it.
- Do I include
Windows.h
or timeapi.h
?
At least in my experimentation, Windows.h
was actually required to use timeBeginPeriod
. timeapi.h
was not sufficient or necessary. It is unclear to me why that is the case.
Here's how I got this answer:
Compiling a simple program
Just to prove things will compile:
#include <iostream>
int main()
{
std::cout << "Hello!" << std::endl;
}
>cl /EHsc src\app.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.cpp
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
app.obj
> .\app.exe
Hello!
Consume functions known to exist in Windowsapp.lib
#include <iostream>
#include <winstring.h>
int main()
{
std::cout << "Hello!" << std::endl;
// Ignore the poor error handling
HSTRING string;
WindowsCreateString(L"Test", 4, &string);
const auto len = WindowsGetStringLen(string);
std::cout << len << std::endl;
WindowsDeleteString(string);
}
> cl /EHsc windowsapp.lib src\app.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.cpp
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
windowsapp.lib
app.obj
> .\app.exe
Hello!
4
Consuming functions time API functions (this question)
#include <iostream>
// Interestingly, timeapi.h does not work:
// #include <timeapi.h>
#include <Windows.h>
int main()
{
std::cout << "Hello!" << std::endl;
timeBeginPeriod(500);
}
Linking winmm.lib:
> cl /EHsc winmm.lib src\app.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.cpp
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
winmm.lib
app.obj
> .\app.exe
Hello!
Interestingly, you can also link just Windowsapp.lib instead:
> cl /EHsc windowsapp.lib src\app.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.cpp
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
windowsapp.lib
app.obj
> .\app.exe
Hello!