0

I am writing a simple C program that changes the value of a data structure to 100.

#include"node.h"
#include <stdio.h>

int main() {
    simpleObject object1;
    object1.x = 200;
    updateLocation(&object1);
    printf("%d", object1.x);
}

It holds 2 other files, the header file named "node" and the source file also named "node".

"node.h"

typedef struct {
    float x;
    float y;
    float z;
    float mass;
} simpleObject;

void updateLocation(simpleObject* initObject);

"node.c"

#include "node.h"

void updateLocation(simpleObject* initObject) {
    initObject -> x = 100;
}

When the file was compiled, I was given this error:

undefined reference to `updateLocation(simpleObject*)' collect2.exe: error: ld returned 1 exit status

I am completely confused as to why the updateLocation function is not defined, even though it is defined within "node.c".

When the code from "node.c" was deleted and placed in "node.h" in place of the declaration, it worked, but printed out 0.

What is occuring within my code?

  • How did you compile the code? Was it `gcc main.c -o a.out` or `gcc main.c node.c -o a.out`? – Harith Mar 05 '23 at 06:04
  • 1
    [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) . That's not a compile-time error; it's link-time. It means you're trying to use a function that, even if properly declared in a header, isn't actually defined in any of your source files or consumed libraries. It nearly always means you're failing to build and/or link at least one of those. – WhozCraig Mar 05 '23 at 06:11
  • @Haris I used ```gcc main.c -o a.out``` – Tricide Mar 05 '23 at 06:26
  • Well, `gcc` has no way of knowing about `node.c` unless you apprise it of it. Specify `node.c` on the command line, just as you did with `main()`. – Harith Mar 05 '23 at 06:27

0 Answers0