使用CMake

时间:2016-12-29 18:34:48

标签: c++ cuda cmake

我正在尝试使用CMake编译使用C ++和CUDA的程序。 编译c ++文件的CMakeLists部分已经完成,我正在添加部件来编译CUDA文件。 我添加了源代码并且编译好了,但是当链接部分到来时它停止了。 这是CMakeLists.txt文件:

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:

    '@USERFILE_ENVIRONMENT@@LAUNCHER_LINESEP@@_arg@'

  the old evaluation rules produce:

    'LD_LIBRARY_PATH=:$LD_LIBRARY_PATH@LAUNCHER_LINESEP@LD_LIBRARY_PATH=:$LD_LIBRARY_PATH'

  but the new evaluation rules produce:

    '@USERFILE_ENVIRONMENT@@LAUNCHER_LINESEP@@_arg@'

  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:

    '@USERFILE_ENVIRONMENT@@LAUNCHER_LINESEP@@_arg@'

  the old evaluation rules produce:

    'LD_LIBRARY_PATH=:$LD_LIBRARY_PATH@LAUNCHER_LINESEP@LD_LIBRARY_PATH=:$LD_LIBRARY_PATH'

  but the new evaluation rules produce:

    '@USERFILE_ENVIRONMENT@@LAUNCHER_LINESEP@@_arg@'

  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

以下是错误出现的“make”输出部分:

[ 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}”,但这不起作用。

2 个答案:

答案 0 :(得分:1)

如果你看https://cmake.org/cmake/help/v3.0/module/FindCUDA.html,你会发现你的呼叫

find_package(CUDA REQUIRED)

定义CUDA_LIBRARIES,而非CUDA_LIBRARY。 我想改变这个可以解决你的问题。

答案 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()