.c and .h files

I am sure there is an easy solution for this.

I have a function that returns a string
String GetValueFromCSV(String theValue) {
blah blah
return value;
}

If this snippit of code resides in my main.ccp

a call in setup Serial.println(GetValueFromCSV(“Network”)); works

However if I move the code to a file cmdFunctions.h
make a include #include “../lib/cmdFunctions.h”

I do not get anything from Serial.println(GetValueFromCSV(“Network”));
I know the function is running because of a printf(“First Part: %s, Second Part: %s\n”, firstPart.c_str(), secondPart.c_str()); << produces a response
Should the code reside in a .c file and a .h be made?
new to C++ so excuse my ignorance

.c files should contain definition of functions and variables.

.h files should contain declarations of the functions and variables.

That means that the .c file needs only to be compiled once and any other code block that wants to reference those functions and variables (etc) need only #include the appropriate .h file.

Note that the .h file is needed as you compile other .c files. However the later linker phase actually connects the call to a function (or the reference to a variable) in one module to the actual function (or variable) in another module.

BTW this has nothing to do with c++ per se - it is basic c program structuring (and also other compilers that are based/derived from c).

1 Like