CMake and CUDA: Separate Compilation of Class Constructor on Device Fail During Linking
When working with CUDA and C++, it is often necessary to separately compile the class constructor for the device. This can be done using the __global__
keyword. However, if the constructor is not properly declared, it can lead to linking errors.
The Problem
The problem occurs when the class constructor is declared as follows:
__global__ MyClass::MyClass(int a, int b) { // ... }
This declaration is incorrect because the __global__
keyword should only be used for functions that are executed on the device. The class constructor is not a function, so it should not be declared as __global__
.
The Solution
To fix the problem, the class constructor should be declared as follows:
MyClass::MyClass(int a, int b) { // ... }
This declaration is correct because it does not use the __global__
keyword.
Conclusion
By following these steps, you can avoid linking errors when separately compiling the class constructor for the device.