I have a large sketch that I would like to break into multiple source files. Basically a library but not use a class (I have to understand classes much better first). The functions make use of global variables that I need to access in the main sketch and in the functions itself. I created an very small example that recreates the problem/scenario.

The variable needs to be declared in the header file (func.h) just like a function prototype. But it cannot be initialized in the header file. Instead the initialization can happen in either source file. Since my global vars are specific to the library, it makes the most sense to put the initialization into the func.cpp file.

In my actual project I initialized all the global variables in the header file. I did not do that in the example I posted, but the results are nonetheless the same. So here is a func.cpp with which the project will build just fine.

although state and pin are declared to be extern in func.h, and although func.h has been included to help the linker resolve references to them, you have not defined these two variables in the code you've shown. You need to do that.

"extern int foo;" -> declares a variable which essentially informs the compiler we intent to use it"int foo;" -> defines the variable and causes memory to be allocated, but the content is undefined"int foo = 42;" -> defines and initializes the variable, memory is allocated and set to the value (42)