diff --git a/Documentation/meson.build b/Documentation/meson.build
index c355d5feb504..b1720b05f5ee 100644
--- a/Documentation/meson.build
+++ b/Documentation/meson.build
@@ -1,5 +1,5 @@
 doc_install_dir = join_paths(get_option('datadir'), 'doc',
-                             'libcamera-@0@'.format(meson.project_version()))
+                             'libcamera-@0@'.format(libcamera_version))
 
 #
 # Doxygen
@@ -9,7 +9,7 @@ doxygen = find_program('doxygen', required : false)
 
 if doxygen.found()
     cdata = configuration_data()
-    cdata.set('VERSION', meson.project_version())
+    cdata.set('VERSION', 'v@0@'.format(libcamera_git_version))
     cdata.set('TOP_SRCDIR', meson.source_root())
     cdata.set('TOP_BUILDDIR', meson.build_root())
 
@@ -48,7 +48,7 @@ if sphinx.found()
         'index.rst',
     ]
 
-    release = 'release=' + meson.project_version()
+    release = 'release=v' + libcamera_git_version
 
     custom_target('documentation',
                   command : [sphinx, '-D', release, '-q', '-W', '-b', 'html',
diff --git a/include/libcamera/camera_manager.h b/include/libcamera/camera_manager.h
index cf3a85ae8fe3..633d27d17ebf 100644
--- a/include/libcamera/camera_manager.h
+++ b/include/libcamera/camera_manager.h
@@ -31,6 +31,7 @@ public:
 	void removeCamera(Camera *camera);
 
 	static CameraManager *instance();
+	static const std::string &version() { return version_; }
 
 	void setEventDispatcher(std::unique_ptr<EventDispatcher> dispatcher);
 	EventDispatcher *eventDispatcher();
@@ -46,6 +47,8 @@ private:
 	std::vector<std::shared_ptr<Camera>> cameras_;
 
 	std::unique_ptr<EventDispatcher> dispatcher_;
+
+	static const std::string version_;
 };
 
 } /* namespace libcamera */
diff --git a/include/libcamera/gen-header.sh b/include/libcamera/gen-header.sh
index e171c08c20b8..ccd3414bd8ca 100755
--- a/include/libcamera/gen-header.sh
+++ b/include/libcamera/gen-header.sh
@@ -16,8 +16,12 @@ cat <<EOF > "$dst_file"
 
 EOF
 
-for header in "$src_dir"/*.h ; do
+headers=$(for header in $"$src_dir"/*.h ; do
 	header=$(basename "$header")
+	echo $header
+done ; echo "version.h" | sort)
+
+for header in $(echo $headers) ; do
 	echo "#include <libcamera/$header>" >> "$dst_file"
 done
 
diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build
index 6f81f1117318..972513fc2df5 100644
--- a/include/libcamera/meson.build
+++ b/include/libcamera/meson.build
@@ -16,13 +16,6 @@ libcamera_api = files([
     'timer.h',
 ])
 
-gen_version = join_paths(meson.source_root(), 'utils', 'gen-version.sh')
-
-version_h = vcs_tag(command : [gen_version, meson.current_source_dir()],
-                    input : 'version.h.in',
-                    output : 'version.h',
-                    fallback : 'v0.0')
-
 gen_header = files('gen-header.sh')
 
 libcamera_h = custom_target('gen-header',
@@ -32,5 +25,16 @@ libcamera_h = custom_target('gen-header',
                             install : true,
                             install_dir : 'include/libcamera')
 
+version = libcamera_version.split('.')
+libcamera_version_config = configuration_data()
+libcamera_version_config.set('LIBCAMERA_VERSION_MAJOR', version[0])
+libcamera_version_config.set('LIBCAMERA_VERSION_MINOR', version[1])
+libcamera_version_config.set('LIBCAMERA_VERSION_PATCH', version[2])
+
+configure_file(input : 'version.h.in',
+               output : 'version.h',
+               configuration : libcamera_version_config,
+               install_dir : 'include/libcamera')
+
 install_headers(libcamera_api,
                 subdir : 'libcamera')
diff --git a/include/libcamera/version.h.in b/include/libcamera/version.h.in
index e49b36962aed..5e9a30911d12 100644
--- a/include/libcamera/version.h.in
+++ b/include/libcamera/version.h.in
@@ -9,14 +9,8 @@
 #ifndef __LIBCAMERA_VERSION_H__
 #define __LIBCAMERA_VERSION_H__
 
-#include <string>
-
-#define LIBCAMERA_VERSION "@VCS_TAG@"
-
-namespace libcamera {
-
-extern const std::string version;
-
-} /* namespace libcamera */
+#define LIBCAMERA_VERSION_MAJOR		@LIBCAMERA_VERSION_MAJOR@
+#define LIBCAMERA_VERSION_MINOR		@LIBCAMERA_VERSION_MINOR@
+#define LIBCAMERA_VERSION_PATCH		@LIBCAMERA_VERSION_PATCH@
 
 #endif /* __LIBCAMERA_VERSION_H__ */
