0

CMake generates compile_commands.json in cmake build directory which means that my installation of YouCompleteMe or YCM cannot find it. For YCM to use it I need to move it from the build directory to the source or top level of the project.

Project
|- CMakeLists.txt
|- compile_commands.json (there is what i want)
|- build (this iw where i build my project) 
|-- compile_commands.json (this is what i have)
|-- OtherCmakeGenerateStuff
|- src
|-- CMakeLists.txt
|-- main.cc (includes library not in project)
|-lib
|-- CMakeLists.txt
|-- math
|--- CMakeLists.txt
|--- math.cc
|--- math.hh
|-include
|-- globals.hh
|-- definitions.hh

There may be mistakes in these files as I just typed it and haven't tested it. however, my focus is on the top level CMakeLists.txt file.

CMakeLists.txt (Top level)

cmake_minimum_require(VERSION 3.8)

project(just_some_project LANGUAGES CXX VERSION 1.0)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_subdirectory(src)
add_subdirectory(lib)
include_directories(include)

src/CMakeLists.txt

cmake_minimum_require(VERSION 3.8)

project(just_some_project LANGUAGES CXX VERSION 1.0)

find_project(someotherlibrary REQUIRED)

add_executable(main main.cc)
target_include_directories(main PUBLIC math someotherlibraryfound)

lib/CMakeLists.txt (Top level)

cmake_minimum_require(VERSION 3.8)

file(GLOB Headers "*.hh")
file(GLOB Sources "*.cc")

add_library(math STATIC $Sources $Headers)
Brothaman
  • 199
  • 1
  • 5
  • 1
    Possibly related: [Copy compile_commands.json to project root folder](/q/57464766/11107541) – starball Jan 05 '23 at 23:24
  • @starball this is pretty much what I was looking for but hoping someone knew of a more elegant solution. This requires calling cmake twice to get the up-to-date compile_commands.json into the project's root directory. – Brothaman Jan 07 '23 at 05:37
  • Possible workaround: just symlink or copy to it in your source directory? If you want to do it during CMake's configuration process, see [`file(COPY_FILE)` and `file(CREATE_SYMLINK)`](https://cmake.org/cmake/help/latest/command/file.html) Not sure if copying at configure time works since I'm not sure when during the configuration process the `compile_commands.json` file gets generated, but a symlink should be immune to that ordering problem, no? If that works as a workaround for you, ping me and I'll write an answer post. – starball Jan 07 '23 at 06:01

0 Answers0