Unknown CMake command "add_clang_executable"
Asked Answered
P

1

6

I am trying to build a clang tool, and I am trying to debug it using CLion. However, I can't get it to compile as a standalone binary. Currently I have this in my CMakeLists.txt:

add_clang_executable(clang_my_tool my_tool_util.h my_tool_util.cpp ClangMyTool.cpp)

target_link_libraries(clang_my_tool PRIVATE clangTooling)

However, it's giving me the error message:

Unknown CMake command "add_clang_executable".

I know I need to add an directory, but I don't know which one to add.

My llvm directory looks something like this:

llvm
|-build
  |- ...
|
|-clang
  |-tools
    |-clang_my_tool
      |-ClangMyTool.cpp
      |-my_tool_util.h
      |-my_tool_util.c
      |-CMakeLists.txt
|- ... other directories...

What do I add to my CMakeLists.txt?

Protomorphic answered 8/10, 2020 at 23:13 Comment(2)
Have you seen this? clang-developers.42468.n3.nabble.com/…Atcliffe
@JohnZwinck Yes, this is what I want, basically. However, the link provided in the thread unfortunately links to a 5-year old repostiory, and doesn't seem to work...Protomorphic
A
2

You need to include the LLVM CMakeLists.txt file which defines add_clang_executable(). Currently that file is here (and may well already be installed on your system): https://github.com/llvm/llvm-project/blob/master/clang/cmake/modules/AddClang.cmake

If you figure out the path to that file on your system, add this to your own CMakeLists.txt:

include("/path/to/AddClang.cmake")
Atcliffe answered 11/10, 2020 at 2:0 Comment(3)
Unfortunately, since paths in clang are relative, even if I add this, CMake will fail: ``` fatal error: clang/AST/Attr.h: No such file or directory #include "clang/AST/Attr.h" ^~~~~~~~~~~~~~~~~~ ``` Due to how the include statements are in the clang source code itself -- it's all relative from the top directoryProtomorphic
@gust: I think you can solve that by adding some -I rules to your compiler command, so it finds the headers relative to whatever directory you specify.Atcliffe
-I rules to compilers?Protomorphic

© 2022 - 2024 — McMap. All rights reserved.