Maddy
Maddy

Reputation: 2224

Undefined reference error - MPI compilation

I am trying to compile a CUDA file and an MPI file separately and then link them together. The code files are as follows:

stras.h

void pr(void);

stras.cu

#include "stras.h"
//..
void pr(){
printf("ok");
}
//...

mm_mpi.c

//..
#include "stras.h"
pr();
//..

makefile

# Location of the CUDA Toolkit binaries and libraries
CUDA_PATH       ?= /usr/local/cuda-5.0
CUDA_INC_PATH   ?= $(CUDA_PATH)/include
CUDA_BIN_PATH   ?= $(CUDA_PATH)/bin
CUDA_LIB_PATH  ?= $(CUDA_PATH)/lib

# Common binaries
NVCC            ?= $(CUDA_BIN_PATH)/nvcc
GCC             ?= g++

# MPI check and binaries
MPICC           = /usr/bin/mpicc

# OS-specific build flags
LDFLAGS   := -L$(CUDA_LIB_PATH) -lcudart
CCFLAGS   := -m32

# Target rules
all: build

build: stras

stras.o: stras.cu
    $(NVCC) -o $@ -c $<

main.o: mm_mpi.c
    $(MPICC) -o $@ -c $<

stras: stras.o main.o
    $(MPICC) $(CCFLAGS) -o $@ $+ $(LDFLAGS)

run: build
    ./stras

clean:
    rm -f stras.o main.o

But it gives me the error:

/usr/local/cuda-5.0/bin/nvcc  -o stras.o -c stras.cu
/usr/bin/mpicc  -o main.o -c mm_mpi.c
/usr/bin/mpicc -m32 -o stras stras.o main.o -L/usr/local/cuda-5.0/lib -lcudart -I
main.o: In function `main':
mm_mpi.c:(.text+0x6a3): undefined reference to `pr'
collect2: ld returned 1 exit status
make: *** [stras] Error 1

I am not that proficient in C/C++. Could anyone please tell me if I have missed anything? Thanks in advance.

Upvotes: 0

Views: 1434

Answers (1)

starrify
starrify

Reputation: 14751

This is expected to solve your problem:

/* This is stras.h */
#ifdef __CUDACC__
extern "C" void pr(void);
#else
extern void pr(void);
#endif

The reason, is just like it when you're trying to export a symbol from a C++ object file for other linkers to use: you shall explicitly declare it as a "C" type.

Upvotes: 3

Related Questions