Я новичок в cuda, C++, и я пытаюсь переместить код процессора openssl sha1 на cuda c, но я столкнулся с серьезной проблемой.
вот минимальный код, который может воспроизвести проблему. В этом проекте vs2015 cuda9.0 есть три файла. Это main.cpp, sha1.cu и sha1.h.
//main.cpp
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "openssl\sha.h"
int main()
{
SHA_CTX ctx;
SHA1_Init(&ctx);
return 0;
}
//sha1.h
#ifndef SHA1_H
#define SHA1_H
#include <stdint.h>
#include <cuda_runtime.h>
namespace cudatest {
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
uint32_t state[5];
uint32_t count[2];
unsigned char buffer[64];
} SHA1_CTX;
#define SHA_CTX SHA1_CTX
#define SHA_DIGEST_LENGTH 20
__device__ void SHA1_Init(SHA1_CTX * context);
#ifdef __cplusplus
}
#endif
}
#endif /* SHA1_H */
//sha1.cu
#include <cuda_runtime.h>
#include "sha1.h"
namespace cudatest {
__device__ void SHA1_Init(SHA1_CTX * context)
{
}
}
Main.cpp использует компилятор C / C++, а sha1.cu использует CUDA C / C++
И я добавляю заголовки openssl в AdditionalIncludeDirectories, устанавливаю каталог, содержащий ssleay32.lib и libeay32.lib, в путь к библиотеке, устанавливаю AdditionalDependencies с помощью ssleay32.lib, libeay32.lib.
Then the project built with no error and no warning. But when I run it or debug it,I found the function SHA1_Init runs into device code and the program crashed immediately.
why the compiler linked function SHA1_Init with the cuda device SHA1_Init implement which has a namespace cudatest wrapped instead of a ssleay32.lib, libeay32.lib CPU implement?
Хорошо, я обнаружил проблему. Я не должен использовать extern «C» в пространстве имен C++. Это делает функцию видимой для глобального пространства имен. если вы определите другую функцию c SHA1_Init в файле .cpp, компоновщик будет жаловаться, но если другой SHA1_Init находится в openssl lib, компоновщик vs C / C++ не предупредил ничего, кроме ссылки на реализацию cuda.