strdup() function

前端 未结 7 1314
孤城傲影
孤城傲影 2020-12-02 00:01

I recently became aware that the strdup() function I\'ve enjoyed using so much on OS X is not part of ANSI C, but part of POSIX. I don\'t want to rewrite all my

7条回答
  •  爱一瞬间的悲伤
    2020-12-02 00:18

    Usually, you just use an #if to define the function you want when under a certain compiler. If the built-in library doesn't define strdup, there is no problem in defining it yourself (other than if they do define it in the future, you'll have to take it out.)

    // Only define strdup for platforms that are missing it..
    #if COMPILER_XYZ || COMPILER_ABC
    char *strdup(const char *)
    {
       // ....
    }
    #endif
    

提交回复
热议问题