strdup error on g++ with c++0x

前端 未结 4 1203
情书的邮戳
情书的邮戳 2020-12-17 00:25

I have some C++0x code. I was able to reproduce it below. The code below works fine without -std=c++0x however i need it for my real code.

How do i incl

4条回答
  •  北海茫月
    2020-12-17 00:58

    strdup may not be included in the library you are linking against (you mentioned mingw). I'm not sure if it's in c++0x or not; I know it's not in earlier versions of C/C++ standards.

    It's a very simple function, and you could just include it in your program (though it's not legal to call it simply "strdup" since all names beginning with "str" and a lowercase letter are reserved for implementation extensions.)

    char *my_strdup(const char *str) {
        size_t len = strlen(str);
        char *x = (char *)malloc(len+1); /* 1 for the null terminator */
        if(!x) return NULL; /* malloc could not allocate memory */
        memcpy(x,str,len+1); /* copy the string into the new buffer */
        return x;
    }
    

提交回复
热议问题