[c] C error: undefined reference to function, but it IS defined

Just a simple program, but I keep getting this compiler error. I'm using MinGW for the compiler.

Here's the header file, point.h:

//type for a Cartesian point
typedef struct {
  double x;
  double y;
} Point;

Point create(double x, double y);
Point midpoint(Point p, Point q);

And here's point.c:

//This is the implementation of the point type
#include "point.h"

int main() {
  return 0;
}
Point create(double x, double y) {
  Point p;
  p.x = x;
  p.y = y;
  return p;
}

Point midpoint(Point p, Point q) {
  Point mid;
  mid.x = (p.x + q.x) / 2;
  mid.y = (p.y + q.y) / 2;
  return mid;
}

And here's where the compiler issue comes in. I keep getting:

testpoint.c: undefined reference to 'create(double x, double y)'

While it is defined in point.c.

This is a separate file called testpoint.c:

#include "point.h"
#include <assert.h>
#include <stdio.h>
int main() {
  double x = 1;
  double y = 1;
  Point p = create(x, y);

  assert(p.x == 1);
  return 0;
}

I'm at a loss as to what the issue could be.

This question is related to c function linker-errors undefined-reference

The answer is


I had this issue recently. In my case, I had my IDE set to choose which compiler (C or C++) to use on each file according to its extension, and I was trying to call a C function (i.e. from a .c file) from C++ code.

The .h file for the C function wasn't wrapped in this sort of guard:

#ifdef __cplusplus
extern "C" {
#endif

// all of your legacy C code here

#ifdef __cplusplus
}
#endif

I could've added that, but I didn't want to modify it, so I just included it in my C++ file like so:

extern "C" {
#include "legacy_C_header.h"
}

(Hat tip to UncaAlby for his clear explanation of the effect of extern "C".)


I think the problem is that when you're trying to compile testpoint.c, it includes point.h but it doesn't know about point.c. Since point.c has the definition for create, not having point.c will cause the compilation to fail.

I'm not familiar with MinGW, but you need to tell the compiler to look for point.c. For example with gcc you might do this:

gcc point.c testpoint.c

As others have pointed out, you also need to remove one of your main functions, since you can only have one.


Add the "extern" keyword to the function definitions in point.h


Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to function

$http.get(...).success is not a function Function to calculate R2 (R-squared) in R How to Call a Function inside a Render in React/Jsx How does Python return multiple values from a function? Default optional parameter in Swift function How to have multiple conditions for one if statement in python Uncaught TypeError: .indexOf is not a function Proper use of const for defining functions in JavaScript Run php function on button click includes() not working in all browsers

Examples related to linker-errors

Duplicate Symbols for Architecture arm64 Multiple definition of ... linker error What is an undefined reference/unresolved external symbol error and how do I fix it? Why am I getting "undefined reference to sqrt" error even though I include math.h header? What causes the error "undefined reference to (some function)"? Undefined reference to 'vtable for xxx' C error: undefined reference to function, but it IS defined Undefined reference to sqrt (or other mathematical functions) Undefined reference to `sin` Linker error: "linker input file unused because linking not done", undefined reference to a function in that file

Examples related to undefined-reference

DSO missing from command line gcc: undefined reference to libpthread.so.0: error adding symbols: DSO missing from command line What is an undefined reference/unresolved external symbol error and how do I fix it? Undefined reference to `pow' and `floor' C error: undefined reference to function, but it IS defined Undefined Reference to Undefined reference to sqrt (or other mathematical functions) Undefined reference to `sin` C++ undefined reference to defined function