# how to call kernal in .cpp file

**URL:** <https://forums.developer.nvidia.com/t/how-to-call-kernal-in-cpp-file/11927>\
**Category:** CUDA Programming and Performance\
**Created:** [September 2, 2009, 4:47pm UTC](https://forums.developer.nvidia.com/t/how-to-call-kernal-in-cpp-file/11927 "2009-09-02T16:47:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![answill](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@answill](https://forums.developer.nvidia.com/u/answill)\
**Post date:** [September 2, 2009, 4:47pm UTC](https://forums.developer.nvidia.com/t/how-to-call-kernal-in-cpp-file/11927/1 "2009-09-02T16:47:59Z")

</div>

Hi guys,

I want to utilize cuda to speed up the current project.

I have written the kernal thread. And I want to call the kernal thread in a .cpp file. But when compiling the project, there are some errors:

[yongzhao@205 threads]$ make

threads.cu:10: error: Ã¢blockDimÃ¢ was not declared in this scope  
threads.cu:10: error: Ã¢blockIdxÃ¢ was not declared in this scope  
threads.cu:10: error: Ã¢threadIdxÃ¢ was not declared in this scope

main.cpp:36: error: expected primary-expression before Ã¢\<Ã¢ token  
main.cpp:36: error: expected primary-expression before Ã¢\>Ã¢ token

make: \*\*\* [obj/release/main.cpp.o] Error 1

How to solve it?

---

<div class="post-metadata">

**Author:** ![moulik](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@moulik](https://forums.developer.nvidia.com/u/moulik)\
**Post date:** [September 2, 2009, 7:14pm UTC](https://forums.developer.nvidia.com/t/how-to-call-kernal-in-cpp-file/11927/2 "2009-09-02T19:14:24Z")

</div>

how are you linking this cpp file to the nvcc compiler? Are you using the template w/ make files provided in the SDK?

---

<div class="post-metadata">

**Author:** ![answill](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@answill](https://forums.developer.nvidia.com/u/answill)\
**Post date:** [September 2, 2009, 9:07pm UTC](https://forums.developer.nvidia.com/t/how-to-call-kernal-in-cpp-file/11927/3 "2009-09-02T21:07:17Z")

</div>

yes, I copy the make file from the /NVIDIA\_CUDA\_SDK/projects/asyncAPI and then write all of files of my project and include the common.mk

> [@](#):
>
> how are you linking this cpp file to the nvcc compiler? Are you using the template w/ make files provided in the SDK?

---

<div class="post-metadata">

**Author:** ![moulik](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@moulik](https://forums.developer.nvidia.com/u/moulik)\
**Post date:** [September 3, 2009, 7:46pm UTC](https://forums.developer.nvidia.com/t/how-to-call-kernal-in-cpp-file/11927/4 "2009-09-03T19:46:29Z")

</div>

Well, it would be helpful if you would post your code… it’s not clear just from the error message what is going wrong.

---

<div class="post-metadata">

**Author:** ![laughingrice](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@laughingrice](https://forums.developer.nvidia.com/u/laughingrice)\
**Post date:** [September 7, 2009, 11:31pm UTC](https://forums.developer.nvidia.com/t/how-to-call-kernal-in-cpp-file/11927/5 "2009-09-07T23:31:11Z")

</div>

> [@](#):
>
> Hi guys,
> 
> I want to utilize cuda to speed up the current project.
> 
> I have written the kernal thread. And I want to call the kernal thread in a .cpp file. But when compiling the project, there are some errors:
> 
> [yongzhao@205 threads]$ make
> 
> threads.cu:10: error: Ã¢blockDimÃ¢ was not declared in this scope
> 
> threads.cu:10: error: Ã¢blockIdxÃ¢ was not declared in this scope
> 
> threads.cu:10: error: Ã¢threadIdxÃ¢ was not declared in this scope
> 
> main.cpp:36: error: expected primary-expression before Ã¢\<Ã¢ token
> 
> main.cpp:36: error: expected primary-expression before Ã¢\>Ã¢ token
> 
> make: \*\*\* [obj/release/main.cpp.o] Error 1
> 
> How to solve it?

If I’m reading this correctly

1. you are trying to compile the .cu with a standard c++ compiler (g++ ?) which doesn’t recognize cuda stuff (and specifically the blockDim etc. code), or you didn’t declare your kernel as a kernel (you need to give it the **global** attribute) which seems more plausible as you didn’t get an error (or at least didn’t mention it) about not recognizing it (which g++ should complain about)

2. You are trying to make a kernel call from the cpp file which is definitely not compiled with nvcc (never tested whether nvcc would even agree to compile it as cuda code) and thus the kernel paradigm \<\<\< dimGrid, dimBlock \>\>\> is not recognized.

You should:

a. make sure the cu file is compiled with nvcc (remove the @ from the compilation rules in the make file if they are there to print the actual compilation command and submit that)

b. make sure the kernels are marked as such ( **global** )

c. make the kernel call from the .cu file and make a wrapper function to be called from the .cpp file to make that happen
