4

I am trying to use CMake for the MIT JOS Operating system project - http://repo.or.cz/mit-jos.git/tree or https://pdos.csail.mit.edu/6.828/2016/jos.git

Here is the directory structure

lab
- .bochsrc
- CODING
- GNUmakefile
- boot
- conf
- fs
- grade.sh
+ inc
    - assert.h
    - elf.h
    - error.h
    - kbdreg.h
    - memlayout.h
    - mmu.h
    - stab.h
    - stdarg.h
    - stdio.h
    - string.h
    - types.h
    - x86.h
- kern
- lib
- mergedep.pl
- user
- CMakeLists.txt

The CMakeLists.txt under the lab folder (i.e. the project folder) looks like

    cmake_minimum_required(VERSION 3.6)
    project(lab)

    set(CMAKE_CXX_STANDARD 11)

    set(SOURCE_FILES
            boot/main.c
            fs/test.c
            kern/console.c
            kern/console.h
            kern/entrypgdir.c
            kern/init.c
            kern/kdebug.c
            kern/kdebug.h
            kern/monitor.c
            kern/monitor.h
            kern/printf.c
            lib/printfmt.c
            lib/readline.c
            lib/string.c
            user/sendpage.c)

    add_executable(lab ${SOURCE_FILES})

How can I include the header files under inc so that the source files can still include them using #include <inc/types.h> etc. rather than #include "../inc/types.h" ?

srgsanky
  • 671
  • 1
  • 11
  • 16
  • Judging from your folder tree, I would guess that whoever created it wanted you to add `inc` as include folder, and use `#include ` instead of `#include `. – at-2500 Mar 19 '17 at 20:14

1 Answers1

7

It sounds like you need to add an include directory for your target with target_include_directories, for example (at the end of your CMakeLists.txt):

target_include_directories(lab PRIVATE ${CMAKE_SOURCE_DIR} )

As for the difference between angle brackets and quotes when using #include, generally you use angle brackets only for 'system' header files (such as the C/C++ standard headers), and quotes for user supplied header (see What is the difference between #include <filename> and #include "filename"?).

Community
  • 1
  • 1
MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78
  • I tried adding `target_include_directories(lab PRIVATE inc)` to the end of `CMakeLists.txt` under `lab` directory. I still see the same kind of error `fatal error: 'inc/mmu.h' file not found #include `. Do I need a `CMakeLists.txt` under the `inc` directory? If so what should it contain? – srgsanky Mar 18 '17 at 16:12
  • 2
    @srgsanky: It is root source directory (`lab` in you case) in which the header file can be reffered as `inc/mmu.h`. So your need to include this directory instead: `target_include_directories(lab PRIVATE ${CMAKE_SOURCE_DIR})`. – Tsyvarev Mar 18 '17 at 21:54
  • 3
    I don't agree with your distinction between `""` and `<>`. It is very common to use `<>` also for non-system header, especially if you have a library with a set of header files that contain the public API of the library. Then, you would put the public headers into a folder named for example `include` and `target_include_directories(mylib PUBLIC include)` so that the include folder is automatically forwarded to anyone who links to your library. – at-2500 Mar 19 '17 at 20:19
  • Thanks @Tsyvarev and MuertoExcobito. Adding target_include_directories(lab PRIVATE ${CMAKE_SOURCE_DIR}) to the end of the CMake file solved the problem. CLion shows no error now. – srgsanky Mar 20 '17 at 05:43