2

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.

4
  • 3
    You don't build with the vector2d.c source file. You need to explicitly build it and link with its generated object file. Commented Jul 19, 2024 at 23:00
  • 2
    You also don't link header files. You link the libraries that contain the symbols used in your code that are defined in the header file. Header files are used by the compiler, not the linker. Commented Jul 19, 2024 at 23:56
  • If you are just using the default commands from build menu (like F5, ...) -I, -l etc. are not set automaticity on gcc Commented Jul 20, 2024 at 9:03
  • In geany, when you have multiple files you need to configure a project with instructions how to build them. Commented Jul 20, 2024 at 11:26

1 Answer 1

1

Running your code through GCC gcc test_vectors.c vector2d.c -o prgm compiled just fine, so it's entirely possible the build system underpinning geany is misidentifying what to target in it's build step. I'd look around (or google) what files it's building and make sure it's test_vectors.c and vector2d.c, for all your concerns the header file shouldn't matter as a build mention.

P.S. I obviously don't know your level of skill, but I'd recommend starting header files with:
#ifndef FILE_NAME
#define FILE_NAME
["File name" can be really anything, but most commonly the file name] Then end the file with an #endif. Wish you luck!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.