How can I let cuda(compiler) compile *.cpp files which have type float4 by cmake?

Recently, I compile a simple c++ file:
main.cpp:
#include
int main() {
std::cout << “this is a test \n” << std::endl;
float4 a = make_float4(1.0, 2.33, 4.5, 6.5);

std::cout << a.x << a.y << a.z << a.w << std::endl;

return 0;

}
if I use the command “nvcc -o main main.cpp -x=cu”, I succeeding in compile main.cpp and the output is right.
But I use a CMakeLists.txt for compiling it and I fail in compiling it.
the CMakeLists.txt is as follow:

CMakeLists.txt for the project

project(test_cuda_project)

required cmake version

cmake_minimum_required(VERSION 2.8)

packages

find_package(CUDA)

set(CUDA_NVCC_FLAGS -arch=sm_61;-O3;-G;-g;-std=c++11;-x=cu)

file(GLOB_RECURSE CURRENT_HEADERS *.h *.hpp *.cuh)
file(GLOB CURRENT_SOURCES *.cpp *.cu)
source_group(“Include” FILES {CURRENT_HEADERS}) source_group("Source" FILES {CURRENT_SOURCES})

CUDA_ADD_EXECUTABLE(test_cuda_project {CURRENT_HEADERS} {CURRENT_SOURCES})

And the errors are as follow:
main.cpp: In function ‘int main()’:
main.cpp:4:1: error: ‘float4’ was not declared in this scope
float4 a = make_float4(1.0, 2.33, 4.5, 6.5);
^
main.cpp:6:14: error: ‘a’ was not declared in this scope
std::cout << a.x << a.y << a.z << a.w << std::endl;

Could you someone know why?

Thanks a lot and best regards for you!

https://devtalk.nvidia.com/default/topic/463919/cuda-programming-and-performance/float4-on-host-/post/3294570/

try to #include CUDA’s “vector_types.h” header from your cpp file.

If that fails, redeclare the structure in a binary compatible form (and potentally also a make_float4 function)

struct float4 { float x,y,z,w; };
static float4 make_float4(float x,float y,float z,float w) { float4 result={x,y,z,w}; return result; }

Thank you very much. I add #include<vector_types.h> in my cpp files, but I failed in compiled it. the error is as follow:
/home/manifold/CUDA_Test1/test1/main.cpp: In function ‘int main()’:
/home/manifold/CUDA_Test1/test1/main.cpp:5:43: error: ‘make_float4’ was not declared in this scope
float4 a = make_float4(1.0, 2.33, 4.5, 6.5);
^
CMakeFiles/test_cuda_project.dir/build.make:62: recipe for target ‘CMakeFiles/test_cuda_project.dir/main.cpp.o’ failed
make[2]: *** [CMakeFiles/test_cuda_project.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/test_cuda_project.dir/all’ failed
make[1]: *** [CMakeFiles/test_cuda_project.dir/all] Error 2
Makefile:83: recipe for target ‘all’ failed
make: *** [all] Error 2

I can use command “nvcc -o main main.cpp -x=cu” to succeed compile cpp files with float4 types. But I cann’t use CMakeLists.txt to compiles it. This is really confusing.

If I add
"
struct float4 { float x,y,z,w; };
static float4 make_float4(float x,float y,float z,float w) { float4 result={x,y,z,w}; return result; }"

to my cpp files . It succeed in compiling cpp files and results is right.

my os is ubuntu16.04

float4 is in vector_types.h

make_float4 is in vector_functions.h

you can discover this with some usage of grep

Note that CUDA’s float4 type is a struct with additional alignment requirements. Substituting a one-off local struct in the manner shown in #4 may or may not make a difference (e.g. on performance).

in ubuntu16.04, using “nvcc -o main main.cpp -x=cu” can succeed in compiling it, but cmake cann’t.
I think ‘set(CUDA_NVCC_FLAGS … -x=cu)’ this command don’t work. My version is CMake 3.5.1. I think Cmake has something wrong with -x=cu?