Martin Kruse
Martin Kruse

Reputation: 103

C / CUDA implementation with external variables produces linker error (unresolved external symbol)

I have a C-Project, which I would like to boost using a CUDA-module. But somehow, the externally defined variables can not be resolved. I am using Microsoft Visual C++ 2010 Express and CUDA Toolkit 5.0.

The following shows my minimal (not) working example:

main.c:

#include "main.h"
#include <stdio.h>
#include "cuda_test.cu"

int main( int argc, const char* argv[] )
{
    testfunc();
    return 1;
}

main.h:

#ifndef main_h
#define main_h

extern float PI;

#endif

testfile.c:

#include "main.h"

float PI = 3;

cuda_test.cu:

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

void testfunc()
{
    printf("Hello from cudafile: %E", PI);
}

This yields the following error:

1>------ Build started: Project: cuda_min, Configuration: Debug Win32 ------
1>cuda_test.cu.obj : error LNK2001: unresolved external symbol "float PI" (?PI@@3MA)
1>D:\Backup\diplomarbeit\cuda_xanthos\cuda_min\Debug\cuda_min.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

When passing the variable PI to the function testfunc, I get the desired behavior. This is what I am doing in my project (which actually uses the CUDA-device), but I really do not want to pass about 20 variables to my functions.

I suppose I am missing some setting for nvcc...

Any help would be much appreciated.

Upvotes: 0

Views: 414

Answers (1)

Roger Dahl
Roger Dahl

Reputation: 15724

.cu is compiled and linked as .cpp, not .c. So, you can either rename your .c files to .cpp, or use extern "C" in your .cu file.

Upvotes: 1

Related Questions