Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to compile when using strdup C23 standard function #23398

Open
BlackGoku36 opened this issue Jan 14, 2025 · 2 comments
Open

Fails to compile when using strdup C23 standard function #23398

BlackGoku36 opened this issue Jan 14, 2025 · 2 comments

Comments

@BlackGoku36
Copy link

Please include the following in your bug report:

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.74 (1092ec3)
clang version 20.0.0git (https:/github.com/llvm/llvm-project 322eb1a92e6d4266184060346616fa0dbe39e731)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/x/emsdk/upstream/bin

Failing command line in full:
Command line:

-> emcc src/usdc.c src/token.c external/lz4/lib/lz4.c -I external/lz4/lib -std=c23 -o build/wasm/wasm_usdc.html -Wall -Wextra --preload-file data -sMEMORY64=2

Output:

src/token.c:169:10: error: use of undeclared identifier 'strdup'; did you mean 'strupr'?
  169 |                 .str = strdup(str),
      |                        ^~~~~~
      |                        strupr
/Users/urjasvisuthar/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/urjasvisuthar/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/urjasvisuthar/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_5hhhrejl/token_1.o' failed (returned 1)

Full link command and output with -v appended:
Command line:

emcc src/usdc.c src/token.c external/lz4/lib/lz4.c -I external/lz4/lib -std=c23 -o build/wasm/wasm_usdc.html -Wall -Wextra --preload-file data -sMEMORY64=2 -v

Output:

/Users/urjasvisuthar/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/urjasvisuthar/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 -v src/usdc.c -c -o /var/folders/7x/khcwdw3d39g8c3k03mcrcw3r0000gn/T/emscripten_temp_s3__nidp/usdc_0.o
clang version 20.0.0git (https:/github.com/llvm/llvm-project 322eb1a92e6d4266184060346616fa0dbe39e731)
Target: wasm64-unknown-emscripten
Thread model: posix
InstalledDir: /Users/urjasvisuthar/emsdk/upstream/bin
 (in-process)
 "/Users/urjasvisuthar/emsdk/upstream/bin/clang-20" -cc1 -triple wasm64-unknown-emscripten -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name usdc.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -target-feature -bulk-memory -target-feature -bulk-memory-opt -target-feature -nontrapping-fptoint -fvisibility=hidden -debugger-tuning=gdb -fdebug-compilation-dir=/Users/urjasvisuthar/Desktop/test/fta -v -fcoverage-compilation-dir=/Users/urjasvisuthar/Desktop/test/fta -resource-dir /Users/urjasvisuthar/emsdk/upstream/lib/clang/20 -D EMSCRIPTEN -I external/lz4/lib -isysroot /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /Users/urjasvisuthar/emsdk/upstream/lib/clang/20/include -internal-isystem /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include/wasm64-emscripten -internal-isystem /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include -Werror=implicit-function-declaration -Wall -Wextra -std=c23 -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/7x/khcwdw3d39g8c3k03mcrcw3r0000gn/T/emscripten_temp_s3__nidp/usdc_0.o -x c src/usdc.c
clang -cc1 version 20.0.0git based upon LLVM 20.0.0git default target x86_64-apple-darwin24.1.0
ignoring nonexistent directory "/Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include/wasm64-emscripten"
#include "..." search starts here:
#include <...> search starts here:
 external/lz4/lib
 /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
 /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include/compat
 /Users/urjasvisuthar/emsdk/upstream/lib/clang/20/include
 /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
 /Users/urjasvisuthar/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/urjasvisuthar/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 -v src/token.c -c -o /var/folders/7x/khcwdw3d39g8c3k03mcrcw3r0000gn/T/emscripten_temp_s3__nidp/token_1.o
clang version 20.0.0git (https:/github.com/llvm/llvm-project 322eb1a92e6d4266184060346616fa0dbe39e731)
Target: wasm64-unknown-emscripten
Thread model: posix
InstalledDir: /Users/urjasvisuthar/emsdk/upstream/bin
 (in-process)
 "/Users/urjasvisuthar/emsdk/upstream/bin/clang-20" -cc1 -triple wasm64-unknown-emscripten -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name token.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -target-feature -bulk-memory -target-feature -bulk-memory-opt -target-feature -nontrapping-fptoint -fvisibility=hidden -debugger-tuning=gdb -fdebug-compilation-dir=/Users/urjasvisuthar/Desktop/test/fta -v -fcoverage-compilation-dir=/Users/urjasvisuthar/Desktop/test/fta -resource-dir /Users/urjasvisuthar/emsdk/upstream/lib/clang/20 -D EMSCRIPTEN -I external/lz4/lib -isysroot /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /Users/urjasvisuthar/emsdk/upstream/lib/clang/20/include -internal-isystem /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include/wasm64-emscripten -internal-isystem /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include -Werror=implicit-function-declaration -Wall -Wextra -std=c23 -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/7x/khcwdw3d39g8c3k03mcrcw3r0000gn/T/emscripten_temp_s3__nidp/token_1.o -x c src/token.c
clang -cc1 version 20.0.0git based upon LLVM 20.0.0git default target x86_64-apple-darwin24.1.0
ignoring nonexistent directory "/Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include/wasm64-emscripten"
#include "..." search starts here:
#include <...> search starts here:
 external/lz4/lib
 /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
 /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include/compat
 /Users/urjasvisuthar/emsdk/upstream/lib/clang/20/include
 /Users/urjasvisuthar/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
src/token.c:169:10: error: use of undeclared identifier 'strdup'; did you mean 'strupr'?
  169 |                 .str = strdup(str),
      |                        ^~~~~~
      |                        strupr
/Users/urjasvisuthar/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/urjasvisuthar/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/urjasvisuthar/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 -v src/token.c -c -o /var/folders/7x/khcwdw3d39g8c3k03mcrcw3r0000gn/T/emscripten_temp_s3__nidp/token_1.o' failed (returned 1)

Google group conversation:
https://groups.google.com/g/emscripten-discuss/c/3DMpT62eLuE

@drmdanowar
Copy link

This is the most important part of the best way

@sbc100
Copy link
Collaborator

sbc100 commented Jan 14, 2025

The reason for this is that -std=c23 (or -std=c11) will define __STRICT_ANSI__ and musl assumes that this means that strdup is not available:

#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
|| defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
|| defined(_BSD_SOURCE)
char *strtok_r (char *__restrict, const char *__restrict, char **__restrict);
int strerror_r (int, char *, size_t);
char *stpcpy(char *__restrict, const char *__restrict);
char *stpncpy(char *__restrict, const char *__restrict, size_t);
size_t strnlen (const char *, size_t);
char *strdup (const char *);
char *strndup (const char *, size_t);
char *strsignal(int);
char *strerror_l (int, locale_t);
int strcoll_l (const char *, const char *, locale_t);
size_t strxfrm_l (char *__restrict, const char *__restrict, size_t, locale_t);
void *memmem(const void *, size_t, const void *, size_t);
#endif

While this assumption was try for c11 it is no longer true for c23 which added strdup to the standard: https://en.cppreference.com/w/c/string/byte/strdup

Kind of crazy that strdup was not part of the C standard until 2023 :)

Anyway, I think musl upstream is looking into fixing this: https://www.openwall.com/lists/musl/2023/05/31/20.

We could fix it downstream ahead of them if folks are running into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants