This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Make and call a C function

Using Keil MDK Lite v 5.22, writing code in C for nRF52, with SDK 12.

Example code works.

How does one declare a function, define it, and call it?

I've created a new module: myFunction.c and a header file: myFunction.h.

I added a folder under the project folder named: myFunction. I added the C file under that folder. I couldn't figure out how to add a header file, so I added the function prototype to the header file stdio.h.

Here's the declaration in stdio.h: void myFunction(void);

In the C file, the definition looks like this: void myFunction(void) { int myVar = 0; myVar++;

if(myVar ==1) {
myVar = 0; } }

Then, I called the function from main(), like this: myFunction();

The code compiles without issue, uploads to the dev board, and runs. When I debug and break on the function, the code breaks as expected, but it skips over the steps in the function.

I'm new to this system, and this is the first time I'm adding a function. I'm sure there's a simple, fundamental step missing, but I need some help.

Any ideas? Can anyone point me to a tutorial that describes adding a function?

Thanks,

  • OK, I found the problem. I was expecting there to be some non-ANSI C setting up of function declarations/definitions, but it turned out to be a path problem.

    While looking for a solution, I saw many references to path problems, but at first glance I didn't think it was the issue here. Since my header file was located in the same directory as the rest of my project, and the other project files were found, I didn't think there was a path issue.

    I tested it by placing my header file (myFunction.h) in the same path as stdio.h. Then my header file worked.

    So, the solution was to open the Windows File Explorer window to the folder where my header file was located and copy the path. Then I pasted it in the C/C++ Include Paths text box in the Options for Target dialog window at the end of all the other paths.

    I still don't know why it couldn't find the header file in the working directory without adding the path.

    Hopefully this will help someone else with similar problems.

    robin

Related