Skip to main content
hans536
Associate III
October 26, 2022
Solved

The problem is that the variables and header files declared in ".h" cannot be loaded from the ".c" file.

  • October 26, 2022
  • 3 replies
  • 3670 views

I want to work by adding the c and h files I created.

The problem is that the variables and header files declared in ".h" cannot be loaded from the ".c" file.

Below is the situation I tested.

The c and h files I created are core.c and core.h.

not problem As shown in the figure, there was no problem when "components.h" and "stdio.h" were included in the core.c file and variables were declared.

However, an error occurred when variables declared in core.h and header files were included.

I would like to ask for help on how to solve it.

    This topic has been closed for replies.
    Best answer by ODOUV.1

    Hello, annyeonghaseyo,

    the first quick work around is to use:

    #include "../include/core.h"

    0693W00000VO7tKQAT.png 

    But I will look how to fix Makefile to extend include directories.

    got bwayo

    Best regards.

    3 replies

    KnarfB
    Super User
    October 26, 2022

    Defining a global variable in a header file often causes trouble and is generally not recommended or even forbidden in some domains, like MISRA C, see https://analyst.phyzdev.net/documentation/help/reference/misra.onedefrule.var.htm

    There seems to be another issue in your code as the compiler warns about printf which should be declared in stdio.h. ...

    hth

    KnarfB

    ODOUV.1
    ST Employee
    October 26, 2022

    This is also true

    the variable should be declared in the .h (extern uint8_t abcd;)

    and instantiated in the .c (uint8_t abcd = 255;)

    ODOUV.1
    ODOUV.1Best answer
    ST Employee
    October 26, 2022

    Hello, annyeonghaseyo,

    the first quick work around is to use:

    #include "../include/core.h"

    0693W00000VO7tKQAT.png 

    But I will look how to fix Makefile to extend include directories.

    got bwayo

    Best regards.

    hans536
    hans536Author
    Associate III
    October 26, 2022

    thank you

    ODOUV.1
    ST Employee
    October 26, 2022

    I found the root cause of your issue:

    0693W00000VO8PzQAL.pngwe already have a core.h in our sample code

    so you need to rename your file (eg mycore.h)

    and include it

    #include "mycore.h"

    Best regards,

    -Olivier