redis / hiredis

Minimalistic C client for Redis >= 1.2
BSD 3-Clause "New" or "Revised" License
6.15k stars 1.8k forks source link

hiredis.c compile error [E0513] in VS2022 #1234

Closed hesetone closed 8 months ago

hesetone commented 8 months ago

Hi: there is a question during compiling hiredis.c, a lot of compiler [E0513] would be thrown out. just like in line 182

    if (elements > 0) {
        r->element = hi_calloc(elements,sizeof(redisReply*));
        if (r->element == NULL) {
            freeReplyObject(r);
            return NULL;
        }
    }

it shows that type void* cannot be cast to redisReply*, there are many errors in hiredis.c like it. how can i fix it in VS2022?

michael-grunder commented 8 months ago

I can be more helpful if I have the exact compiler error, but my assumption is that the Visual Studio C compiler is attempting to compile hiredis as C++.

Hiredis is written in C (not C++) and targets ANSI C99, which is why we pass -std=c99 in our makefile.

Can you provide more information about how you're trying to compile the project?

hesetone commented 8 months ago

I can be more helpful if I have the exact compiler error, but my assumption is that the Visual Studio C compiler is attempting to compile hiredis as C++.

Hiredis is written in C (not C++) and targets ANSI C99, which is why we pass -std=c99 in our makefile.

Can you provide more information about how you're trying to compile the project?

thanks for your help, i have a visual studio project linked with an unreal engine 4.27 project, redis is a submodule of my UE4.27 plugin. there is nothing wrong with those files except hiredis.c. compilation will broke when i call api in hiredis.c directly or indirectly( many errors will be thrown out like E0513. ).

michael-grunder commented 8 months ago

C and C++ are somewhat compatible with each other, but this is a case where they are not. C++ won't let you coerce void* -> someType* without a typecast:

// No typecast required
void doSomething(void *p) {
    someType *st = p;
}
// We have to typecast the `p` variable
void doSomething(void *p) {
    someType *st = (someType*)p;
}

So what I assume is happening is that the compiler is trying to compile hiredis as C++ code, instead of C code. That said, I don't use Visual Studio or Windows, so I'm not really sure how you would go about fixing it.

hesetone commented 8 months ago

C and C++ are somewhat compatible with each other, but this is a case where they are not. C++ won't let you coerce void* -> someType* without a typecast:

// No typecast required
void doSomething(void *p) {
    someType *st = p;
}
// We have to typecast the `p` variable
void doSomething(void *p) {
    someType *st = (someType*)p;
}

So what I assume is happening is that the compiler is trying to compile hiredis as C++ code, instead of C code. That said, I don't use Visual Studio or Windows, so I'm not really sure how you would go about fixing it.

there is also so much appreciation for your replay, i have found some tips. in order to fix it, i defined functions in redis.cpp manually and it really works fine. this can prevent compiler from turning to hiredis.c and compile errors disappeared.