Error in compiling pure c code in VS2012 CUDA project

Here’s my code on C.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <ctype.h>


#define m_VALUES 16 
#define M_VALUE 1 
#define T_EXP_VALUE 3 
#define SIZE_BIT 32 
#define G_VALUES (int64)pow((double)2, (double)22) 
#define NUM_ITERATIONS (int64)pow((double)2, (double)14) 

typedef unsigned long long int64; 

int64 MontgExp(int64 base, int64 exp, int64 mod); 
int64 decimal_to_binary(int64); 
void extended_euclid(int64 a, int64 b, int64 *x, int64 *y, int64 *d); 
int64 contains(int64 num, int64 *arr, int64 size); 

int64 main()
{
	int64 bigPrimeNumber = 7;
	int64 i = 0, j = 0;
	int64 bArr[m_VALUES]; 
	int64 T = (int64)pow((double)2, (double)T_EXP_VALUE);
	int64 *dArr, *cArr; 
	int64 M = 0;
	int64 *gArr; 
	int64 R = 0; 
	int64 tmp = 0;
	int k = 0;

	srand(time(NULL));
	memset(bArr, 0, m_VALUES * sizeof(int64));

	dArr = (int64*)malloc(T * sizeof(int64));
	memset(dArr, 0, T * sizeof(int64));

	cArr = (int64*)malloc(T * sizeof(int64));
	memset(cArr, 0, T * sizeof(int64));

	gArr = (int64*)malloc(G_VALUES * sizeof(int64));
	memset(gArr, 0, G_VALUES * sizeof(int64));

	for (i = 0; i < m_VALUES; i++) 
		bArr[i] = MontgExp(2, rand(), bigPrimeNumber);
	
	
	for (i = 0; i < T; i++) {
		dArr[i] = MontgExp(2, rand(), bigPrimeNumber);
		cArr[i] = bArr[i];
	}
	for (i = 0; i < T; i++) {
		M = 0;
		while (M != M_VALUE) {
			cArr[i] = (bArr[decimal_to_binary(cArr[i])] * cArr[i]) % bigPrimeNumber;
			M++;
		}
	}
	
	srand(time(NULL));
	
	for (i = 0; i < G_VALUES; i++) {
		tmp = rand();
		if (tmp != 0) {
			gArr[i] = rand();

		}
	}
	tmp = 0;
	for (i = 0; i < G_VALUES; i++) {
		k++;
		if (k >= 52428) {
			k = 0;
			printf("%c", 219);
			fflush(stdout);			
		}
		tmp = (bArr[decimal_to_binary(gArr[i])] * gArr[i]) % bigPrimeNumber;
		if (contains(tmp, cArr, T) == 1) {
			R++;
			continue;
		}
		for (j = 1; j < NUM_ITERATIONS; j++) {
			tmp = (bArr[decimal_to_binary(tmp)] * tmp) % bigPrimeNumber;
			if (contains(tmp, cArr, T) == 1) {
				R++;
				break;
			}
		}
	}
	printf ("Num of R-points is - %d", R);
	printf("Press any key...");
	getchar();
	return 0;
}

int64 MontgExp(int64 base, int64 exp, int64 mod)
{
	int64 z = 0;
    if (exp == 0) return 1;
	z = MontgExp(base, exp / 2, mod);
    if (exp % 2 == 0)
		return (z*z) % mod;
	else
		return (base*z*z) % mod;
}
int64 decimal_to_binary(int64 n)
{
   int c = 0, d = 0, count;
   char *pointer, charLSB[5];
   int64 LSB = 0, i = 0;


   count = 0;
   pointer = (char*)malloc(SIZE_BIT+1);
   memset(pointer, 0, (SIZE_BIT + 1) * sizeof(char));
   if ( pointer == NULL )
      exit(EXIT_FAILURE);
   
   memset(charLSB, 0, 5 * sizeof(char));
   for ( c = SIZE_BIT - 1 ; c >= 0 ; c-- ) {
      d = n >> c;
 
      if ( d & 1 )
         *(pointer+count) = 1 + '0';
      else
         *(pointer+count) = 0 + '0';
 
      count++;
   }
   *(pointer+count) = '\0';
   strncpy(charLSB, pointer + (32 - 4), 4);
   charLSB[4] = '\0';
   for (i = 0; i < 5; i++) {
	   if (charLSB[i] == '0')
		   LSB *= 2;
	   if (charLSB[i] == '1')
		   LSB = 2 * LSB + 1;
   }
   free(pointer);
   return LSB;
}
void extended_euclid(int64 a, int64 b, int64 *x, int64 *y, int64 *d)
{

  int64 q = 0, r = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0;

  if (b == 0) {
    *d = a, *x = 1, *y = 0;
    return;
  }

  x2 = 1, x1 = 0, y2 = 0, y1 = 1;

  while (b > 0) {
    q = a / b, r = a - q * b;
    *x = x2 - q * x1, *y = y2 - q * y1;
    a = b, b = r;
    x2 = x1, x1 = *x, y2 = y1, y1 = *y;
  }

  *d = a, *x = x2, *y = y2;
}
int64 contains(int64 num, int64 *arr, int64 size)
{
	int64 i = 0, result = 0;
	for (i = 0; i < size; i++) 
		if (*(arr + i) == num) {
			result = 1;
			break;
		}
	return result;
}

