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

Linker not linking my own static library

本文关键字:链接 静态 自己的 我自己      更新时间:2023-10-16

我正在尝试创建一个使用MSVC 14(2015,V140)编译器的Windows 10 X64中QT 5,VTK 8.0.1和CUDA 9.1的应用程序。

由于VTK,这几乎必须使用CMake而不是Visual Studio完成。我已经从源构建了VTK 8.0.1,其编译器与上面的X64发布和调试相同,并且DLL在路径中。

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

我决定,处理每个库的不同汇编需求的最简单方法是为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}
    )

我的 main.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: %sn", 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"