[Cmake] compile with MSVC
version 0.2.0
I was able to compile with Visual Studio 2019 and 2022, but there was a lot of tricks. Some of them could be addressed in the CMake. Here is a list of what I had to do in the projects generated by CMake
- install vcpkg to get dirent and pthread implementations
- remove every linker flag trying to link with m.lib
- remove additional flags -Wall -Wpedantic...
- manually define R_OK
- define attribute() as an empty macro
- use -D__thread=__declspec(thread)
- even with experimental flags and changing the ISO, stdatomic could not be found. So I compiled t_ops.c as C++ (wrapping code extern "C") to #include the C++
and declare a C interface to atomics with it - manually define CLOCK_MONOTONIC and implement some clock_gettime()
- comment out the #include <unistd.h>
Thanks for the detailed report @PierreChatelier! I've pushed a fix in commit 5042622 that addresses all the MSVC issues you listed:
CMake changes:
-lmlinker flag is now conditional (skipped on MSVC — math is in the CRT)- Added
_CRT_SECURE_NO_WARNINGSand_USE_MATH_DEFINES - MSVC uses
/W3with targeted warning suppressions instead of-Wall -Wpedantic
Source changes:
| Issue | Fix |
|---|---|
stdatomic.h |
MSVC _Interlocked* intrinsics |
__thread |
__declspec(thread) on MSVC |
__attribute__((unused)) |
__pragma(warning(suppress: 4505)) |
clock_gettime / CLOCK_MONOTONIC |
QueryPerformanceCounter |
R_OK |
#define R_OK 4 + _access() |
pthread.h |
Windows CRITICAL_SECTION / CONDITION_VARIABLE shim |
<unistd.h> / <dirent.h> |
Guarded behind #ifndef _WIN32 |
I don't have a Windows machine to test on, so I'd appreciate if you could try the latest main and let me know if it compiles cleanly now. The pthread shim covers the basic thread pool usage but there may be edge cases I missed.
So I have checked the latest main
- -Wall -Wpedantic -Wunused-parameters are still there in the additional compiler options of the project, in both normal and CUDA options
- #include <dirent.h> is needed for _WIN32 in tq_model.c (I use dirent from vcpkg)
- PTHREAD_MUTEX_INITIALIZER is undefined, and I don't know what you can do for that with a CRITICAL_SECTION backend. If you do not need a recursive mutex, consider using SRWLOCK and SRWLOCK_INIT
- #include <Windows.h> is needed in tq_transformer.c in order to find LARGE_INTEGER
@PierreChatelier Thank you for testing and the detailed follow-up! All 4 issues addressed:
1. -Wall -Wpedantic on MSVC — Now wrapped in if(NOT MSVC) in CMakeLists.txt. MSVC uses /W3 with targeted suppressions instead.
2. dirent.h for Windows — Added a minimal opendir/readdir/closedir implementation using FindFirstFile/FindNextFile (no vcpkg needed).
3. PTHREAD_MUTEX_INITIALIZER — Switched from CRITICAL_SECTION to SRWLOCK which supports SRWLOCK_INIT as a static initializer. PTHREAD_MUTEX_INITIALIZER now maps to SRWLOCK_INIT.
4. <Windows.h> in tq_transformer.c — Added #ifdef _WIN32 include for LARGE_INTEGER / QueryPerformanceCounter.
These are in the latest main. Would appreciate if you could test again!
It must not be easy if you can't test with Windows !
So with the new main :
-
There is an unconditional
#include <pthread.h>in tq_context.c, that fails because now that there is your custom pthread redefinitions, I cannot use vcpkg pthread any more You have two custom pthread implementations in tq_generate.c and tq_ops.c, I suggest you create a dedicated private header that could be included by tq_context.c, tq_generate.c and tq_ops.c -
There is an unsupported (if yoiu don't use vcpkg) unguarded
#include <pthread.h>in quant.h, but actually the quant.h body needs dirent, CLOCK_MONOTONIC, pthreads... and you only defined custom implementations in some c files. A common header would help -
in tq_context.c,
if (pthread_mutex_init(&c->mutex, NULL) != 0) {is illegal sincepthread_mutex_initis implemented asInitializeSRWLockwhich returns void -
there is an unguarded unsupported
#include <unistd.h>in tq_convert.c -
tq_convert.c and tq_tranformer.c do not see the custom CLOCK_MONOTONIC and timespec from quant.c. A common header would help
-
tq_convert.c does not see R_OK from tq_model.c. A common header would help
-
struct timespec { long tv_sec; long tv_nsec; };must not be defined because Windows has such a definition in some time.h that happen to be included. -
the stdatomic header will be supported under Visual Studio only if the additional flag "/experimental:c11atomics" is set