How to link extern variable defines in a header file to the main source file?

MikeLemon
Posts: 45
Joined: Tue Feb 02, 2021 5:55 pm

How to link extern variable defines in a header file to the main source file?

Postby MikeLemon » Fri May 20, 2022 7:51 pm

I'm trying to access a variables defines in a file I created named button_funcs.h in the same directory as the main.c file and there I defined a freeRTOS QHandle such as extern xQueueHandle DoItQ;
,when I come to compile it with CMake it gives me the following error:
c:/coding/esp32/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(main.c.obj):(.literal.displayRoutine+0x4c): undefined reference to `TimerTickQ'
I have defined the INCLUDE_DIRS "." in IDF component register in CMakeLists.txt and added button_funcs.c to SRCS if that matters.

What am I doing wrong? isn't that how you suppose to use extern variables? how'd one make it work?

User avatar
mbratch
Posts: 298
Joined: Fri Jun 11, 2021 1:51 pm

Re: How to link extern variable defines in a header file to the main source file?

Postby mbratch » Fri May 20, 2022 7:57 pm

Sorry to ask a dumb question, but since you didn't mention.... did you include the header in your `main.c` file?

Also, in your top level CMakeList.txt, you might have a command that looks like this:

Code: Select all

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") 
In my case, I have my includes in a separate `include` subdirectory. If yours are in your `main` directory, you could indicate that here.

MikeLemon
Posts: 45
Joined: Tue Feb 02, 2021 5:55 pm

Re: How to link extern variable defines in a header file to the main source file?

Postby MikeLemon » Sat May 21, 2022 8:14 am

Hi there, Yes I have included that in the main.c file yes,
and I have the header in the same folder and the cmakelists.txt file looks as such:

Code: Select all

idf_component_register(SRCS "main.c" "u8g2_esp32_hal.c" "my_inits.c" "button_funcs.c"
                       INCLUDE_DIRS "." )
which I assume it's the same as what you mentioned
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
any idea?

User avatar
mbratch
Posts: 298
Joined: Fri Jun 11, 2021 1:51 pm

Re: How to link extern variable defines in a header file to the main source file?

Postby mbratch » Sat May 21, 2022 10:50 pm

MikeLemon wrote:

Code: Select all

idf_component_register(SRCS "main.c" "u8g2_esp32_hal.c" "my_inits.c" "button_funcs.c"
                       INCLUDE_DIRS "." )
which I assume it's the same as what you mentioned
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
The project I have doesn't use that particular directive.

Where is your `CMakeList.txt`? Is it in your `main` source folder, or at the top project level (just above `main`)?

MikeLemon
Posts: 45
Joined: Tue Feb 02, 2021 5:55 pm

Re: How to link extern variable defines in a header file to the main source file?

Postby MikeLemon » Sun May 22, 2022 4:34 pm

I Actually figured out what I did wrong to achieve "linking global variable" from one source file to another or the main.c so what I did is first defined the global variables I want to init in say funcs1.c and then declared it in it's header file funcs1.h note there is a difference between define and declare a variable and that is all covered up in this stackoverflow thread additionally you add it to SRC files in CMakeLists.txt in the same directory as main as mentioned above, hope this is useful for anybody.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 117 guests