Fails to compile when using strdup function

66 views
Skip to first unread message

Urjasvi Suthar

unread,
Jan 13, 2025, 5:18:34 AM1/13/25
to emscripten-discuss
Hello,
I am trying to compile for wasm with emscripten but there seems to be no 'strdup' C standard function.
```
src/token.c:169:10: error: use of undeclared identifier 'strdup'; did you mean 'strupr'?
  169 |                 .str = strdup(str),
      |                        ^~~~~~
      |                        strupr
/Users/x/emsdk/upstream/emscripten/cache/sysroot/include/compat/string.h:9:14: note: 'strupr' declared here
    9 | extern char* strupr(char *);
      |              ^
1 error generated.
emcc: error: '/Users/x/emsdk/upstream/bin/clang -target wasm64-unknown-emscripten -fignore-exceptions -mno-bulk-memory -mno-bulk-memory-opt -mno-nontrapping-fptoint -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/Users/x/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -Iexternal/lz4/lib -std=c23 -Wall -Wextra src/token.c -c -o /var/folders/7x/khcwdw3d39g8c3k03mcrcw3r0000gn/T/emscripten_temp_u3yctg1o/token_1.o' failed (returned 1)
```
Is this C23 standard not supported yet or am I missing something?
I know I can implement it myself easily (that what I am currently doing as workaround), but less code and #ifdefs the better :)

Regards,
Urjasvi Suthar

Sam Clegg

unread,
Jan 13, 2025, 2:59:14 PM1/13/25
to emscripte...@googlegroups.com
`strdup` is available in emscripten.  Its part of the musl libc: https://github.com/emscripten-core/emscripten/blob/ebfeaf7a17f53ad322ea322efa8dfca5c752679b/system/lib/libc/musl/include/string.h#L70

It looks like using `-std=c23` is somehow suppressing that definition.   Could you open a bug about that?

cheers,
sam

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/c666906e-ad13-4470-8f4a-3aae76b36925n%40googlegroups.com.

Sam Clegg

unread,
Jan 13, 2025, 3:15:01 PM1/13/25
to emscripte...@googlegroups.com
After a little investigation it seems like the problem is that when you use `-std=c23` or `-std=c11` clang will define `__STRICT_ANSI__`.  `strdup` was not available in this mode, at least not prior to C23.  So musl does not declare it in that mode.

You can work around this by using `-std=gnu23` or by adding `-D_BSD_SOURCE`.

cheers,
sam

Urjasvi Suthar

unread,
Jan 14, 2025, 4:16:05 AM1/14/25
to emscripten-discuss
Thank you for your response. The work-around worked, should I still open a bug report?

Regards,
Urjasvi Suthar

Sam Clegg

unread,
Jan 14, 2025, 10:48:10 AM1/14/25
to emscripte...@googlegroups.com
Yes, I think it is probably worth having a bug to track this.  Thank you.

Urjasvi Suthar

unread,
Jan 14, 2025, 11:00:07 AM1/14/25
to emscripten-discuss
Here, https://github.com/emscripten-core/emscripten/issues/23398

Thanks and regards,
Urjasvi Suthar

Reply all
Reply to author
Forward
0 new messages