vrai
vrai

Reputation: 31

vs2015 cuda9.0 linked SHA1_Init with CUDA implement instead of openssl cpu libs

I am a beginner to cuda, c++ and I am trying to move openssl sha1 cpu code to cuda c,but I ran into a weired problem.

here is the minimum code that can reproduce the problem. There are three files in this vs2015 cuda9.0 project. They are main.cpp ,sha1.cu and 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)
    {

    }
}

The main.cpp uses C/C++ compiler and sha1.cu uses CUDA C/C++

And I add openssl headers into the AdditionalIncludeDirectories,set directory which contains ssleay32.lib and libeay32.lib to library path,set AdditionalDependencies with 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?

Upvotes: 0

Views: 239

Answers (1)

vrai
vrai

Reputation: 31

OK,I found the problem.I shouldn't use extern "C" in a c++ namespace.It make the function visiable to the global namespace. if you define another c SHA1_Init function in a .cpp file ,the linker will complain.But if another SHA1_Init is in a openssl lib,the vs C/C++ linker warnned nothing but linked to the cuda implement.

Upvotes: 2

Related Questions