NeuroAgent

How to Fix crtdefs.h Error in PG Vector on Windows

Learn how to fix the 'crtdefs.h: No such file or directory' error when installing PG Vector on Windows. Complete troubleshooting guide with step-by-step solutions and alternative installation methods.

Question

I’m trying to install PG Vector on Windows following the instructions from the GitHub repository. I’ve installed the necessary C++/C compiler environment (GNU) and set up the path variable for nmake to:

C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.44.35207\bin\Hostx64\x86

When I run the command nmake /F Makefile.win from the Visual Studio Developer Command Prompt, I encounter the following error:

D:\pg_vector_clone\pgvector>Microsoft (R) Program Maintenance Utility
Version 14.44.35217.0 'Microsoft' is not recognized as an internal or
external command, operable program or batch file.

D:\pg_vector_clone\pgvector>Copyright (C) Microsoft Corporation.  All
rights reserved. 'Copyright' is not recognized as an internal or
external command, operable program or batch file.

D:\pg_vector_clone\pgvector> D:\pg_vector_clone\pgvector>        cl
/nologo /I"C:\Program
Files\PostgreSQL\18\include\server\port\win32_msvc" /I"C:\Program
Files\PostgreSQL\18\include\server\port\win32" /I"C:\Program
Files\PostgreSQL\18\include\server" /I"C:\Program
Files\PostgreSQL\18\include"   /O2 /fp:fast /c src\bitutils.c
/Fosrc\bitutils.obj bitutils.c

D:\pg_vector_clone\pgvector>bitutils.c 'bitutils.c' is not recognized
as an internal or external command, operable program or batch file.

D:\pg_vector_clone\pgvector>C:\Program
Files\PostgreSQL\18\include\server\pg_config_os.h(29): fatal error
C1083: Cannot open include file: 'crtdefs.h': No such file or
directory 'C:\Program' is not recognized as an internal or external command, operable program or batch file.

D:\pg_vector_clone\pgvector>NMAKE : fatal error U1077: 'cl /nologo
/I"C:\Program Files\PostgreSQL\18\include\server\port\win32_msvc"
/I"C:\Program Files\PostgreSQL\18\include\server\port\win32"
/I"C:\Program Files\PostgreSQL\18\include\server" /I"C:\Program
Files\PostgreSQL\18\include"   /O2 /fp:fast /c src\bitutils.c
/Fosrc\bitutils.obj' : return code '0x2' NMAKE :  U1065: invalid
option ':' Stop.

D:\pg_vector_clone\pgvector>Stop. 'Stop.' is not recognized as an
internal or external command, operable program or batch file.

I’m unable to understand what I’m missing here. Can someone help me resolve this “fatal error C1083: Cannot open include file: ‘crtdefs.h’: No such file or directory” when installing PG Vector on Windows?

NeuroAgent

The “fatal error C1083: Cannot open include file: ‘crtdefs.h’: No such file or directory” error when installing PG Vector on Windows typically occurs due to missing Windows SDK files or incorrect Visual Studio setup. This error indicates that the C compiler cannot find the crtdefs.h file, which is part of the Visual C++ runtime and Windows SDK components.


Contents


Understanding the crtdefs.h Error

The crtdefs.h file is a critical header file in the Visual C++ runtime environment that contains important definitions for C standard library functions. When this file is missing or cannot be found during the pgvector compilation process, the build fails with the C1083 error.

According to Microsoft’s documentation, this error commonly occurs when:

  • Windows SDK is not properly installed or corrupted
  • Visual Studio is missing required C++ components
  • Environment variables are incorrectly configured
  • Wrong command prompt is being used for compilation

The error specifically points to pg_config_os.h(29) trying to include crtdefs.h, which means the PostgreSQL build system is attempting to compile using Visual Studio tools but cannot locate the necessary header files.


Common Causes of the Error

Based on research from multiple sources, here are the most common causes of the crtdefs.h error:

1. Missing Windows SDK Components

The Windows SDK contains the crtdefs.h file and related C runtime headers. If the SDK is not installed or is incomplete, compilation will fail.

2. Incorrect Visual Studio Installation

Visual Studio installations may not include all necessary C++ build tools by default, especially in “Build Tools” editions.

3. Wrong Command Prompt Environment

Using a regular Command Prompt instead of the Visual Studio Native Tools Command Prompt can cause path and environment issues.

4. Corrupted Installation Files

Sometimes, partial or corrupted Visual Studio/SDK installations can lead to missing system files.

5. Path Configuration Issues

Incorrect environment variables or paths can prevent the compiler from finding the necessary files.


Step-by-Step Solutions

Solution 1: Verify and Reinstall Windows SDK

  1. Check Windows SDK Installation:

    • Open Visual Studio Installer
    • Select your installation and click “Modify”
    • Ensure “Windows 10 SDK” or “Windows 11 SDK” is installed (version 10.0.19041.0 or later recommended)
  2. Reinstall Windows SDK if Missing:

    • Download and install the latest Windows SDK from Microsoft’s website
    • During installation, select “C++ build tools” component

