Search code examples
gccclangninja

Ninja doesn't use CC and CXX?


I'm compiling Tesseract with Ninja. Clang has some linking problem, but gcc works. I exports CC and CXX with gcc and make -j8 runs fine. However, Ninja (using the given script in /build folder) still uses clang. How can I specify compiler in Ninja, using a given .ninja script?


Solution

  • Method1:

    ninja is a very simplified and powerful build system. You only need to look at build.ninja and rules.ninja files to make relevant changes.

    build.ninja contains build commands to be run in order to build the project.

    rules.ninja contains rules, CC, CXX and other flags. In order to change the compiler for a specific rule you'd need to grep for that target in rules.ninja. There might be several rules you'd like to change, in that case you can use some bash magic.

    Method2

    All said, changing *.ninja files is not recommended as they are generated by cmake. So you should modify the compiler paths (CMAKE_CXX_COMPILER, CMAKE_C_COMPILER flags) in CMakeCache.txt file in your build directory.

    Method3

    Remove the build directory completely, then export CC and CXX flags or pass the -DCMAKE_CXX_COMPILER:FILEPATH=/path/to/c++compiler -DCMAKE_C_COMPILER=/path/to/c-compiler while invoking cmake. e.g.,

    cmake -GNinja \
    -DCMAKE_CXX_COMPILER:FILEPATH=/path/to/c++compiler \
    -DCMAKE_C_COMPILER=/path/to/c-compiler \
    ... <other-flags>
    <path-to-llvm-repo>
    

    I'd recommend Method3, YMMV. hth.