nvcc vs. gcc/g++ when using float2, float3 etc

Given that I am making heavy use of float2, float3 etc. datatypes on the GPU side, I want to mirror some of it on the CPU side, i.e., in host code. Upon doing so, I have run into problems when using g++/gcc to compile the host side, whereas everything is fine when using nvcc. Consider the following example of pure host code

#include <stdlib.h>

#include <stdio.h>

#include <cuda_runtime.h> // for gcc/g++

float2 sqrt(float2 a) {

  return make_float2(sqrt(a.x),sqrt(a.y));


int main () 


  double value,sqrtvalue;

  float  valuef,sqrtvaluef;

  float2 value2,sqrtvalue2;

printf("Enter a floating point number:\n");







printf("values: %f %f %f %f\n",value,valuef,value2.x,value2.y);  

  printf("sqrts : %f %f %f %f\n",sqrtvalue,sqrtvaluef,sqrtvalue2.x,sqrtvalue2.y);


If this is saved as test.cu and compiled with “nvcc test.cu” everything works as expected. If however I rename the file to test.cpp and compile with “g++ -I /usr/local/cuda/include/ test.cpp”, I get various errors “conversion from ‘float’ to non-scalar type ‘float2’ requested”. What am I overlooking? Can I get g++ to compile the above snippet? Am I just missing an include file (I thought that cuda_runtime.h pulls in all relevant include files …)



I think you will find the problem stems from the lack of prior declaration of sqrt(). If you include and compile using g++, the problem will probably go away. In C that will definitely not work.

Thank you – that was it of course. With a “too” heavy fortran77/95/etc. background, the need

for #include-s keeps tripping me up. That the example could never work in plain C was always

clear. So now I know that nvcc automatically pulls in cmath(math.h) in addition to cuda_runtime.h.

This still makes me wonder whether there is a side by side comparison of what nvcc does differently when

compiling a *.cu file compared to g++ compiling the same file named


Again thanks – Stefan

The nvcc documentation does talk about the compilation trajectory used for different input files. You can use the --verbose option with nvcc to see all the interim compilation stages, and the --x option allows the compilation dialect to be explicitly set rather than chosen automatically. So you should be able to do something like

nvcc -c --verbose --x cu <options> somefile

nvcc -c --verbose --x c  <options> somefile

nvcc -c --verbose --x c++ <options> somefile

and see what changes occur in the compilation trajectory when a given piece of code is compiled as c, c++ or a cuda containing file. That might provide some insight.