vendredi 28 avril 2017

Cmake linking error between cu and cpp files

I feel like I have tried everything that I could come up with and what the web has to say and I'm still stuck with the same linking error so I hope someone here can help.

The problem is I have a method in a .cu file which is called from the main.cpp file, and the linker tells me that there is an undefined reference to the method. I'm using Cmake 3.3, Cuda 7.5, gcc 4.8.5.

I do need a templated method, and I do need separable compilation since (what isn't show here) NVCC is unable to compile some other code (ITK) that will be called in main(), and I do need c++11.

Any suggestions? It's probably just me overlooking something...

NOTE I have tried a couple of things, like using cuda_compile(...) to make cuda object files, and set(CUDA_SEPARABLE_COMPILATION ON), but these don't change the error at all... so I'm a little stuck.

The barebones code that produces the linking error is the following:

main.cpp

#include <iostream>

#include "ThrustCode.h"

using namespace std;

#define T float

int main() {
    cout << "Hello, World!" << endl;
    ThrustCode<T> t;
    t.test(10);
    return 0;
}

--

ThrustCode.h

#ifndef TESTCUDACPP_THRUSTCODE_HPP
#define TESTCUDACPP_THRUSTCODE_HPP

template <typename T>
class ThrustCode {
public:
    void test(T n);

};

#endif //TESTCUDACPP_THRUSTCODE_HPP

--

ThrustCode.cu

#include "ThrustCode.h"

template <typename T>
void ThrustCode<T>::test(T n) {
}

--

CMakeLists.txt

cmake_minimum_required(VERSION 3.3)

project(TestCudaCpp)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/Cmake)

find_package(CUDA QUIET REQUIRED)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_52,code=sm_52)

# this line is supposed to have the path to cuda libraries
# (specifically it is needed for the line at `target_link_libraries` below which imports curand
#  to generate random numbers on the host, when the program is compiled to run strictly on CPU).
link_directories(
        /usr/local/cuda/lib64
)

set(SOURCE_FILES
        ThrustCode.cu
        main.cpp)

cuda_add_executable(TestCudaCpp ${SOURCE_FILES})
target_link_libraries(TestCudaCpp)

This results in

[ 33%] Building NVCC (Device) object CMakeFiles/http://ift.tt/2ppCVsO
Scanning dependencies of target TestCudaCpp
[ 66%] Building CXX object CMakeFiles/http://ift.tt/2pGlVlg
[100%] Linking CXX executable TestCudaCpp
CMakeFiles/http://ift.tt/2pGlVlg: In function `main':
..<some path>../TestCudaCpp/main.cpp:12: undefined reference to `ThrustCode<float>::run(float)'
collect2: error: ld returned 1 exit status
gmake[2]: *** [TestCudaCpp] Error 1
gmake[1]: *** [CMakeFiles/TestCudaCpp.dir/all] Error 2
gmake: *** [all] Error 2

Aucun commentaire:

Enregistrer un commentaire