8

I'm new to osx and I have some problems.I tried to makefile but come up with: error: unknown type name 'u_char'; did you mean 'char'?

I referred to C PCAP library unknown types error ,add -D_BSD_SOURCE CFLAGS to my makefile, but it doesn't help.What am I missing?

EDIT:

This is the code which produces the error:

char *computePwd(const u_char *md5) {
    static char buf[16];
    unsigned char tmp[40];
    int tmpl=0;
    tmpl = strlen(userName);
    strcpy((char*)tmp, userName);
    memcpy(tmp + tmpl, md5, 16);
    tmpl += 16;
    memcpy(buf, ComputeHash(tmp, tmpl), 16);
    memset(tmp, 0, 16);
    strcpy((char*)tmp, password);
    int i;
    for (i=0; i<16; ++i)
        buf[i] ^= tmp[i];
    return buf;
}
Community
  • 1
  • 1
Jian Guo
  • 708
  • 1
  • 9
  • 19
  • Can you post the code which produces the error? – zvone Dec 11 '15 at 18:21
  • `char *computePwd(const u_char *md5) { static char buf[16]; unsigned char tmp[40]; int tmpl=0; tmpl = strlen(userName); strcpy((char*)tmp, userName); memcpy(tmp + tmpl, md5, 16); tmpl += 16; memcpy(buf, ComputeHash(tmp, tmpl), 16); memset(tmp, 0, 16); strcpy((char*)tmp, password); int i; for (i=0; i<16; ++i) buf[i] ^= tmp[i]; return buf; }` @zvone – Jian Guo Dec 12 '15 at 02:05

3 Answers3

8

Add -Du_char="unsigned char" to CFLAGS or just fix the source. Using u_char as lazy shorthand for unsigned char was a common practice in legacy BSD codebases where the system headers historically exposed such a typedef. It should not be used in modern code.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • It would be better to fix that error at the source, if possible. OP does not state where it appears, so this option should be taken first. – too honest for this site Dec 11 '15 at 18:29
  • @Olaf: Which is better depends on what OP is doing. For reusing existing code in new software or actively porting existing software, fixing it is preferable, of course. But for just deploying existing legacy software/libraries with minimal fuss and maintenance effort on a modern platform, adding `-D`'s to `CFLAGS` may be the most reasonable approach. – R.. GitHub STOP HELPING ICE Dec 11 '15 at 19:18
  • That was my idea. Just to add that `-D` defines a macro which is a text-replacement, not a `typedef` or similar. Macros can have unexpected side-effects, because they have do not care about the normal C grammar. – too honest for this site Dec 11 '15 at 19:38
  • @R.. I changed all the u_char to unsigned char and it works!Thanks! – Jian Guo Dec 12 '15 at 02:14
2

Or you could add

#ifdef __APPLE__
#include <sys/types.h>
#endif

That fixed the same problem for me.

It includes a definition for u_char.

typedef unsigned char   u_char;
batalyx
  • 21
  • 4
  • No huge surprise, but worth to mention: including sys/types.h solved the same problem on FreeBSD. – Leo Jun 09 '17 at 08:20
2

Add -D_DARWIN_C_SOURCE and it should work.

ldanko
  • 557
  • 8
  • 20