c++ - 链接器没有链接我自己的静态库

标签 c++ cmake cuda linker-errors vtk

我正在尝试使用 MSVC 14(2015,v140)编译器在 Windows 10 x64 中创建一个结合了 Qt 5、VTK 8.0.1 和 CUDA 9.1 的应用程序。

由于 VTK,这几乎必须使用 CMake 而不是 Visual Studio 来完成。我已经使用与上述 x64 发布和调试相同的编译器从源代码构建了 VTK 8.0.1,并且 dll 在 PATH 中。

我的项目结构只是一个包含所有内容的文件夹(下面的三个文件)。

我决定处理每个库的不同编译需求的最简单方法是为 CUDA 创建一个单独的静态库并在之后链接它,其他所有内容都被编译成可执行文件。

问题是无论我做什么我都会在构建时收到以下错误

main.cpp.obj:-1: error: LNK2019: unresolved external symbol addKernel referenced in function "enum cudaError __cdecl addWithCuda(int *,int const *,int const *,unsigned int)" (?addWithCuda@@YA?AW4cudaError@@PEAHPEBH1I@Z)

我肯定这与某种形式的 C/C++ 名称重整冲突有关,但这只是让我到目前为止。

请拯救我的圣诞节

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(Test2 LANGUAGES CXX CUDA)

# QT5
find_package(Qt5Widgets)

# VTK
set(VTK_DIR "correctPath" CACHE PATH directory FORCE)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
#message(${VTK_LIBRARIES})

# CUDA
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
#message(STATUS "CUDA_LIBRARIES: ${CUDA_INCLUDE_DIRS} ${CUDA_LIBRARIES}")

# Adds all the desired files to their lists
set(SOURCES
    main.cpp
    )

set(KERNELS
    kernelOnly.cu
    )

set(HEADERS
    )

set(UI
    )

set(RESOURCES
    )

# Processes Qt files
QT5_WRAP_CPP(HEADERS_MOC ${HEADERS})
QT5_WRAP_UI(UI_MOC ${UI})
QT5_ADD_RESOURCES(RESOURCES_RCC ${RESOURCES})

# Include bin directories so we can find MOC'd stuff later
set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories (${PROJECT_SOURCE_DIR})
include_directories (${PROJECT_BINARY_DIR})

# Compile our CUDA kernel library
list(APPEND CUDA_NVCC_FLAGS -gencode=arch=compute_61,code=sm_61)
add_library(CudaLib STATIC ${KERNELS})
target_compile_features(CudaLib PUBLIC cxx_std_11)
set_target_properties(CudaLib PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
set_target_properties(CudaLib PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)

# Compile Qt+VTK+Cpp into an executable
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS_MOC} ${UI_MOC} ${RESOURCES_RCC})
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

# Link libraries to the executable
target_link_libraries(${PROJECT_NAME}
    CudaLib
    Qt5::Widgets
    ${VTK_LIBRARIES}
    ${CUDA_LIBRARIES}
    ${CUDA_cusparse_LIBRARY}
    ${CUDA_cublas_LIBRARY}
    )

我的ma​​in.cpp:

#include <cuda_runtime.h>

#include <stdio.h>

#include <QApplication>
#include <QVTKWidget.h>

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>

cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);
extern "C" void addKernel(int* c, const int* a, const int* b);

int main(int argc, char** argv)
{
    const int arraySize = 5;
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
    int c[arraySize] = { 0 };

    // Add vectors in parallel.
    cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addWithCuda failed!");
        return 1;
    }

    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
        c[0], c[1], c[2], c[3], c[4]);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
        return 1;
    }
}

// Helper function for using CUDA to add vectors in parallel.
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
    int *dev_a = 0;
    int *dev_b = 0;
    int *dev_c = 0;
    cudaError_t cudaStatus;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for three vectors (two input, one output)    .
    cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    // Copy input vectors from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    // Launch a kernel on the GPU with one thread for each element.
    // replaces addKernel<<<1, size>>>(dev_c, dev_a, dev_b) syntax
    void* args[] = { &dev_c, &dev_a, &dev_b };
    cudaStatus = cudaLaunchKernel(
      (const void*)&addKernel, // pointer to kernel func.
                      dim3(1), // grid
                   dim3(size), // block
                         args  // arguments
    );

    // Check for any errors launching the kernel
    cudaStatus = cudaGetLastError();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
        goto Error;
    }

    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
        goto Error;
    }

    // Copy output vector from GPU buffer to host memory.
    cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

Error:
    cudaFree(dev_c);
    cudaFree(dev_a);
    cudaFree(dev_b);

    return cudaStatus;
}

和我的kernelOnly.cu:

#include <device_launch_parameters.h>

__global__ void addKernel(int *c, const int *a, const int *b)
{
    int i = threadIdx.x;
    c[i] = a[i] + b[i];
}

最佳答案

我认为应该是:

extern void addKernel(int* c, const int* a, const int* b);

使用 “C”

关于c++ - 链接器没有链接我自己的静态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47947486/

相关文章:

c++ - 指针有点问题

c++ - 指针丢失对前一个节点 C++ 的引用

testing - 托管在 github 上的 cmake 项目的持续集成软件

python - 异步内核启动后返回 pyCUDA 中的主机代码

c++ - 将结构复制到设备内存 CUDA

android - Eclipse 中的 Android 项目缺少 "Add native support"

cmake - 我什么时候应该在 CMake 中用 ${...} 包装变量?

c++将ubuntu下的boost库与cmake : undefined reference to `boost::iostreams::zlib::okay' 链接

c++ - 访问 OpenCV GpuMat channel

c++ - 从 Qt5 ColorDialog 中删除颜色渐变窗口