D bindings for the CFITSIO C library, which provides high-level routines for reading and writing FITS (Flexible Image Transport System) files.
- Direct access to the full CFITSIO C API from D
- Cross-platform: Windows, Linux, macOS
- Easy integration with DUB
- Includes a CLI installer to fetch prebuilt binaries
- Actively maintained and auto-updatable bindings
You must have the native CFITSIO library and its dependencies (e.g., zlib) available for linking and at runtime.
This D package provides the bindings, not the C library itself.
Note:
Due to a current issue,cfitsio-dis not yet available on the official DUB package registry.
You must add it manually to yourdub.jsonordub.sdlusing the GitHub repository URL and a specific commit hash or branch.
In your dub.json:
"dependencies": {
"cfitsio-d": {
"version": "<commit-hash>",
"repository": "git+https://github.com/chrischtel/cfitsio-d.git"
}
},In your dub.sdl:
dependency "cfitsio-d" repository="git+https://github.com/chrischtel/cfitsio-d.git" version="<commit-hash>"
Run the installer to download and extract the correct prebuilt binaries for your platform:
dub run cfitsio-d:installTo see all options:
dub run cfitsio-d:install -- --helpOptions include:
--versionor-vto specify the CFITSIO version--outputor-oto set the output directory--forceor-fto force re-download--no-update-dubto skip updatingdub.json--keep-archiveor-kto keep the downloaded archive--quietor-qfor less output
The installer will:
- Download the correct binary archive for your OS/arch (and MSVC/MinGW on Windows)
- Extract
cfitsio.dll/.so/.dylibandzlib1.dll/.libif needed - Optionally update your
dub.jsonwith the correct linker flags
-
Windows:
- Use vcpkg or the official CFITSIO site.
- Make sure you use the correct architecture (x86_64 vs x86) and toolchain (MSVC vs MinGW).
- Place
cfitsio.dllandzlib1.dllnext to your executable or in yourPATH. - Place
cfitsio.libandzlib.libin your project directory or add their location to your linker path.
-
Linux:
- Install with your package manager:
sudo apt install libcfitsio-dev # or sudo dnf install cfitsio-devel # or sudo pacman -S cfitsio
- This provides
libcfitsio.soin your system library path.
- Install with your package manager:
-
macOS:
- Install with Homebrew:
brew install cfitsio
- This provides
libcfitsio.dylibin your system library path.
- Install with Homebrew:
"lflags-windows": [
"/LIBPATH:.", // or your lib directory
"cfitsio.lib",
"zlib.lib"
]/LIBPATH:.tells the linker to look in the current directory for.libfiles.- Make sure your
.libfiles are built with MSVC if you use DMD/LDC with the MSVC toolchain.
"lflags-windows": [
"-L.",
"-lcfitsio",
"-lz"
]- Only use this if you are building everything with MinGW (not recommended for DMD/LDC).
"lflags-posix": [
"-L.",
"-lcfitsio"
]- If installed system-wide, just use
-lcfitsio.
- Place
cfitsio.dllandzlib1.dllin the same directory as your executable, or in yourPATH. - MSVC vs MinGW:
- MSVC
.libfiles are not compatible with MinGW, and vice versa. - Always match the architecture (x86_64 vs x86) and toolchain.
- MSVC
- If you build your D project as 64-bit (
x86_64), you must use 64-bitcfitsio.dll/.libandzlib1.dll/.lib. - If you build as 32-bit, use 32-bit binaries.
- Mixing 32/64-bit or MSVC/MinGW will result in linker or runtime errors.
import cfitsio;
import std.stdio;
void main() {
fitsfile* fptr = null;
int status = 0;
string filename = "!example.fits\0"; // '!' to overwrite if exists
int ret = ffinit(&fptr, filename.ptr, &status);
if (ret != 0) {
writeln("Error initializing FITS file: ", status);
return;
}
writeln("FITS file initialized successfully.");
// Write a minimal primary HDU (header only, no data)
int simple = 1;
int bitpix = 8;
int naxis = 0;
int* naxes = null;
ffphpr(fptr, simple, bitpix, naxis, naxes, 0, 1, 1, &status);
ffclos(fptr, &status);
if (status != 0) {
writeln("Error closing FITS file: ", status);
return;
}
writeln("FITS file closed successfully.");
}-
Linker error: "invalid or corrupt file"
- Your
.libfile is not built with MSVC, or is the wrong architecture. - Use the CLI installer or build CFITSIO with the correct toolchain.
- Your
-
Linker error:
/L.not recognized- Use
/LIBPATH:.for MSVC, not-L..
- Use
-
Runtime error:
Program exited with code -1073741515(0xC0000135)- A required DLL (
cfitsio.dll,zlib1.dll, or a Visual C++ runtime DLL) is missing. - Place all required DLLs next to your executable or in your
PATH. - Use Dependencies to check for missing DLLs.
- A required DLL (
-
"library not found" on Linux/macOS
- Set
LD_LIBRARY_PATH(Linux) orDYLD_LIBRARY_PATH(macOS) if the library is not in a standard location.
- Set
-
Architecture mismatch
- All binaries (your app,
cfitsio.dll,zlib1.dll) must be either all 64-bit or all 32-bit.
- All binaries (your app,
-
MinGW vs MSVC
- Do not mix MinGW and MSVC binaries. Use MSVC-built libraries for DMD/LDC on Windows.
The installer can be run with various options:
dub run cfitsio-d:install -- --helpOptions:
--versionor-v— Specify CFITSIO version (default: latest)--outputor-o— Output directory for library files--forceor-f— Force re-download even if files exist--no-update-dub— Do not updatedub.json--keep-archiveor-k— Keep the downloaded archive--quietor-q— Suppress non-error output--proxy— Use a proxy for downloads
The installer will:
- Download the correct binary archive for your OS/arch/toolchain
- Extract all required files (
cfitsio.dll,cfitsio.lib,zlib1.dll,zlib.lib, etc.) - Optionally update your
dub.jsonwith the correct linker flags
To update to a new CFITSIO version:
- coming soon...
- PRs and issues are welcome!
- Please run the update script and test on your platform before submitting changes.
SEE LICENSE FILE
Q: I get a linker error about cfitsio.lib being corrupt.
A: You are probably using a MinGW-built .lib with MSVC, or vice versa. Use the CLI installer or build with the correct toolchain.
Q: My program runs but exits with code -1073741515.
A: A required DLL is missing. Use Dependencies to check, and ensure all DLLs are present and match your architecture.
Q: How do I use the installer with custom options?
A:
dub run cfitsio-d:install -- --helpfor all options.
If you have any issues, please open an issue on GitHub!