I am having trouble compiling/linking the following C code. The linker throws errors that look like the following:
pso.o:pso.c:(.text+0x41): undefined reference to '_ps'
...
pso.o:pso.c:(.text+0x93): more undefined references to '_ps' follow
This is my first time writing C code for gcc, so I'm unsure how to fix this problem. I am assuming that because struct PS is defined my header file, it is somehow not linked to pso.c. However, I did use a #include "ps.h" statement at the top of that source file.
I have included the relevant source files and header file below, as well as the make file I am using. Is there a fundamental concept I am missing for writing linkable C code?
Thank you! Oh, and it's a particle swarm optimizer, if you were wondering :)
The main.c file:
#define MAIN
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "ps.h"
int main(int argc, char *argv[])
{
int c;
double test;
int test_int;
srand(time(NULL));
printf("starting pso\n");
pso();
printf("finished pso\n");
return(0);
}
The file in question, pso.c:
#include<stdio.h>
#include "ps.h"
double it(double C[]);
void pso()
{
int i;
int j;
int k;
puts("Now in PSO");
/* Initialize PSO controls */
psoi();
/* Initialize particle positions and velocities */
for (i=0;i<MPART;i++){
for(j=1;j<MINDV;j++){
/* positions */
ps.X[i][j]=(-1+2*random())*ps.bup;
/* velocities */
ps.V[i][j] = (-1+2*random())*ps.bup / ps.vred;
/* Transform */
ps.ut[j] = (ps.X[i][j] - ps.blo)/(ps.bup - ps.blo) * (ps.bnd_hi[i] - ps.bnd_lo[i]) + ps.bnd_lo[i];
}
/* Evaluate Finess */
ps.fitness = fit(ps.ut);
/* Update particle best position */
ps.pbest[i]=ps.fitness;
/* Update swarm best position */
if(ps.pbest[i] < ps.sbest){
for(j=0;j<MINDV;j++){
ps.g[j]=ps.p[i][j];
}
ps.sbest = ps.pbest[i];
}
}
/* Convergenve Loop */
for(k=2;k<ps.maxk;k++){
for(i=1;i<MPART;i++){
for(j=1;j<MINDV;j++){
/* Velocity Update */
ps.rp = random();
ps.rg = random();
ps.rr = random();
ps.chi = 0.7298;
ps.V[i][j] = ps.chi * ( ps.V[i][j] + ps.phi_p*ps.rp*(ps.p[i][j] - ps.X[i][j]) + ps.phi_g*ps.rg*(ps.g[j] - ps.X[i][j]));
/* Position Update */
ps.X[i][j] = ps.X[i][j] + ps.V[i][j];
/* Transform */
ps.ut[j] = (ps.X[i][j]-ps.blo)/(ps.bup-ps.blo)*(ps.bnd_hi[i]-ps.bnd_lo[i])+ps.bnd_lo[i];
}
/* Evaluate Fitness */
ps.fitness = fit(ps.ut);
/* Update particle best position */
if(ps.fitness < ps.pbest[i]){
for(j=0;j<MINDV;j++){
ps.p[i][j]=ps.X[i][j];
}
ps.pbest[i]=ps.fitness;
}
/* Update swarm best position */
if(ps.pbest[i] < ps.sbest){
for(j=0;j<MINDV;j++){
ps.g[j]=ps.p[i][j];
}
ps.sbest = ps.pbest[i];
printf("%f \n",ps.sbest);
}
}
/* Convergence Criteria */
}
/* return(0); */
}
The header file which defines the data structure PS (ps.h):
#ifndef _PS_
#define _PS_
#define MPART 30
#define MINDV 2
typedef struct{
/* Design Space */
double X[MPART][MINDV];
double V[MPART][MINDV];
double p[MPART][MINDV];
double u[MINDV];
double ut[MINDV];
double sbest;
double sbest_old;
double g[MINDV];
double gt[MINDV];
double pbest[MPART];
double pbest_sort[MPART];
double blo;
double bup;
double fitness;
double maxk;
double bnd_lo[MINDV];
double bnd_hi[MINDV];
/* PSO behavior */
int psotype;
double w;
double phi_p;
double phi_g;
double gam;
double chi;
double vred;
double rp;
double rg;
double rr;
}PS;
/* Dependent Source File Functions */
extern void pso();
extern double fit(double C[2]);
#ifndef MAIN
extern PS ps;
#endif
#endif
Lastly, the make file I am using:
CC=gcc
CFLAGS= -I.
DEPS = ps.h
OBJ = main.o pso.o psoi.o fit.o
default: program
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
program: $(OBJ)
gcc -o $@ $^ $(CFLAGS)