reza moslemi
reza moslemi

Reputation: 33

error function declared but never defined

I have added a library to my c project in codeVision AVR. when I want to use it's functions receive this error: function 'function name' is declared but never defined. here is my code:

#include "pid.h"
#include <mega32.h>
PidType _pid;
void main(void)
{
//some uC hardware initializing codes which are removed here to simplify code
PID_Compute(&_pid);
while (1)
  {
  // Place your code here

  }
}

in pid.h:

.
.
bool PID_Compute(PidType* pid);
.
.

and pid.c:


#include "pid.h"
.
.
bool PID_Compute(PidType* pid) {
  if (!pid->inAuto) {
    return false;
  }
    FloatType input = pid->myInput;
    FloatType error = pid->mySetpoint - input;
    pid->ITerm += (pid->ki * error);
    if (pid->ITerm > pid->outMax)
      pid->ITerm = pid->outMax;
    else if (pid->ITerm < pid->outMin)
      pid->ITerm = pid->outMin;
    FloatType dInput = (input - pid->lastInput);

    FloatType output = pid->kp * error + pid->ITerm - pid->kd * dInput;

    if (output > pid->outMax)
      output = pid->outMax;
    else if (output < pid->outMin)
      output = pid->outMin;
    pid->myOutput = output;


    pid->lastInput = input;
    return true;
}

the ERROR:

function 'PID_Compute' declared, but never defined.

Where is the problem?

EDIT:

to add the library to my project I placed the .c and .h library files in the same folder that my main project file is:

enter image description here

and then #include "pid.h" in my main file:

#include "pid.h"
#include <mega32.h>

// Declare your global variables here
PidType _pid;
void main(void)
{
.
.

my error and warnings: enter image description here

enter image description here

enter image description here

EDIT2: I simplified the code and now can show you the entire code: main code:

#include "pid.h"
PidType _pid;
void main(void)
{
PID_Compute(&_pid);
while (1)
      {

      }
}

pid.h:

#ifndef PID_H
#define PID_H

#include <stdbool.h>

typedef struct {
 int i;
} PidType;


bool PID_Compute(PidType* pid);

#endif

pid.c:

#include "pid.h"
bool PID_Compute(PidType* pid) {

    pid->i = 2;
    return true;
}

Upvotes: 1

Views: 1673

Answers (2)

the busybee
the busybee

Reputation: 12610

From the screenshot with the tree view of your files it is clear that the file "pid.c" is not part of the project.

Move it into your project. Then it should build without that linker error.

This does not mean the location in the file system. I reference the "virtual" view of the IDE on your project.

Upvotes: 1

reza moslemi
reza moslemi

Reputation: 33

thank you every body. As you said, the pid.c was not added to the project. for those who may face the same problem: in codeVision AVR we have to add .c file to project from project->configure->files->input files->add

I addec .c file to project and the error went away.

Upvotes: 1

Related Questions