Header files are not linking properly (some variables/functions are undefined) but the project builds (vscode)

zazas321
Posts: 235
Joined: Mon Feb 01, 2021 9:41 am

Header files are not linking properly (some variables/functions are undefined) but the project builds (vscode)

Postby zazas321 » Tue Jun 13, 2023 5:11 am

Hello. I have this very weird issue for my project. I have noticed that some variables in my custom component .c file show up as undefined even though they are defined in .h file and included in the .c file.

But even though it shows as undefined, I can right click the variable and go to its definition and it will open up the corresponding header file for me which means that its actually linking properly but VScode for some reason thinks it is undefined.

See example below:
I have a "test" component that contains test.c and test.h and CMakeLists.txt:
test_component.png
test_component.png (8.27 KiB) Viewed 1401 times
In test.h I have declared the following functions and variables:

Code: Select all



#ifdef CONFIG_IDF_TARGET_ESP32
    #define GPIO_IA_1                    GPIO_NUM_16
    #define GPIO_IA_2                    GPIO_NUM_4
    #define GPIO_IB_1                    GPIO_NUM_14
    #define GPIO_IB_2                    GPIO_NUM_12

    #define IR_RIGHT                     GPIO_NUM_22
    #define IR_LEFT                      GPIO_NUM_18
    #define IR_MIDDLE                    GPIO_NUM_19
    #define IR_MIDDLE_LEFT               GPIO_NUM_23
    #define IR_MIDDLE_RIGHT              GPIO_NUM_21
#endif


enum motor_control_e{
    COAST,
    FORWARD,
    BACKWARDS,
    STOP
};

void motor_A_control(enum motor_control_e control);
void motor_B_control(enum motor_control_e control);
void motor_GPIO_setup();

And in my test.c I include the header and use those functions and enums but they show up as undefined:
showing_as_undefined.png
showing_as_undefined.png (28.47 KiB) Viewed 1401 times
Even some standard esp-idf GPIO driver keywords show up as undefined!
showing_as_undefined_2.png
showing_as_undefined_2.png (27.4 KiB) Viewed 1401 times
Even though they show up as undefined, as I said, the project builds fine and I can even go to definition by rightclicking and it will open test.h where the variables are defined but I am not sure why the vscode complains about this. Please can someone help me solve this as it is very frustrating to see this. Thanks in advance!

g01d10x
Posts: 3
Joined: Sun Apr 01, 2018 8:19 pm

Re: Header files are not linking properly (some variables/functions are undefined) but the project builds (vscode)

Postby g01d10x » Sun Mar 30, 2025 10:04 am

Did you ever get any resolution on this?

User avatar
egionet
Posts: 39
Joined: Thu Sep 26, 2024 4:34 am
Location: Atlantic Canada

Re: Header files are not linking properly (some variables/functions are undefined) but the project builds (vscode)

Postby egionet » Fri Apr 04, 2025 8:48 am

VS-Code exhibits the same behaviour for me with PlatformIO when a 'clean' is initiated or a new component is added. However, when I restart VS-Code after a build, it stops complaining. I've gotten so used to it that I haven't bothered to investigate what causes this problem. As long as it compiles, I am content with this quirk.
Regards,

Eric Gionet P.Tech.(Eng.), CIOTP, SMIEEE

Who is online

Users browsing this forum: Baidu [Spider], PerplexityBot and 2 guests