error: incomplete type is not allowed

From the book “Professional CUDA C Programming” I tried to compile the first example by command line.
I got the following error message:

E:\Cuda\Uli\CodeSamples\chapter01>nvcc -arch sm_60 hello.cu -o hello
hello.cu
e:\cuda\uli\codesamples\chapter01../common/common.h(69): error: incomplete type is not allowed

e:\cuda\uli\codesamples\chapter01../common/common.h(70): error: incomplete type is not allowed

e:\cuda\uli\codesamples\chapter01../common/common.h(71): error: identifier “gettimeofday” is undefined

3 errors detected in the compilation of “C:/Users/nickl/AppData/Local/Temp/tmpxft_00005470_00000000-12_hello.cpp1.ii”.

I tried to figure out what the problem is and this code includes ‘common.h’ from this book. I think this file is found, but in this file the lines 69 to 71 cause the Problem:

inline double seconds()
{
struct timeval tp;
struct timezone tzp;
int i = gettimeofday(&tp, &tzp);
return ((double)tp.tv_sec + (double)tp.tv_usec * 1.e-6);
}

#endif // _COMMON_H

The inline function seconds() is’n used, but the compiler seems to have a problem with struct timeval tp and struct timezone. What do I have to do to compile all the Files properely?

You appear to be working on windows, but the code you are compiling is for linux. Apparently the fact you are on windows is not being detected during compilation. gettimeofday, struct timeval, and struct timezone are part of the linux commonly used timing system. They don’t exist on windows, and the compilation of that project should have detected you were on windows and used alternate code for timing.

I wouldn’t be able to say much more without knowing exactly what kind of environment you were in, how you installed CUDA, etc.

You are right! I do use my GeForce GTX 1060 graphics card on Windows 10. I also have Visual Studio Community 17 installed. Do I have a chance, to compile the unchanged code properly?

I don’t have the code for that book. I wouldn’t be able to say without seeing all the code.

#include "../common/common.h"
#include <stdio.h>

/*
 * A simple introduction to programming in CUDA. This program prints "Hello
 * World from GPU! from 10 CUDA threads running on the GPU.
 */

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

int main(int argc, char **argv)
{
    printf("Hello World from CPU!\n");

    helloFromGPU<<<1, 10>>>();
    CHECK(cudaDeviceReset());
    return 0;
}

Here I could simply comment out the first #include and also the CHECK command. It would be tedious to edit all sample files in this way.

the problem exists in common.h, which you haven’t shown.

Here ist the requested code from Professional CUDA C Programming. Is there any way to avoid error messages?

#include <sys/time.h>

#ifndef _COMMON_H
#define _COMMON_H

#define CHECK(call)                                                            \
{                                                                              \
    const cudaError_t error = call;                                            \
    if (error != cudaSuccess)                                                  \
    {                                                                          \
        fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__);                 \
        fprintf(stderr, "code: %d, reason: %s\n", error,                       \
                cudaGetErrorString(error));                                    \
        exit(1);                                                               \
    }                                                                          \
}

#define CHECK_CUBLAS(call)                                                     \
{                                                                              \
    cublasStatus_t err;                                                        \
    if ((err = (call)) != CUBLAS_STATUS_SUCCESS)                               \
    {                                                                          \
        fprintf(stderr, "Got CUBLAS error %d at %s:%d\n", err, __FILE__,       \
                __LINE__);                                                     \
        exit(1);                                                               \
    }                                                                          \
}

#define CHECK_CURAND(call)                                                     \
{                                                                              \
    curandStatus_t err;                                                        \
    if ((err = (call)) != CURAND_STATUS_SUCCESS)                               \
    {                                                                          \
        fprintf(stderr, "Got CURAND error %d at %s:%d\n", err, __FILE__,       \
                __LINE__);                                                     \
        exit(1);                                                               \
    }                                                                          \
}

#define CHECK_CUFFT(call)                                                      \
{                                                                              \
    cufftResult err;                                                           \
    if ( (err = (call)) != CUFFT_SUCCESS)                                      \
    {                                                                          \
        fprintf(stderr, "Got CUFFT error %d at %s:%d\n", err, __FILE__,        \
                __LINE__);                                                     \
        exit(1);                                                               \
    }                                                                          \
}

#define CHECK_CUSPARSE(call)                                                   \
{                                                                              \
    cusparseStatus_t err;                                                      \
    if ((err = (call)) != CUSPARSE_STATUS_SUCCESS)                             \
    {                                                                          \
        fprintf(stderr, "Got error %d at %s:%d\n", err, __FILE__, __LINE__);   \
        cudaError_t cuda_err = cudaGetLastError();                             \
        if (cuda_err != cudaSuccess)                                           \
        {                                                                      \
            fprintf(stderr, "  CUDA error \"%s\" also detected\n",             \
                    cudaGetErrorString(cuda_err));                             \
        }                                                                      \
        exit(1);                                                               \
    }                                                                          \
}

inline double seconds()
{
    struct timeval tp;
    struct timezone tzp;
    int i = gettimeofday(&tp, &tzp);
    return ((double)tp.tv_sec + (double)tp.tv_usec * 1.e-6);
}

#endif // _COMMON_H

It’s evident that that code is designed to run on linux only, and will not run as-is on windows. This has nothing to do with CUDA per se.

Perhaps you should switch to linux, or else understand the issues being reported to you and learn how to convert host-based timing from linux to windows. There are plenty of examples on the web. This is not a CUDA issue.

You could also take it up with the authors of the book if they have any forums for feedback.

Here’s just one of many examples on the web:

[url]Migrating c program from linux to windows - Stack Overflow

You might also check to see if the book offers an alternate source code library for windows users.