2016-12-29 248 views
-2

我試圖編譯一個使用CMake的C++和CUDA程序。 編譯C++文件的CMakeLists部分已經編譯完成,我添加了編譯CUDA文件的部分。 我添加了源代碼並且它們編譯正常,但是當鏈接部分到來時它會停止。 這裏的的CMakeLists.txt文件:錯誤「編譯Cuda時使用CMake編譯CUDA時出現」__cudaRegisterLinkedBinary「的未定義引用

cmake_minimum_required (VERSION 2.6) 

set (CMAKE_CXX_STANDARD 11) 
set(PROJECT_NAME "UFFS - Empty Project" CACHE STRING "Nome do Projeto") 

project (${PROJECT_NAME}) 

find_package(OpenGL REQUIRED) 
find_package(CUDA REQUIRED) 
add_subdirectory (external) 

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/rpavlik-cmake-modules-1c73e35") 
include(CreateLaunchers) 
include(MSVCMultipleProcessCompile) 

if(INCLUDE_DISTRIB) 
    add_subdirectory(distrib) 
endif(INCLUDE_DISTRIB) 

set(CUDA_NVCC_FLAGS -Wno-deprecated-gpu-targets;-rdc=true;-std=c++11) 

include_directories(
    external/glfw-2.7.6/include 
    external/glm-0.9.4.0 
    external/glew-1.9.0/include 
    external/RVO2/include 
    external/GoTools/gotools-core/include 
    external/GoTools/parametrization/include 
    external/SFML/include 
    external/OpenNL/include 
    CUDA/include 
    . 
) 

add_library(ant_teak_bar STATIC IMPORTED) 
set_property(TARGET ant_teak_bar PROPERTY IMPORTED_LOCATION external/AntTweakBar/lib/libAntTweakBar.a) 

add_library(openNL STATIC IMPORTED) 
set_property(TARGET openNL PROPERTY IMPORTED_LOCATION external/OpenNL/lib/openNL.a) 

set(ALL_LIBS 
    ${OPENGL_LIBRARY} 
    ${CUDA_LIBRARY} 
    GLFW_276 
    GLEW_190 
    RVO 
    parametrization 
    pthread 
    sfml-audio 
    sfml-window 
    ant_teak_bar 
    openNL 
) 

add_definitions(
    -DTW_STATIC 
    -DTW_NO_LIB_PRAGMA 
    -DTW_NO_DIRECT3D 
    -DGLEW_STATIC 
    -D_CRT_SECURE_NO_WARNINGS 
) 

file(GLOB main_src 
    "*.h" 
     "*.hpp" 
    "*.cpp" 
     "CUDA/include/CUDA/*.cuh" 
     "OpenCL/*.cpp" 
     "common/*.cpp" 
     "common/*.hpp" 
     "common/*.h" 
) 


cuda_compile( 
    AGENT_O CUDA/_Agent.cu 
) 
cuda_compile( 
    GEOMETRIC_O CUDA/cudaGeometric.cu 
) 
cuda_compile( 
    KERNEL_O CUDA/kernel.cu 
) 
cuda_compile( 
    RVOAGENT_O CUDA/RVOAgent.cu 
) 
cuda_compile( 
    RVOSIMULATOR_O CUDA/RVOSimulator.cu 
) 

cuda_add_executable(main ${main_src} 
    ${DLINK_O} 
    ${AGENT_O} 
    ${GEOMETRIC_O} 
    ${KERNEL_O} 
    ${RVOAGENT_O} 
    ${RVOSIMULATOR_O} 
    ${CUDA_LIBRARY} 
    ) 