If i create VS C porject, it is compiled very well, but if i create CUDA project, there for about 20 error in functions. For example, one of the errors is connected with using strncpy (it is undefined). Help me, please, to understand, why my code is not compiled.

According to the ISO C and ISO C++ standards, this is not a valid return type for main(). Other than throwing an error for that, CUDA 8.0 shows no errors when I compile the code as posted. When I change the return type of main() to int, the code compiles fine with CUDA 8.0.

Building with MSVC:

C:\Users\Norbert\My Programs>cl /O2 /W3 doesnt_build.c
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.40219.01 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

doesnt_build.c
doesnt_build.c(38) : warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data
doesnt_build.c(66) : warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data
doesnt_build.c(127) : warning C4244: '=' : conversion from 'int64' to 'int', possible loss of data
doesnt_build.c(137) : warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for
details.
        c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\string.h(188) : see declaration of 'strncpy'
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:doesnt_build.exe

Now CUDA 8.0:

C:\Users\Norbert\My Programs>nvcc -o doesnt_build.exe  doesnt_build.cu
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
nvcc warning : nvcc support for Microsoft Visual Studio 2010 and earlier has been deprecated and is no longer being maintained
doesnt_build.cu
doesnt_build.cu(25): warning: return type of function "main" must be "int"

doesnt_build.cu(25): warning: return type of function "main" must be "int"

support for Microsoft Visual Studio 2010 has been deprecated!
doesnt_build.cu(26) : error C3874: return type of 'main' should be 'int' instead of 'int64'

Hm…i changed int64 to int, but got the same error in VS2012 (debug version). But have a lot of error still…Have you only changed

int64

to

int

? Nothing else?

I flunked clairvoyance class back in school. Maybe you could cut & paste the exact error messages you are getting from the compiler (with line numbers that match up with the code you posted), along with the exact compiler switches used. What CUDA version are you using?

Keep in mind that not all valid C code is also valid C++ code, although I don’t see how that comes into play here.

Yes, I changed “int64 main” to “int main” and your posted code compiled fine after that:

C:\Users\Norbert\My Programs>nvcc -o doesnt_build.exe  doesnt_build.cu
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
nvcc warning : nvcc support for Microsoft Visual Studio 2010 and earlier has been deprecated and is no longer being maintained
doesnt_build.cu
support for Microsoft Visual Studio 2010 has been deprecated!
   Creating library doesnt_build.lib and object doesnt_build.exp

Sorry for not posting error-messages.

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin>nvcc -o first.exe ke
rnel.cu
kernel.cu
kernel.cu(107): error: expected a ";"

kernel.cu(138): warning: parsing restarts here after previous syntax error

kernel.cu(140): error: identifier "count" is undefined

kernel.cu(141): error: expected a ")"

kernel.cu(141): error: this declaration has no storage class or type specifier

kernel.cu(141): error: variable "pointer" has already been defined

kernel.cu(142): error: this declaration has no storage class or type specifier

kernel.cu(142): error: declaration is incompatible with "char *strncpy(char *, c
onst char *, size_t)"
C:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/bin/../../VC/INCLUDE\stri
ng.h(191): here

kernel.cu(142): error: identifier "charLSB" is undefined

kernel.cu(142): error: expected a ")"

kernel.cu(143): error: expected a declaration

kernel.cu(149): warning: parsing restarts here after previous syntax error

kernel.cu(150): error: expected a declaration

kernel.cu(151): error: expected a declaration

At end of source: warning: parsing restarts here after previous syntax error

12 errors detected in the compilation of "C:/Users/user/AppData/Local/Temp/tmpxf
t_00000a3c_00000000-10_kernel.cpp1.ii".

When you see an error like this, following errors are likely just follow-on errors. In your file kernel.cu, line 107 reads:

if (exp % 2 == 0)

Correct?

I do note we are using different CUDA versions and thus different compiler versions. CUDA 7.5 was a reasonably bug-free release, however. We are also using different versions of MSVS, which compiles host code.

Well, i decided to copy all code from my question to single file kernel.cu and compile it from cmd by following command:

nvcc -o train.exe kernel.cu
kernel.cu
LINK : fatal error LNK1104: cannot open file "train.exe"

I believe exp cannot be used as a variable name in your CUDA code, for example because somehow within nvcc it might have a special meaning. exp is also the name of a math function. Try renaming that variable.

Well, it is strange, but porblem has gone away after i have installed latest CUDA toolkit 9.1…