Search code examples
cudaautoconfautomake

Automake and CUDA, Include flags ignored


I am writing a simple automake example with CUDA but the implicit flags that should be added are ignored.

I have 2 files, main.cpp:

#include <stdio.h>
#include <cuda_runtime.h>

int cuda_hello_launcher();

int main() {
    cuda_hello_launcher();
    return 0;
}

and aux.cu:

#include <cuda_runtime.h>
#include <stdio.h>

__global__ void cuda_hello(){
    printf("Hello World from GPU!\n");
}

void cuda_hello_launcher(){
    cuda_hello<<<1,1>>>(); 
    cudaDeviceSynchronize();
}

These two files are inside the src directory. I also have a Makefile.am in the src directory:

bin_PROGRAMS = cuda_automake_example

cuda_automake_example_SOURCES = main.cu aux.cpp
cuda_automake_example_LDFLAGS = $(CUDA_LIBS)
cuda_automake_example_LDADD = $(cuda_automake_example_LDFLAGS)
cuda_automake_example_CXXFLAGS = $(CUDA_INCLUDE_FLAGS) $(CUDA_LIBS)

$(info $(cuda_automake_example_CXXFLAGS))
$(info $(CUDA_INCLUDE_FLAGS))

.cu.o:
    $(NVCC) -c -o $@ $<

And in the root directory (src directory is root/src): I have configure.ac:

AC_INIT([cuda_automake], [1.0], [])
AM_INIT_AUTOMAKE([foreign])

AC_PROG_CXX
AC_CONFIG_HEADERS([src/config.h])
AC_CONFIG_FILES([
 Makefile
 src/Makefile
])

AC_LANG_DEFINE([CUDA], [cuda], [CUDA], [CUDACXX],
[])

AC_ARG_WITH([cuda],
   [AS_HELP_STRING([--with-cuda=PATH], [specify CUDA installation path])],
   [with_cuda="$withval"],
   [with_cuda=""])

if test -n "$with_cuda"
then
   if test "$with_cuda" = yes
   then
      AC_MSG_ERROR([With-cuda argument requires the path to CUDA, --with-cuda=<CUDA_PATH>])
   fi

   if test -z "$with_cuda"
   then
      AC_MSG_ERROR([If you are building with CUDA GPU support, please input the CUDA path with --with-cuda=<CUDA_PATH>])
   fi

   # Extract the path from the "--with-cuda" argument
   cuda_path=$(echo "$with_cuda" | sed 's/--with-cuda=//')

   # Check if the path is valid
   if test ! -d "$cuda_path"; then
      AC_MSG_ERROR([The input for cuda-path=$cuda_path is not a valid directory])
   fi

   CUDA_INCLUDE_FLAGS="-I$with_cuda/include"
   CUDA_LIBS="-L$with_cuda/lib64 -L$with_cuda/lib -lcudart"
   NVCC="$with_cuda/bin/nvcc"
   CUDA_PATH="$with_cuda"
else
   AC_MSG_ERROR([B: $with_cuda])
fi


AC_SUBST(CUDA_INCLUDE_FLAGS)
AC_SUBST(CUDA_LIBS)
AC_SUBST(NVCC)
AC_SUBST(CUDA_PATH)

AC_OUTPUT

and a Makefile.am:

SUBDIRS = src
ACLOCAL_AMFLAGS = -I m4

When I call:

automake && autoconf  && ./configure --with-cuda=/opt/nvidia/hpc_sdk/Linux_x86_64/23.3/cuda && VERBOSE=1 make

I get the following error:

Making all in src
make[1]: Entering directory '/home/primrose/Work/aux/cuda_automake/src'
-I/opt/nvidia/hpc_sdk/Linux_x86_64/23.3/cuda/include -L/opt/nvidia/hpc_sdk/Linux_x86_64/23.3/cuda/lib64 -L/opt/nvidia/hpc_sdk/Linux_x86_64/23.3/cuda/lib -lcudart
-I/opt/nvidia/hpc_sdk/Linux_x86_64/23.3/cuda/include
make  all-am
make[2]: Entering directory '/home/primrose/Work/aux/cuda_automake/src'
-I/opt/nvidia/hpc_sdk/Linux_x86_64/23.3/cuda/include -L/opt/nvidia/hpc_sdk/Linux_x86_64/23.3/cuda/lib64 -L/opt/nvidia/hpc_sdk/Linux_x86_64/23.3/cuda/lib -lcudart
-I/opt/nvidia/hpc_sdk/Linux_x86_64/23.3/cuda/include
g++ -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cpp
main.cpp:2:10: fatal error: cuda_runtime.h: No such file or directory
    2 | #include <cuda_runtime.h>
      |          ^~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [Makefile:380: main.o] Error 1
make[2]: Leaving directory '/home/primrose/Work/aux/cuda_automake/src'
make[1]: *** [Makefile:270: all] Error 2
make[1]: Leaving directory '/home/primrose/Work/aux/cuda_automake/src'
make: *** [Makefile:337: all-recursive] Error 1

I would expect the target_CXXFLAGS of the target I am building also to include the include directory containing cuda_runtime.h as well as attached to the build command (the folder has cuda_runtime.h btw that is not a problem, I checked it with ls), but apparently it is not the case, why would it be so?


Solution

  • <target_name>_CPPFLAGS and <target_name>_CXXFLAGS are automatically added as C++ flags to the target.

    Of course, make sure you have the correct filename extensions for the files or manually define the extensions as C++ extensions.