开发者

How to pass a struct from C++ to C?

Updated: main.h

typedef struct
{
    float x;
    float y;
    float z;
}vec3;
const int sizeOfGrid = 20000;
vec3 *grid[sizeOfGrid];//assume initialized

main.cpp

#include "main.h"
extern "C" void cudaTranslate(vec3 *x);
void display()
{
    cudaTranslate(grid);
}

lineCuda.cu

#include <stdio.h>
#include <assert.h>
#include <cuda.h>
#include "main.h"

extern "C" void c开发者_运维技巧udaTranslate(vec3 *x)
{

}

getting:

main.obj : error LNK2005: "struct vec3 * * grid" (?grid@@3PAPAUvec3@@A) already defined in lineCuda.obj

fatal error LNK1169: one or more multiply defined symbols found


Move grid to main.cpp. Pass it to lineCuda.cu. Problem solved.

Updated: main.h

typedef struct
{
    float x;
    float y;
    float z;
}vec3;
const int sizeOfGrid = 20000;

main.cpp

#include "main.h"
vec3 *grid[sizeOfGrid];//assume initialized
extern "C" void cudaTranslate(vec3 *x);
void display()
{
    cudaTranslate(grid);
}


You could make this a lot simpler by removing extern "C" everywhere and just use C++ bindings.

Having said that, you actually have a multiply defined symbol grid because you are including the file main.h in two translation units. Move the line vec3 *grid[sizeOfGrid] to main.cpp.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