Search code examples
c++linuxcmakeclang

Clang with cmake. target_include_directories "No such file or directory"


Here is my c++ project.

├── CMakeLists.txt
├── include
│   └── ccli
        └──  # headers
├── src
│   ├── CMakeLists.txt
│   ├── exec_expr.cpp
│   ├── GlobalContext.cpp
│   ├── main.cpp
    └──  # others

/CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)                                                                  

project(main)                                                                                        

set(CMAKE_CXX_FLAGS "-Wall -std=c++17")                                                              

find_package(LLVM REQUIRED CONFIG)                                                                   
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION} in ${LLVM_INSTALL_PREFIX}")                       
message(STATUS "Using LLVMConfig.cmake in ${LLVM_DIR}")                                              

find_package(Clang REQUIRED CONFIG HINTS "{LLVM_INSTALL_PREFIX}/lib/cmake/clang")                    
message(STATUS "Found Clang in ${CLANG_INSTALL_PREFIX}")                                             
message(STATUS "Found Cland headers in ${CLANG_INCLUDE_DIRS}")                                       
message(STATUS "Found LLVM headers in ${LLVM_INCLUDE_DIRS}")                                         

add_subdirectory(src)                                                                                

add_executable(                                                                                      
    main                                                                                             
        src/main.cpp                                                                                 
)                                                                                                    

target_include_directories(main PRIVATE ${LLVM_INCLUDE_DIRS})                                        
target_include_directories(                                                                          
    main PUBLIC                                                                                      
        ${PROJECT_SOURCE_DIR}/include                                                                
)                                                                                                    

# Fixes clang linking error.                                                                         
target_compile_definitions(main PUBLIC -DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING)                 
target_link_libraries(main ccli readline)  

/src/CMakeLists.txt:

cmake_minimum_required (VERSION 3.5)                             

add_library(                                                     
    ccli STATIC                                                  
        exec_expr.cpp                                            
        runToolOnCode.cpp                                        
        GlobalContext.cpp                                        
        Utility.cpp                                              
)                                                                

target_include_directories(ccli PRIVATE "${LLVM_INCLUDE_DIR}")   
target_include_directories(                                      
    ccli PRIVATE                                                 
        ${PROJECT_SOURCE_DIR}/include                            
)                                                                

target_link_libraries(                                           
    ccli PRIVATE                                                 
        clangTooling                                             
        clangFrontend                                            
        clangStaticAnalyzerFrontend                              
)                                                                

top of src/main.cpp:

#include "ccli/runToolOnCode.hpp"          
#include "ccli/GlobalContext.hpp"          
#include "ccli/exec_expr.hpp"              
#include "ccli/Utility.hpp"                

#include "clang/Frontend/FrontedActions.h"  //<- error occures here                                   

Problem is all files used in add_library() can include and use clang headers, but main.cpp cannot. Also I don't understand why main.cpp is allowed to include /include/ccli headers, as I understand, it means that this code works well:

target_include_directories(                                                                          
    main PUBLIC                                                                                      
        ${PROJECT_SOURCE_DIR}/include                                                                
)

But this not?:

target_include_directories(main PRIVATE ${LLVM_INCLUDE_DIRS})

And finally Output of cmake .

-- Found LLVM 8.0.0 in /usr/lib/llvm-8
-- Using LLVMConfig.cmake in /usr/lib/llvm-8/cmake
-- Found Clang in /usr/lib/llvm-8
-- Found Cland headers in /usr/lib/llvm-8/include
-- Found LLVM headers in /usr/lib/llvm-8/include
-- Configuring done
-- Generating done
-- Build files have been written to: /c_repl/build

Output of make

/c_repl/src/main.cpp:6:10: fatal error: clang/Frontend/FrontedActions.h: No such file or directory
 #include "clang/Frontend/FrontedActions.h"
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/main.dir/build.make:62: recipe for target 'CMakeFiles/main.dir/src/main.cpp.o' failed
make[2]: *** [CMakeFiles/main.dir/src/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Any help or tips would be appreciated. Thanks. PS Working on linux

Distributor ID: Ubuntu
Description:    Ubuntu 18.04.3 LTS
Release:        18.04
Codename:       bionic

PPS: Sorry for my bad english


Solution

  • Seems to be a typo, try: #include "clang/Frontend/FrontendActions.h"

    Hope that helps