0

I can not solve these "undefined reference to" problem. There error could be in my header file. my IDE is CodeBlocks

#include<windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include "maths.h"
#include <math.h>

int main(int argc, char *argv[])
{
    //stuff
    struct Vector v1;
    struct Vector v2;

    v1.x = 3;
    v1.y = 4;

    v2.x = 6;
    v2.y = 10;



    struct Vector v3 = addVectors(v1,v2);

    printf("%f %f",v3.x,v3.y);

}

maths.h

#include <math.h>
#include <stdio.h>

#ifndef MATHS_H_INCLUDED
#define MATHS_H_INCLUDED

struct Vector {

    float x;
    float y;

};

    void setVector(struct Vector *v, float x, float y);

    struct Vector addVectors(struct Vector v1, struct Vector v2);

    void makeUnit(Vector *v);

#endif // MATHS_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "maths.h"

void setVector(struct Vector *v, float x, float y){

    v->x = x;
    v->y = y;

}

struct Vector addVectors(struct Vector v1, struct Vector v2){

    int x1 = v1.x;
    int x2 = v2.x;
    int y1 = v1.y;
    int y2 = v2.y;

    struct Vector v3;

    v3.x = x1 + x2;
    v3.y = y1 + y2;

    return v3;

}

 void makeUnit(Vector *v){

    double a = (v->x * v->x) + (v->y * v->y);
    double lengthvector = sqrt(a);

    v->x = x/lengthvector;
    v->y = y/lengthvector;

}

I can compile struct Vector but I can't compile functions.

||=== Build: |192|undefined reference to `addVectors(Vector, Vector)'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    You don't seem to build with the source file containing the `addVectors` funciton. – Some programmer dude May 23 '19 at 14:14
  • 1
    Agree with Some programmer dude. You're getting an error from the linker `ld`, which means that it's not a `#include` problem. You haven't told your IDE to build your `maths.c` file and link it into your project. – brhans May 23 '19 at 14:17
  • If the argument types are quoted in the error message, it normally means you're compiling with a C++ compiler instead of a C compiler. C++ compilers do 'name mangling' which identifies the arguments so that functions can be overloaded. – Jonathan Leffler Nov 06 '22 at 04:38

1 Answers1

-2

try to change the function like that :

struct Vector* addVectors(struct Vector v1, struct Vector v2);

And in this function return a pointer of Vector

mm98
  • 409
  • 1
  • 6
  • 18