链接器没有链接我自己的静态库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链接器没有链接我自己的静态库相关的知识,希望对你有一定的参考价值。
我正在尝试使用MSVC 14(2015,v140)编译器在Windows 10 x64中创建一个结合Qt 5,VTK 8.0.1和CUDA 9.1的应用程序。
由于VTK,这几乎必须使用CMake而不是Visual Studio来完成。我已经使用与上面针对x64 Release和Debug相同的编译器从源代码构建了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}
)
我的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}
",
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
", 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!
", 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"
。
以上是关于链接器没有链接我自己的静态库的主要内容,如果未能解决你的问题,请参考以下文章