Cmake+Ubuntu+CUDA+C混合编译

一. 环境
Jetson TK1 开发套件+Ubuntu 14.04
Cmake安装:sudo apt-get install cmake

Cmake教程:http://sewm.pku.edu.cn/src/paradise/reference/CMake%20Practice.pdf

二.示例
1.目录树

2.源码
/cuda/cube.cu
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include 
#include "cube.cuh"

__global__ void cube_core(int *dev_a, int *dev_b){
	int tid=blockIdx.x;
	int tmp=*(dev_a+tid);
	*(dev_b+tid)=tmp*tmp*tmp;	
}


void cube(int result[], int n){
	int a[n];
	for(int i=0;i>>(dev_a,dev_b);
	cudaMemcpy((void **)&result[0],dev_b,n*sizeof(int),cudaMemcpyDeviceToHost);
}
/cuda/cu.cuh
#ifndef CUBE_H
	#define CUBE_H
	extern "C" void cube(int *result, int n);
#endif
/cuda/CMakeLists.txt
project(CUBE)
cmake_minimum_required(VERSION 2.8)
find_package(CUDA REQUIRED)

file(GLOB_RECURSE CURRENT_HEADERS *.cuh)
file(GLOB CURRENT_SOURCES *.cu)

list(APPEND CUDA_NVCC_FLAGS "-std=c++11")

source_group("Include" FILES ${CURRENT_HEADERS})
source_group("Source" FILES ${CURRENT_SOURCES})

cuda_add_library(cube STATIC ${CURRENT_HEADERS} ${CURRENT_SOURCES})

#not used in this demo
TARGET_LINK_LIBRARIES(cube pthread)
TARGET_LINK_LIBRARIES(cube cufft)
TARGET_LINK_LIBRARIES(cube pcap)
/src/print.c
#include
#include"print.h"

void print(int *a, int n){
	printf("\tHola\n");
	for(int i=0;i
/src/print.h
#ifndef PRINT_H
	#define PRINT_H
	extern "C" void print(int *a, int n);
#endif
/src/CMakeLists.txt
project(PRINT)
cmake_minimum_required(VERSION 2.8)

file(GLOB_RECURSE CURRENT_HEADERS *.h)
file(GLOB CURRENT_SOURCES *.c)

set(CMAKE_C_COMPILER g++)
list(APPEND CMAKE_C_FLAGS "-lstdc++")

#source_group("Include" FILES ${CURRENT_HEADERS})
#source_group("Source" FILES ${CURRENT_SOURCES})

add_library(print STATIC ${CURRENT_HEADERS} ${CURRENT_SOURCES})
/main.c
#include
extern "C" void cube(int *a, int n);
extern "C" void print(int *a, int n);

int main(){
	int N=10;
	int a[10];
	cube(a,N);
	print(&a[0],N);
}
/CMakeLists.txt
project(PRO)
cmake_minimum_required(VERSION 2.8)
add_subdirectory(cuda ./cuda/bin)
add_subdirectory(src ./src/bin)
set(EXTRA_LIBS ${EXTRA_LIBS} cube print)
SET(CMAKE_C_COMPILER g++)
list(APPEND CMAKE_C_FLAGS "-lstdc++")

add_executable(pro main.c)
target_link_libraries(pro ${EXTRA_LIBS})
3.切换到build目录,编译,链接,执行
cd build
cmake ..
make
./pro
4.结果
Cmake+Ubuntu+CUDA+C混合编译_第1张图片

三.问题及解决
1. -lcufft not found
拷贝cuda库里面的libcufft文件,并生成软链接
sudo cp  /usr/local/cuda-6.5/lib/libcufft.so.6.5.53 	/usr/lib/arm-linux-gnueabihf/libcufft.so.6.5.53
sudo ln –s /usr/lib/arm-linux-gnueabihf/libcufft.so.6.5.53 /usr/lib/arm-linux-gnueabihf/libcufft.so.6.5
sudo ln –s /usr/lib/arm-linux-gnueabihf/libcufft.so.6.5  /usr/lib/arm-linux-gnueabihf/libcufft.so
2. /usr/bin/ld:cuda/libgpu.a(gpu_generated_active.cu.o): undefined reference to symbol '_ZNSs4_Rep9_S_createEjjRKSaIcE@@GLIBCXX_3.4'

//usr/lib/arm-linux-gnueabihf/libstdc++.so.6:error adding symbols: DSO missing from command line

CMakeLists.txt添加:set(CMAKE_C_COMPILER g++)  # gcc使用c++库。

注意:直接用gcc进行链接时,-lstdc++放在最后,因为gcc是按从右到左的顺序查找依赖库的。
3./home/ubuntu/Documents/Active/active_uart/cuda/active.cu(1535): error:namespace "std" has no member "to_string"
/cuda/CMakeLists.txt 添加:list(APPEND  CUDA_NVCC_FLAGS  “-std=c++11”)



 
  



你可能感兴趣的:(软件安装与使用)