<div dir="ltr">if you are using the new first class cuda language, as seen by `<span style="font-size:small;text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">project(MyTest LANGUAGES CXX CUDA)` you shouldn't use the old find cuda package or the associated cuda_add_library calls. </span>When using the first class cuda language simply use add_library.<div><br></div><div>The other difference I notice is that you makefile example is not building shared libraries but object files, so to the same in CMake you need to use 'add_library' with the OBJECT keyword not the SHARED keyword.<br><div><div><span style="font-size:small;text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div><div><span style="font-size:small;text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Jul 6, 2018 at 12:01 AM Quang Ha <<a href="mailto:quang.t.ha.20@gmail.com">quang.t.ha.20@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi all,<div><br></div><div>Following upon how to perform code linking between CUDA and C++ (here at <a href="https://devblogs.nvidia.com/separate-compilation-linking-cuda-device-code/" target="_blank">https://devblogs.nvidia.com/separate-compilation-linking-cuda-device-code/</a>), I have successfully come up with a Makefile that is currently working for my test case:</div><div><br></div><div>##################################Makefile###############<span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">##################################</span></div><div><div>main_objects = main.o</div><div>host_objects = host.o</div><div>device_objects = device.o</div><div><br></div><div>all: $(main_objects) $(host_objects) $(device_objects)</div><div> g++ -O3 -fopenmp $(main_objects) $(host_objects) $(device_objects) -L/usr/local/cuda/lib64 -lcudart -o main</div><div><br></div><div>$(main_objects): $(main_objects:.o=.cpp)</div><div> g++ -O3 -I/usr/local/cuda/include -c $< -o $@</div><div><br></div><div>$(host_objects): $(host_objects:.o=.cpp)</div><div> g++ -O3 -DTHRUST_HOST_SYSTEM=THRUST_HOST_SYSTEM_OMP -fopenmp -c $< -o $@</div><div><br></div><div>$(device_objects): $(device_objects:.o=.cu)</div><div> nvcc -O3 -DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_CUDA -c $< -o $@</div><div><br></div><div>clean:</div><div> rm -f *.o main</div></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">##################################</span><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">##################################</span><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">##################################</span><br></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">Now, I am trying to write a CMake for this, my current attempt is as followed:</span></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div><div><span style="background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><div>##############################################CMakeLists.txt############################################</div><div>cmake_minimum_required(VERSION 3.8)</div><div>project(MyTest LANGUAGES CXX CUDA)</div><div><br></div><div># check requirements</div><div>find_package(CUDA REQUIRED)</div><div>find_package(OpenMP)</div><div><br></div><div>if (CUDA_FOUND AND OPENMP_FOUND)</div><div> # OpenMP</div><div> set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")</div><div> set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")</div><div> set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")</div><div><br></div><div> # CUDA</div><div> include_directories(${CUDA_INCLUDE_DIRS})</div><div> set(ALL_CUDA_LIBS ${CUDA_LIBRARIES} ${CUDA_cusparse_LIBRARY} ${CUDA_cublas_LIBRARY})</div><div> set(LIBS ${LIBS} ${ALL_CUDA_LIBS})</div><div> set(CUDA_SEPARABLE_COMPILATION OFF)</div><div> set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};--maxrregcount 32)</div><div><br></div><div> set(OMP_SOURCES host.cpp)</div><div> add_library(Host_OMP SHARED ${OMP_SOURCES})</div><div> set_target_properties(Host_OMP PROPERTIES COMPILE_FLAGS "-DTHRUST_HOST_SYSTEM=THRUST_HOST_SYSTEM_OMP")</div><div> set(LIBS ${LIBS} ${Host_OMP})</div><div><br></div><div> set(CUDA_SOURCES <a href="http://device.cu" target="_blank">device.cu</a>)</div><div> cuda_add_library(Device_CUDA SHARED ${CUDA_SOURCES})</div><div> set_target_properties(Device_CUDA PROPERTIES COMPILE_FLAGS "-DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_CUDA")</div><div> set_target_properties(Device_CUDA PROPERTIES CUDA_SEPERABLE_COMPILATION ON)</div><div> set(LIBS ${LIBS} ${Device_CUDA})</div><div><br></div><div> add_executable(main main.cpp)</div><div> target_link_libraries(main ${LIBS})</div><div><br></div><div>endif(CUDA_FOUND AND OPENMP_FOUND)</div><div style="font-size:small"><span style="text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline">##################################</span><span style="text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline">##################################</span><span style="text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline">##################################</span><br></div><div style="font-size:small"><span style="text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"><br></span></div><div style="font-size:small"><span style="text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline">It is still, unfortunately, not working. If I commented out the add_executable line, the object code for Host_OMP and Device_CUDA is compiled. I can then use the terminal and link them with main.cpp using gcc and generate the executable - i.e. using the final command in the Makefile above.</span></div><div style="font-size:small"><span style="text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"><br></span></div><div style="font-size:small"><span style="text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline">What am I doing wrong here? It seems to be very fundamental, but I can't seem to get it right...</span></div><div style="font-size:small"><span style="text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"><br></span></div><div style="font-size:small">Any suggestions would be greatly appreciated!!</div><div style="font-size:small"><br></div><div style="font-size:small">Thanks,</div><div style="font-size:small">Quang</div></span></div></div>
-- <br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
</blockquote></div>