CUDA Compile Error Fix

Published on
Last updated on
Technical
OS

Background

After upgrading my home OS to Debian 13 last month, I encountered persistent compilation failures with CUDA-dependent software (specifically Ollama and mistral.rs). The primary issue stemmed from GCC version incompatibility - Debian 13 defaults to GCC 14, which is not compatible with CUDA tooling and the mathematical library.

Installing GCC 13

The first step was to install GCC 13, as the default GCC 14 in Debian 13 is not compatible with CUDA tooling. This can be done using the following command:

Terminal window
sudo apt install gcc-13 g++-13

After installing GCC 13, I discovered that nvcc always uses the system’s default GCC compiler. Since there was no direct way to configure nvcc to use GCC 13 specifically, I implemented a solution using Debian’s alternatives system to manage multiple GCC versions.

Terminal window
echo "Setting up alternatives for GCC 11..."
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11 \
--slave /usr/bin/g++ g++ /usr/bin/g++-11 \
--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-11 \
--slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-11 \
--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-11 \
--slave /usr/share/man/man1/gcc.1.gz gcc.1.gz /usr/share/man/man1/gcc-11.1.gz \
--slave /usr/share/man/man1/g++.1.gz g++.1.gz /usr/share/man/man1/g++-11.1.gz
echo "Setting up alternatives for GCC 12..."
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12 \
--slave /usr/bin/g++ g++ /usr/bin/g++-12 \
--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-12 \
--slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-12 \
--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-12 \
--slave /usr/share/man/man1/gcc.1.gz gcc.1.gz /usr/share/man/man1/gcc-12.1.gz \
--slave /usr/share/man/man1/g++.1.gz g++.1.gz /usr/share/man/man1/g++-12.1.gz
echo "Setting up alternatives for GCC 13..."
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13 \
--slave /usr/bin/g++ g++ /usr/bin/g++-13 \
--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-13 \
--slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-13 \
--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-13 \
--slave /usr/share/man/man1/gcc.1.gz gcc.1.gz /usr/share/man/man1/gcc-13.1.gz \
--slave /usr/share/man/man1/g++.1.gz g++.1.gz /usr/share/man/man1/g++-13.1.gz
echo "Setting up alternatives for GCC 14..."
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 14 \
--slave /usr/bin/g++ g++ /usr/bin/g++-14 \
--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-14 \
--slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-14 \
--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-14 \
--slave /usr/share/man/man1/gcc.1.gz gcc.1.gz /usr/share/man/man1/gcc-14.1.gz \
--slave /usr/share/man/man1/g++.1.gz g++.1.gz /usr/share/man/man1/g++-14.1.gz

After configuring the alternatives, set GCC 13 as the default compiler before building:

Terminal window
sudo update-alternatives --set gcc /usr/bin/gcc-13

After building, switch back to GCC 14 as the default compiler:

Terminal window
sudo update-alternatives --set gcc /usr/bin/gcc-14

Math Library Compatibility Issue

After resolving the GCC version issue, I still encountered the following build error. Unsure of its origin, I searched online and found a Stack Overflow post titled NVCC compilation error: exception specification is incompatible with that of previous function “cospi” that described the exact same error.

#$ "/usr/bin"/gcc-13 -std=c++17 -D__CUDA_ARCH_LIST__=610 -D__NV_LEGACY_LAUNCH -E -x c++ -D__CUDACC__ -D__NVCC__ -D__CUDACC_EXTENDED_LAMBDA__ -D__CUDACC_RELAXED_CONSTEXPR__ -fPIC -O3 "-I/usr/local/cuda/bin/../targets/x86_64-linux/include" -U "__CUDA_NO_HALF_OPERATORS__" -U "__CUDA_NO_HALF_CONVERSIONS__" -U "__CUDA_NO_HALF2_OPERATORS__" -U "__CUDA_NO_BFLOAT16_CONVERSIONS__" -D__CUDACC_VER_MAJOR__=12 -D__CUDACC_VER_MINOR__=4 -D__CUDACC_VER_BUILD__=131 -D__CUDA_API_VER_MAJOR__=12 -D__CUDA_API_VER_MINOR__=4 -DCUDA_API_PER_THREAD_DEFAULT_STREAM=1 -D__NVCC_DIAG_PRAGMA_SUPPORT__=1 -include "cuda_runtime.h" -m64 "kernels/bitsandbytes/dequant.cu" -o "/tmp/tmpxft_0000732d_00000000-5_dequant.cpp4.ii"
/usr/include/x86_64-linux-gnu/bits/mathcalls.h(79): error: exception specification is incompatible with that of previous function "cospi" (declared at line 5554 of /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/math_functions.h)
extern double cospi (double __x) noexcept (true); extern double __cospi (double __x) noexcept (true);
^
/usr/include/x86_64-linux-gnu/bits/mathcalls.h(81): error: exception specification is incompatible with that of previous function "sinpi" (declared at line 5442 of /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/math_functions.h)
extern double sinpi (double __x) noexcept (true); extern double __sinpi (double __x) noexcept (true);
^
/usr/include/x86_64-linux-gnu/bits/mathcalls.h(79): error: exception specification is incompatible with that of previous function "cospif" (declared at line 5606 of /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/math_functions.h)
extern float cospif (float __x) noexcept (true); extern float __cospif (float __x) noexcept (true);
^
/usr/include/x86_64-linux-gnu/bits/mathcalls.h(81): error: exception specification is incompatible with that of previous function "sinpif" (declared at line 5502 of /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/math_functions.h)
extern float sinpif (float __x) noexcept (true); extern float __sinpif (float __x) noexcept (true);
^

The solution provided by this page is to patch /usr/local/cuda/include/crt/math_functions.h with the patch cuda_glibc_241_compat.diff

--- a/math_functions.h 00:02:30.815134398 +0300
+++ b/math_functions.h 00:03:30.815134398 +0300
@@ -2547,7 +2547,7 @@
*
* \note_accuracy_double
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double sinpi(double x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double sinpi(double x) noexcept (true);
/**
* \ingroup CUDA_MATH_SINGLE
* \brief Calculate the sine of the input argument
@@ -2570,7 +2570,7 @@
*
* \note_accuracy_single
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sinpif(float x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sinpif(float x) noexcept (true);
/**
* \ingroup CUDA_MATH_DOUBLE
* \brief Calculate the cosine of the input argument
@@ -2592,7 +2592,7 @@
*
* \note_accuracy_double
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double cospi(double x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double cospi(double x) noexcept (true);
/**
* \ingroup CUDA_MATH_SINGLE
* \brief Calculate the cosine of the input argument
@@ -2614,7 +2614,7 @@
*
* \note_accuracy_single
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float cospif(float x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float cospif(float x) noexcept (true);
/**
* \ingroup CUDA_MATH_DOUBLE
* \brief Calculate the sine and cosine of the first input argument

Apply the patch to resolve the math library compatibility issues:

Terminal window
pushd /usr/local/cuda/include/crt
patch < /path/to/cuda_glibc_241_compat.diff
popd

if we can add each window id to simply the implementation,

both settings and caption window should add window id too

Back to Blog