How to set up module in include library


#1

I want to set up a module in the “lib” folder of my project and #include it in my main program, but I have not been able to get it to work. I have followed the instructions in the Platformio pages about setting up a library in the “lib” folder, and followed the instructions in how to set up an include module in a c++ tutorial here (http://www.learncpp.com/cpp-tutorial/19-header-files/), and I just can’t get it to work. Everything compiles, but the link fails, with an “Undefined reference” error. I’ve tried it in CodeBlocks and VSCode and the same problem happens in both. I use Linux Mint 18.1.
Does anyone have an example of a header and process file I can include in my program so I can understand how to set up the files correctly?
Thanks.

Sorry, I think I’m suffering from delusions of understanding C. Apologies for wasting everybody’s time.
:-\


#3

Have you seen this file?


#4

Hi Ivan,
Yes I have seen that page, and I’ve set up my lib exactly as per the foo example, i.e. just the myFile.c and myFile.h in a folder called myFile in the lib folder. The includes work fine. I think my problem is that I was trying to do something that is just “not” C, if you see what I mean. I wanted do define some global variables in the main.c, and then reference them directly from within a #included file, assuming the #included file would be able to find them as they are global. I thought #include simply copied the text of the target into the place where the #include is define. Then I learned that each included function must be compilable on its own, so I put all the variables into myFile.h, and included that in the myFile.c. I also included it in the main.c, thinking that the main.c program could also reference the same variables, but that gave me the linker errors, probably because I had a guard set up in the .h, so one of my files was not getting the .h file included in it. Without the guard I was getting duplicate definition errors.
I’ve since learned that the correct way is to call the function referencing the variables in the function call parameters (using pass by reference). As I want to pass several arrays and some other variables, I guess it’s going to get a bit messy, and I might be better off just defining the function in the main.c itself as it’s not very big, < 50 lines of code. Or maybe I can put everything in a struct or something. Not sure, I’ve just started playing around with it.
Thanks for having a look at this for me.
:wink: Ian