# Unable to compile kernel with comments

**URL:** <https://forums.developer.nvidia.com/t/unable-to-compile-kernel-with-comments/13336>\
**Category:** CUDA Programming and Performance\
**Created:** [November 19, 2009, 9:45am UTC](https://forums.developer.nvidia.com/t/unable-to-compile-kernel-with-comments/13336 "2009-11-19T09:45:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![whites11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@whites11](https://forums.developer.nvidia.com/u/whites11)\
**Post date:** [November 19, 2009, 9:45am UTC](https://forums.developer.nvidia.com/t/unable-to-compile-kernel-with-comments/13336/1 "2009-11-19T09:45:55Z")

</div>

Hi everybody.

first of all my working environment:

OpenCL on arch linux with latest nvidia sdk, nvidia drivers for linux version 190.29.

i can compile and execute kernels with no comments in it such as

```auto
__kernel void update(__global float *data)

{

}

```

but if i try to run the same code with this kernel:

```auto
__kernel void update(__global float *data)

{

//this is a comment

}

```

i get a build error as if i have an error on the source code (error 1 on clBuildProgram).

ideas?

please ask if you need more info

---

<div class="post-metadata">

**Author:** ![revers](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@revers](https://forums.developer.nvidia.com/u/revers)\
**Post date:** [November 19, 2009, 1:17pm UTC](https://forums.developer.nvidia.com/t/unable-to-compile-kernel-with-comments/13336/2 "2009-11-19T13:17:27Z")

</div>

I’m not sure if it can be that. But I guess depending on how you give the program source to the compiler, if it gives the whole code in a unique line then the end your code is still considered as commented…

Maybe you can try this :

```auto
__kernel void update(__global float *data)

{

/*this is a comment*/

}

```

---

<div class="post-metadata">

**Author:** ![whites11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@whites11](https://forums.developer.nvidia.com/u/whites11)\
**Post date:** [November 19, 2009, 3:08pm UTC](https://forums.developer.nvidia.com/t/unable-to-compile-kernel-with-comments/13336/3 "2009-11-19T15:08:22Z")

</div>

Thanks for the answer.

your suggestions has been very useful.

in fact, there was an error in the function i used to load a \*.cl file.

now i corrected it, and now it loads the file correctly (at least i think so).

anyway, the problem is not solved :(

let me show you what it happens:

if i try to run my code with this kernel, everything goes fine:

```auto
$ ./OpenCLcpp

__kernel void increment(

				__global float *data

){

		const uint index = get_global_id(0);

		// comment

		data[index] = data[index]+1;

}

Size is 136

input 0 output 10

input 0 output 10

input 0 output 10

input 0 output 10

input 0 output 10

input 0 output 10

input 0 output 10

input 0 output 10

input 0 output 10

input 0 output 10

Execution finished correctly

```

If i try to execute the same code with this kernel, an error occours!!!

```auto
$ ./OpenCLcpp

__kernel void increment(

				__global float *data

){

		const uint index = get_global_id(0);

		// comment long

		data[index] = data[index]+1;

}

Size is 141

ERROR: clCreateKernel(-46)

```

-46 is CL\_INVALID\_KERNEL\_NAME but the name of the kernel and the cpp code is the same!!!

this is the function i use to load the \*.cl sources:

```auto
#include <fstream>

std::pair<const char*, unsigned int> ReadClFromFile(const char* path)

{

	std::string str,KernelSource;

	std::ifstream in;

	if(! in)

	{

		std::cerr << "File not found??" << std::endl;

	}

	in.open(path);

	std::getline(in,str);

	while ( in ) {

		KernelSource += str + "\n";

		std::getline(in,str);

	}

	in.close();

	std::cout << KernelSource << std::endl;

	std::cout << "Size is " << KernelSource.length() << std::endl;

	return std::make_pair(KernelSource.c_str(), KernelSource.length());

}

```

---

<div class="post-metadata">

**Author:** ![sagrailo](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sagrailo](https://forums.developer.nvidia.com/u/sagrailo)\
**Post date:** [November 19, 2009, 3:31pm UTC](https://forums.developer.nvidia.com/t/unable-to-compile-kernel-with-comments/13336/4 "2009-11-19T15:31:59Z")

</div>

Are you aware that your ReadClFromFile() function is returning pointer from a string object (KernelSource) that is destructed at the end of the function? Change the first pair element to std::string, and see if that helps.

---

<div class="post-metadata">

**Author:** ![whites11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@whites11](https://forums.developer.nvidia.com/u/whites11)\
**Post date:** [November 19, 2009, 4:22pm UTC](https://forums.developer.nvidia.com/t/unable-to-compile-kernel-with-comments/13336/5 "2009-11-19T16:22:58Z")

</div>

> [@](#):
>
> Are you aware that your ReadClFromFile() function is returning pointer from a string object (KernelSource) that is destructed at the end of the function? Change the first pair element to std::string, and see if that helps.

it worked, thanks a lot. very noob problem :)
