Undefined reference to '_*' linker error

2019-08-17 02:21发布

问题:

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)

回答1:

In "ps.h", you have the line

extern PS ps;

— but this only declares ps (i.e. assert it exists somewhere); it doesn't provide a definition (i.e. make it exist here). What you should do is, step one, replace

#ifndef MAIN
extern PS ps;
#endif

with just

extern PS ps;

and then, step two, go into pso.c and somewhere at the top level of that file add

PS ps = {};

(or PS ps;, or extern PS ps = {};). If your C compiler is old or dumb enough to complain about the empty braces, write PS ps = {0}; instead; {} is a C++'03-ism that I believe is being adopted into C'11 but I'm not 100% certain about that.

Briefly, the rule about declarations is: If you provide an initializer with =, then the declaration must be a definition (because how else could you initialize the variable here?). Otherwise, if you used the extern storage-class, it's not a definition (because mnemonically, you're saying that the variable exists extern-ally to this file). Otherwise, it's a definition (but in K&R C it's a tentative definition, and many compilers will either put it in the BSS section by default or else provide a command-line option to do so).



回答2:

You only declare ps as extern, but you never actually define it anywhere. Write

PS ps;

in one of the C files.



回答3:

The linker output is slightly confusing, it's really complaining about the definition of the object ps being missing. You've declared it extern, but never defined it anywhere.