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?

  • Hi Paul,

    Would it be possible to attach your sample project so that I can try to run it here at my end?

    Regards,

    Priyanka

  • 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

  • Hello,

    main.h is not a source file, so remove it from target_sources(...)

    If inc/ is in the project directory (i.e. not within src), then that path should be ok on zephyr_include_directories(...)

    Best regards,

  • Yes, I added the main.h file to the target_source list while debugging this issue; it didn't fix the problem but it doesn't stop a clean compile either when I provide the full windows path in the #include statement. I removed it from the target_sources list as you suggested and now the compile is working with "include <main.h>". That makes absolutely no sense to me at any level; a novice like me would expect that if you use the GUI to add a file to the project, the GUI should at least have the decency to ask if it is needed for the build and then set the appropriate commands in the configuration files. I think this explains why I never became a professional programmer! Thanks for your help!

Related