set_target_properties(main PROPERTIES XCODE_ATTRIBUTE_CONFIGURATION_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/") 
create_target_launcher(main WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/") 
create_default_target_launcher(main WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/") 

target_link_libraries(main ${ALL_LIBS}) 


SOURCE_GROUP(common REGULAR_EXPRESSION ".*/common/.*") 

if (NOT ${CMAKE_GENERATOR} MATCHES "Xcode") 
add_custom_command(
    TARGET main POST_BUILD 
    COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/main${CMAKE_EXECUTABLE_SUFFIX}" "${CMAKE_CURRENT_SOURCE_DIR}/" 
) 

elseif (${CMAKE_GENERATOR} MATCHES "Xcode") 

endif (NOT ${CMAKE_GENERATOR} MATCHES "Xcode") 

這裏是cmake的輸出:

-- Could NOT find Boost 
failed to create symbolic link '/home/fcg/Desktop/TCC_CUDA_LINUX/TCC_OPENCL/external/GoTools/lrsplines2D/data' because existing path cannot be removed: Is a directory 
CMake Warning (dev) at external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:196 (string): 
    Policy CMP0053 is not set: Simplify variable reference and escape sequence 
    evaluation. Run "cmake --help-policy CMP0053" for policy details. Use the 
    cmake_policy command to set the policy and suppress this warning. 

    For input: 

    '@[email protected]@[email protected]@[email protected]' 

    the old evaluation rules produce: 

    'LD_LIBRARY_PATH=:[email protected][email protected]_LIBRARY_PATH=:$LD_LIBRARY_PATH' 

    but the new evaluation rules produce: 

    '@[email protected]@[email protected]@[email protected]' 

    Using the old result for compatibility since the policy is not set. 
Call Stack (most recent call first): 
    external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:275 (_launcher_process_args) 
    CMakeLists.txt:105 (create_target_launcher) 
This warning is for project developers. Use -Wno-dev to suppress it. 

CMake Warning (dev) at external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:251 (get_target_property): 
    Policy CMP0026 is not set: Disallow use of the LOCATION target property. 
    Run "cmake --help-policy CMP0026" for policy details. Use the cmake_policy 
    command to set the policy and suppress this warning. 

    The LOCATION property should not be read from target "main". Use the 
    target name directly with add_custom_command, or use the generator 
    expression $<TARGET_FILE>, as appropriate. 

Call Stack (most recent call first): 
    external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:280 (_launcher_create_target_launcher) 
    CMakeLists.txt:105 (create_target_launcher) 
This warning is for project developers. Use -Wno-dev to suppress it. 

CMake Warning (dev) at external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:196 (string): 
    Policy CMP0053 is not set: Simplify variable reference and escape sequence 
    evaluation. Run "cmake --help-policy CMP0053" for policy details. Use the 
    cmake_policy command to set the policy and suppress this warning. 

    For input: 

    '@[email protected]@[email protected]@[email protected]' 

    the old evaluation rules produce: 

    'LD_LIBRARY_PATH=:[email protected][email protected]_LIBRARY_PATH=:$LD_LIBRARY_PATH' 

    but the new evaluation rules produce: 

    '@[email protected]@[email protected]@[email protected]' 

    Using the old result for compatibility since the policy is not set. 
Call Stack (most recent call first): 
    external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:265 (_launcher_process_args) 
    CMakeLists.txt:106 (create_default_target_launcher) 
This warning is for project developers. Use -Wno-dev to suppress it. 

CMake Warning (dev) at external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:251 (get_target_property): 
    Policy CMP0026 is not set: Disallow use of the LOCATION target property. 
    Run "cmake --help-policy CMP0026" for policy details. Use the cmake_policy 
    command to set the policy and suppress this warning. 

    The LOCATION property should not be read from target "main". Use the 
    target name directly with add_custom_command, or use the generator 
    expression $<TARGET_FILE>, as appropriate. 

Call Stack (most recent call first): 
    external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:270 (_launcher_create_target_launcher) 
    CMakeLists.txt:106 (create_default_target_launcher) 
This warning is for project developers. Use -Wno-dev to suppress it. 

-- Configuring done 
-- Generating done 
-- Build files have been written to: /home/fcg/Desktop/TCC_CUDA_LINUX/TCC_OPENCL 

而這裏的 「製造」 輸出的一部分出現的錯誤:

[ 14%] Linking CXX executable main 
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated__Agent.cu.o: In function `__sti____cudaRegisterAll_41_tmpxft_0000156c_00000000_7__Agent_cpp1_ii_59b6e43a()': 
/tmp/tmpxft_0000156c_00000000-4__Agent.cudafe1.stub.c:11: undefined reference to `__cudaRegisterLinkedBinary_41_tmpxft_0000156c_00000000_7__Agent_cpp1_ii_59b6e43a' 
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated_cudaGeometric.cu.o: In function `__sti____cudaRegisterAll_48_tmpxft_000015a3_00000000_7_cudaGeometric_cpp1_ii_f05e008c()': 
/tmp/tmpxft_000015a3_00000000-4_cudaGeometric.cudafe1.stub.c:11: undefined reference to `__cudaRegisterLinkedBinary_48_tmpxft_000015a3_00000000_7_cudaGeometric_cpp1_ii_f05e008c' 
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated_kernel.cu.o: In function `__sti____cudaRegisterAll_41_tmpxft_000015da_00000000_7_kernel_cpp1_ii_ac061e5b()': 
/tmp/tmpxft_000015da_00000000-4_kernel.cudafe1.stub.c:2: undefined reference to `__cudaRegisterLinkedBinary_41_tmpxft_000015da_00000000_7_kernel_cpp1_ii_ac061e5b' 
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated_RVOAgent.cu.o: In function `__sti____cudaRegisterAll_43_tmpxft_00001611_00000000_7_RVOAgent_cpp1_ii_5e2ad135()': 
/tmp/tmpxft_00001611_00000000-4_RVOAgent.cudafe1.stub.c:11: undefined reference to `__cudaRegisterLinkedBinary_43_tmpxft_00001611_00000000_7_RVOAgent_cpp1_ii_5e2ad135' 
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated_RVOSimulator.cu.o: In function `__sti____cudaRegisterAll_47_tmpxft_00001535_00000000_7_RVOSimulator_cpp1_ii_a19d5d58()': 
/tmp/tmpxft_00001535_00000000-4_RVOSimulator.cudafe1.stub.c:11: undefined reference to `__cudaRegisterLinkedBinary_47_tmpxft_00001535_00000000_7_RVOSimulator_cpp1_ii_a19d5d58' 
collect2: error: ld returned 1 exit status 
CMakeFiles/main.dir/build.make:2235: recipe for target 'main' failed 
make[2]: *** [main] Error 1 
CMakeFiles/Makefile2:71: recipe for target 'CMakeFiles/main.dir/all' failed 
make[1]: *** [CMakeFiles/main.dir/all] Error 2 
Makefile:138: recipe for target 'all' failed 
make: *** [all] Error 2 

我查了一下,顯然當我使用-rdc = true標誌編譯CUDA程序時,我需要額外執行一個步驟來正確鏈接cuda庫,但我不知道如何在cmake中執行此操作, alredy嘗試將「$ {CUDA_LIBRARY}」添加到與「target_link_libraries(main $ {ALL_LIBS})」鏈接的「$ {ALL_LIBS}」中,但這並不奏效。

回答

1

如果你看一下https://cmake.org/cmake/help/v3.0/module/FindCUDA.html,你會發現,你的呼叫

find_package(CUDA REQUIRED) 

定義CUDA_LIBRARIES,不CUDA_LIBRARY。 我想,改變這將解決你的問題。

+0

我忘了這個問題,我已經放棄了的項目,我不明白爲什麼我上有兩個downvotes ... 由於你的答案有1名給予好評我要去保持我的問題。 對不起,我感謝你。 –

0

試試這個:

find_package(CUDA REQUIRED) 
if(CUDA_FOUND) 

#turn CUDA_SEPARABLE_COMPILATION on. 
set(CUDA_SEPARABLE_COMPILATION ON) 
include_directories(/usr/local/cuda/include) 
add_definitions(-DUSE_GPU) 
file(GLOB cu src/*.cu) 

#The CUDA_NVCC_FLAGS variable is a list not a string. 
list(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_30,code=sm_30 -rdc=true -std=c+ +11) 
cuda_add_library(cudanet STATIC ${cu}) 
target_link_libraries(cudanet ${CUDA_LIBRARIES}) 
target_link_libraries(darknet cudart cuda cublas curand) 
message(STATUS "include & link cuda") 
link_directories(/usr/local/cuda/lib64) 
endif()