Chiawen
Chiawen

Reputation: 11809

How to compile multiple files in cuda?

I did it the way I do with gcc

 nvcc a.cu ut.cu 

but the compiler shows

ptxas fatal   : Unresolved extern function '_Z1fi'

The problem only occurs when the function is __device__ function.

[File ut.h]

__device__ int f(int);

[File ut.c]

#include "ut.h"

__device__ int f(int a){ 
    return a*a;
}

[File a.cu]

#include "ut.h"

__global__ void mk(){
    f(5);
}


int main(){
    mk<<<1,1>>>();
}

Upvotes: 4

Views: 4467

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 152299

When a __device__ or __global__ function calls a __device__ function (or __global__ function, in the case of dynamic parallelism) in another translation unit (i.e. file), then it is necessary to use device linking. To enable device linking with your simple compile command, just add the -rdc=true switch:

nvcc -rdc=true a.cu ut.cu 

That should fix the issue.

Note that in your compile command you list "ut.cu" but in your question you show "ut.c", I assume that should be the file "ut.cu". If not, you will also need to change the file name from "ut.c" to "ut.cu".

You can read more about device linking in the nvcc manual.

Upvotes: 11

Related Questions