diff --git a/meson.build b/meson.build
index 342b3cc76a93..8f3d0ce91cb8 100644
--- a/meson.build
+++ b/meson.build
@@ -1,8 +1,6 @@
 project('libcamera', 'c', 'cpp',
     meson_version : '>= 0.40',
-    version : run_command('utils/gen-version.sh',
-                          '@0@'.format(meson.source_root()),
-                          check : true).stdout().strip(),
+    version : '0.0.0',
     default_options : [
         'werror=true',
         'warning_level=2',
@@ -10,6 +8,21 @@ project('libcamera', 'c', 'cpp',
     ],
     license : 'LGPL 2.1+')
 
+# Generate version information. The libcamera_git_version variable contains the
+# full version with git patch count and SHA1 (e.g. 1.2.3+211-c94a24f4), while
+# the libcamera_version variable contains the major.minor.patch (e.g. 1.2.3)
+# only. If the source tree isn't under git control, or if it matches the last
+# git version tag, the build metadata (e.g. +211-c94a24f4) is omitted from
+# libcamera_git_version.
+libcamera_git_version = run_command('utils/gen-version.sh',
+                                    meson.source_root()).stdout().strip()
+if libcamera_git_version == ''
+    libcamera_git_version = meson.project_version()
+endif
+
+libcamera_version = libcamera_git_version.split('+')[0]
+
+# Configure the build environment.
 cc = meson.get_compiler('c')
 config_h = configuration_data()
 
diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
index c5da46b4062d..337496c21cfc 100644
--- a/src/libcamera/camera_manager.cpp
+++ b/src/libcamera/camera_manager.cpp
@@ -9,7 +9,6 @@
 
 #include <libcamera/camera.h>
 #include <libcamera/event_dispatcher.h>
-#include <libcamera/version.h>
 
 #include "device_enumerator.h"
 #include "event_dispatcher_poll.h"
@@ -26,11 +25,6 @@ namespace libcamera {
 
 LOG_DEFINE_CATEGORY(Camera)
 
-/**
- * \brief The library global version string
- */
-const std::string version(LIBCAMERA_VERSION);
-
 /**
  * \class CameraManager
  * \brief Provide access and manage all cameras in the system
@@ -85,7 +79,7 @@ int CameraManager::start()
 	if (enumerator_)
 		return -EBUSY;
 
-	LOG(Camera, Info) << "libcamera " << version;
+	LOG(Camera, Info) << "libcamera " << version_;
 
 	enumerator_ = DeviceEnumerator::create();
 	if (!enumerator_ || enumerator_->enumerate())
@@ -232,6 +226,12 @@ CameraManager *CameraManager::instance()
 	return &manager;
 }
 
+/**
+ * \fn const std::string &CameraManager::version()
+ * \brief Retrieve the libcamera version string
+ * \return The libcamera version string
+ */
+
 /**
  * \brief Set the event dispatcher
  * \param[in] dispatcher Pointer to the event dispatcher
diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
index 336f4f066fac..97ff86e2167f 100644
--- a/src/libcamera/meson.build
+++ b/src/libcamera/meson.build
@@ -79,8 +79,16 @@ control_types_cpp = custom_target('control_types_cpp',
 
 libcamera_sources += control_types_cpp
 
+gen_version = join_paths(meson.source_root(), 'utils', 'gen-version.sh')
+
+version_cpp = vcs_tag(command : [gen_version, meson.source_root()],
+                      input : 'version.cpp.in',
+                      output : 'version.cpp',
+                      fallback : meson.project_version())
+
+libcamera_sources += version_cpp
+
 libcamera_deps = [
-    declare_dependency(sources : version_h),
     cc.find_library('dl'),
     libudev,
 ]
diff --git a/src/libcamera/version.cpp.in b/src/libcamera/version.cpp.in
new file mode 100644
index 000000000000..5aec08a189f3
--- /dev/null
+++ b/src/libcamera/version.cpp.in
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * version.cpp - libcamera version
+ *
+ * This file is auto-generated. Do not edit.
+ */
+
+#include <libcamera/camera_manager.h>
+
+namespace libcamera {
+
+const std::string CameraManager::version_("v@VCS_TAG@");
+
+} /* namespace libcamera */
diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
index b2f3a1f324f2..907d2423ed15 100644
--- a/src/qcam/main_window.cpp
+++ b/src/qcam/main_window.cpp
@@ -26,7 +26,7 @@ MainWindow::MainWindow(const OptionsParser::Options &options)
 {
 	int ret;
 
-	title_ = "QCam " + QString::fromStdString(libcamera::version);
+	title_ = "QCam " + QString::fromStdString(CameraManager::version());
 	setWindowTitle(title_);
 	connect(&titleTimer_, SIGNAL(timeout()), this, SLOT(updateTitle()));
 
diff --git a/utils/gen-version.sh b/utils/gen-version.sh
index b3003d7a80d3..1800d71642c0 100755
--- a/utils/gen-version.sh
+++ b/utils/gen-version.sh
@@ -8,6 +8,9 @@ then
 	cd "$1" 2>/dev/null || exit 1
 fi
 
+# Bail out if the directory isn't under git control
+git rev-parse --git-dir >/dev/null 2>&1 || exit 1
+
 # Get a short description from the tree.
 version=$(git describe --abbrev=8 --match "v[0-9]*" 2>/dev/null)
 
@@ -16,22 +19,16 @@ then
 	# Handle an un-tagged repository
 	sha=$(git describe --abbrev=8 --always 2>/dev/null)
 	commits=$(git log --oneline | wc -l 2>/dev/null)
-	version=v0.0.$commits.$sha
+	version="v0.0.0-$commits-g$sha"
 fi
 
-# Prevent changed timestamps causing -dirty labels
+# Append a '-dirty' suffix is the working tree is dirty. Prevent false
+# positives due to changed timestamps by running git update-index.
 git update-index --refresh > /dev/null 2>&1
-dirty=$(git diff-index --name-only HEAD 2>/dev/null) || dirty=
+git diff-index --quiet HEAD || version="$version-dirty"
 
-# Strip the 'g', and replace the preceeding '-' with a '+' to denote a label
-version=$(echo "$version" | sed -e 's/-g/+/g')
-
-# Fix the '-' (the patch count) to a '.' as a version increment.
-version=$(echo "$version" | sed -e 's/-/./g')
-
-if [ -n "$dirty" ]
-then
-	version=$version-dirty
-fi
+# Replace first '-' with a '+' to denote build metadata, strip the 'g' in from
+# of the git SHA1 and remove the initial 'v'.
+version=$(echo "$version" | sed -e 's/-/+/' | sed -e 's/-g/-/' | cut -c 2-)
 
 echo "$version"
