Question
Linking header file still leads to undefined reference
I have three files:
vector2d.h
referencing my functions.
vector2d.c
defining my functions.
test_vectors.c
as the main file.
test_vectors.c
doesn't build because it returns an undefined reference error for every function.
I'm using geany and I don't necessarily have a terminal to write anything in, just a build and run button.
vector2d header
typedef struct {
double x;
double y;
} vec2d;
void display_vector(const vec2d v);
double magnitude(const vec2d v);
vec2d add(const vec2d v1, const vec2d v2);
vec2d scale(const vec2d v, const double factor);
double dot_product(const vec2d v1, const vec2d v2);
vector2d C file
#include "vector2d.h"
#include <stdio.h>
#include <math.h>
void display_vector(const vec2d v){
printf("%.2f, %0.2f", v.x, v.y);
}
double magnitude(const vec2d v){
double result = sqrt((v.x*v.x) + (v.y * v.y));
return result;
}
vec2d add(const vec2d v1, const vec2d v2){
vec2d result;
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
return result;
}
vec2d scale(const vec2d v, const double factor){
vec2d result;
result.x = v.x * factor;
result.y = v.y * factor;
return result;
}
double dot_product(const vec2d v1, const vec2d v2){
double result = (v1.x * v2.x) + (v1.y * v2.y);
return result;
}
test_vectors file
#include <stdio.h>
#include "vector2d.h"
int main (void){
int choice;
vec2d v1, v2, result;
double factor, dot;
do {
printf("1) Add vectors; 2) Scale vector; 3) Dot product; 4) Exit: ");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Enter vector 1 (x, y): ");
scanf("(%lf, %lf)", &v1.x, &v1.y);
printf("\n");
printf("Enter vector 2 (x, y): ");
scanf("(%lf, %lf)", &v1.x, &v1.y);
printf("\n");
result = add(v1, v2);
printf("Result: ");
display_vector(result);
printf("\n");
case 2:
printf("Enter vector (x, y): ");
scanf("(%lf, %lf)", &v1.x, &v1.y);
printf("\n");
printf("Enter scaling factor: ");
scanf("%lf", &factor);
printf("\n");
result = scale(v1, factor);
printf("Result: ");
display_vector(result);
printf("\n");
case 3:
printf("Enter vector 1 (x, y): ");
scanf("(%lf, %lf)", &v1.x, &v1.y);
printf("\n");
printf("Enter vector 2 (x, y): ");
scanf("(%lf, %lf)", &v1.x, &v1.y);
printf("\n");
dot = dot_product(v1, v2);
printf("Result: %f\n", dot);
case 4:
printf("Bye!");
break;
}
} while(1);
return 0;
}
I've tried putting the function prototypes in test_vectors.c
, but that obviously didn't help. I can't seem to link them properly using this geany IDE.