main.h file not found in project dir unless full Windows path is provided

This is likely a Visual Studio issue but figured this is an obvious rookie error so someone will set me straight right away! I have loaded a sample project and compiled and run it properly on my hardware development kit; I can debug and all is good. However, the 'main.c' file that was provided contains a lot of stuff that I would normally expect to see in a 'main.h' file (which wasn't included as part of the sample). I created the main.h file and added it to the same folder as main.c and the intelisense cannot find it if I use "#include <main.h>". However, if I instead include the entire Windows directory path with the file name, then it works OK. How is the system not seeing the file since it is clearly in the project directory?

Parents
  • Hi Paul,

    Two paths forward:

    1) you can use #include "main.h"

    2) you can use #include <main.h> if the path for that file is included in CMakeLists.txt by adding it as an include path.

    For instance, if main.h is in the same location as main.c in your_project/src, then adding zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src) before the project sources will work. 

    Best regards,

  • I added the command as you suggested (except the file is now in a sub-directory called "inc" but I get an error telling me that the build failed due to "CMake Generate step failed. This is my CMakeLists.txt file:

    #
    # Copyright (c) 2018 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    cmake_minimum_required(VERSION 3.20.0)

    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

    zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)

    project(NONE)

    # NORDIC SDK APP START
    target_sources(app PRIVATE
    src/main.c,
    inc/main.h
    )

    # NORDIC SDK APP END

Reply
  • I added the command as you suggested (except the file is now in a sub-directory called "inc" but I get an error telling me that the build failed due to "CMake Generate step failed. This is my CMakeLists.txt file:

    #
    # Copyright (c) 2018 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    cmake_minimum_required(VERSION 3.20.0)

    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

    zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)

    project(NONE)

    # NORDIC SDK APP START
    target_sources(app PRIVATE
    src/main.c,
    inc/main.h
    )

    # NORDIC SDK APP END

Children
Related