Generate compile_commands.json from Xcode project

I need to generate a compile_commands.json for our C++ project. I need it for static code analysis tools as well as enabling clangd usage with other editors. I've tried two methods but it only works for some files with clangd. The logs of the clangd extension in VS Code indicate only that many files can't be compiled. If it works for a file, it's great, but sadly it's not consistent.

I've used these two approaches to generate the compile_commands.json:

using xcpretty

xcrun xcodebuild -projectmyproject.xcodeproj -scheme myscheme clean build CODE_SIGNING_ALLOWED=NO | xcpretty -r json-compilation-database -o build/compile_commands.json

the recommended way by sonarcloud

xcrun xcodebuild -project myproject.xcodeproj -scheme myscheme \
-configuration Debug clean build CODE_SIGNING_ALLOWED=NO OTHER_CFLAGS="\$(inherited) -gen-cdb-fragment-path \$(PROJECT_DIR)/CompilationDatabase"
    sed -e '1s/^/[\'$'\n''/' -e '$s/,$/\'$'\n'']/' MacOS/Source/CompilationDatabase/*.json > MacOS/Source/compile_commands.json

Is there something missing or are there better ways to generate the compile_commands.json without having to write a full cmake definition?

Generate compile_commands.json from Xcode project
 
 
Q