0

What value do i have to use for the file - attribute in a compile_commands.json for clang?

Here it says: https://clang.llvm.org/docs/JSONCompilationDatabase.html

file: The main translation unit source processed by this compilation step. This is used by tools as the key into the compilation database. There can be multiple command objects for the same file, for example if the same source file is compiled with different configurations.

[
  { "directory": "/home/user/llvm/build",
    "arguments": ["/usr/bin/clang++", "-Irelative", "-DSOMEDEF=With spaces, quotes and \\-es.", "-c", "-o", "file.o", "file.cc"],
    "file": "file.cc" },
]

My Files (.c) are in the same Folder as my .compile_commands.json. (Basically everything is just in one folder). Yet if i do "file": "myfile.c" it wont work, and clangd uses the Fallback.

But if i use the absolute path it works for some reason.

So what is the correct non-absoulte path value i need to use please? (i am on windows)

2
  • This question is similar to: clang-tidy-10 and compile_commands.json does not support relative paths. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented May 20 at 16:15
  • @Scott McPeak thank you, the question you linked answers my question. -> the directory must be absolute, to use a relative file path / the directory value not beeing an absolute file path was the issue. Commented May 20 at 18:16

2 Answers 2

1

This is documented at https://clang.llvm.org/docs/JSONCompilationDatabase.html#format, specifically this part:

  • directory: The working directory of the compilation. All paths specified in the command or file fields must be either absolute or relative to this directory.

So, if the path in the file field is not absolute, it needs to be relative to the directory specified in directory.

As an example, if the source file is located at /home/user/llvm/src/file.cc, and the value of the directory field is /home/user/llvm/build, then a correct relative path to put into the file field would be ../src/file.cc.


EDIT: The documentation page does not say anything about whether the directory field itself must be absolute, or if it's allowed to be relative (and if so, what it's interpreted relative to).

However, looking at the implementation, I can see that there is no handling of directory being a relative path, it's assumed to be absolute.

This could probably be clarified in the documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you, i specifically misunderstood, that the "directory" file path should be absolute.. because i thought the "directory" path can also be absolute to the compile_command.json location... could you a hint for this please in your answer, then i can put it as answer? (because it would be would be clearer)
@z4x: I edited the answer with some additional details.
A relative directory used to cause issues. Clangd would randomly change its current directory and suddenly stop working. Maybe that got fixed eventually, I don't know.
0

I think the file needs to have a path as well.

see this generated file from CMAKE in one of my "projects".

[
{
  "directory": "/home/****/workspace/vulkan/build",
  "command": "/usr/bin/clang++  -I/home/****/workspace/vulkan/include -g -std=gnu++17 -o CMakeFiles/Vulkan.dir/main.cpp.o -c /home/****/workspace/vulkan/main.cpp",
  "file": "/home/****/workspace/vulkan/main.cpp",
  "output": "CMakeFiles/Vulkan.dir/main.cpp.o"
}
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.