Recently, I compile s simple main.cpp files with CMakeLists.txt, but I failed.
main.cpp is as follow:
#include
#include <stdio.h>
global void foo()
{
printf(“CUDA!!!\n”);
}
void useCUDA()
{
foo<<<1,5>>>();
cudaDeviceSynchronize();
}
int main() {
useCUDA();
return 0;
}
CMakeLists.txt is as follow:
CMakeLists.txt for G4CU project
project(test_cuda_project)
required cmake version
cmake_minimum_required(VERSION 2.8)
packages
find_package(CUDA)
set(CUDA_SEPARABLE_COMPILATION ON)
include_directories(${CUDA_INCLUDE_DIRS})
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
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})
When I use this CMakeLists.txt to compile the main.cpp, I failed and the errors are as follow:
/home/manifold/CUDA_Test1/test1/main.cpp:3:3: error: ‘global’ does not name a type
global void foo()
^
/home/manifold/CUDA_Test1/test1/main.cpp: In function ‘void useCUDA()’:
/home/manifold/CUDA_Test1/test1/main.cpp:11:4: error: ‘foo’ was not declared in this scope
foo<<<1,5>>>();
^
/home/manifold/CUDA_Test1/test1/main.cpp:11:9: error: expected primary-expression before ‘<’ token
foo<<<1,5>>>();
^
/home/manifold/CUDA_Test1/test1/main.cpp:11:15: error: expected primary-expression before ‘>’ token
foo<<<1,5>>>();
^
/home/manifold/CUDA_Test1/test1/main.cpp:11:17: error: expected primary-expression before ‘)’ token
foo<<<1,5>>>();
^
/home/manifold/CUDA_Test1/test1/main.cpp:12:26: error: ‘cudaDeviceSynchronize’ was not declared in this scope
cudaDeviceSynchronize();
^
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
But I can use the command 'nvcc -o main main.cpp -x=cu’in the terminal to compile main.cpp successfully.
Could you someone tell me how can I change my CMakeList.txt to compile main.cpp?
my os is ubuntu16.04 and cmake version is 3.5.1, cuda version is 9.0