Note: As suggested on Stack Overflow, “Reinstall windows 10 sdk, if you missing windows sdk or if its content changed this error will popup”

Solution 2: Configure Visual Studio Properly

  1. Use Correct Command Prompt:

    • Open “x64 Native Tools Command Prompt for VS 2022”
    • This ensures proper environment variables are set
    • Navigate to your pgvector directory and try compilation again
  2. Verify Visual Studio Components:

    • In Visual Studio Installer, ensure these components are installed:
      • MSVC v143 - VS 2022 C++ x64/x86 build tools
      • Windows 10/11 SDK
      • C++ CMake tools for Windows

Solution 3: Set Environment Variables

  1. Add MSVC Tools to Path:

    bash
    set PATH="C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.44.35207\bin\Hostx64\x64;%PATH%"
    
  2. Set Include Directories:

    bash
    set INCLUDE="C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.44.35207\include;%INCLUDE%"
    

Solution 4: Clean and Rebuild

  1. Clean the Build:

    bash
    nmake /F Makefile.win clean
    
  2. Rebuild the Extension:

    bash
    nmake /F Makefile.win
    

Alternative Installation Methods

Method 1: Using Conda (Recommended)

If you’re having trouble with the manual compilation, you can use conda to install pgvector:

bash
conda install -c conda-forge pgvector

This method avoids the compilation issues entirely and is the recommended approach for Windows users, as mentioned in the Stack Overflow discussion.

Method 2: Using Precompiled Binaries

Check if precompiled binaries are available for your PostgreSQL version from the pgvector releases page.

Method 3: Using Docker

Run pgvector in a Docker container to avoid Windows-specific compilation issues:

bash
docker run -d --name pgvector -e POSTGRES_PASSWORD=yourpassword -p 5432:5432 pgvector/pgvector:pg16

Troubleshooting Additional Issues

Issue: “nmake is not recognized”

If you get “nmake is not recognized” error:

  1. Ensure you’re using the correct Visual Studio Command Prompt
  2. Add the nmake path to your system PATH:
    C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.44.35207\bin\Hostx64\x64
    

Issue: “pg_config.h not found”

This typically means PostgreSQL development headers are missing:

  1. Install PostgreSQL development package or
  2. Set PGROOT environment variable:
    bash
    set PGROOT="C:\Program Files\PostgreSQL\18"
    

Issue: “Case value ‘4’ already used”

According to the Windows installation guide, this error occurs when you’re not using the x64 Native Tools Command Prompt. Always use the correct command prompt for your target architecture.


Best Practices for Windows Installation

1. Always Use Visual Studio Native Tools Command Prompt

This ensures proper environment variables are set for compilation.

2. Verify All Dependencies Before Starting

  • Check PostgreSQL installation path
  • Verify Visual Studio components
  • Ensure Windows SDK is installed

3. Keep Software Updated

  • Use the latest PostgreSQL version
  • Keep Visual Studio/Build Tools updated
  • Use recent Windows SDK versions

4. Consider Alternative Installation Methods

For Windows users, conda installation is often more reliable than manual compilation.

5. Document Your Environment

Keep track of:

  • PostgreSQL version and installation path
  • Visual Studio version and build tools
  • Windows SDK version
  • Any custom environment variables

Conclusion

The “crtdefs.h: No such file or directory” error is a common compilation issue when installing pgvector on Windows, typically caused by missing Windows SDK components or incorrect Visual Studio setup. The most reliable solutions include:

  1. Reinstall Windows SDK to ensure all necessary C runtime files are present
  2. Use the correct command prompt - x64 Native Tools Command Prompt for VS 2022
  3. Verify Visual Studio installation includes all required C++ build tools
  4. Consider alternative installation methods like conda for Windows users

For future installations, using conda (conda install -c conda-forge pgvector) is recommended as it avoids many Windows-specific compilation issues entirely. Always ensure your development environment is properly configured before attempting to compile extensions from source.

If you continue to experience issues, consider reaching out to the pgvector GitHub community for specific troubleshooting assistance with your setup.


Sources

  1. Stack Overflow - Unable to install pgvector extension for PostgreSQL Windows
  2. GitHub Issue - crtdefs.h not found issue while running command nmake /F Makefile.win
  3. Microsoft Q&A - How do I fix this issue: C1083 Cannot open include file: ‘corecrt.h’: No such file or directory
  4. Stack Overflow - Cannot find corecrt.h: $(UniversalCRT_IncludePath) is wrong
  5. pgvector4Windows Installation Guide
  6. GitHub Issue - Failed to nmake on postgress 16 [windows]
  7. Stack Overflow - Cannot open include file: ‘libintl.h’ Windows 11