If I use C code in a c++ environment and I include all of the code inside the header, everything works fine. If I try to declare C functions in a header and them implement them in a .c or .cpp file, I get the following error:
Undefined symbols for architecture x86_64:
"vec2_norm(Vec2)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Vec2.h
#ifndef Physics_Engine_Test_Vec2_h
#define Physics_Engine_Test_Vec2_h
typedef struct
{
float x;
float y;
} Vec2;
inline Vec2 vec2_norm(Vec2 v);
#endif
Vec2.c or .cpp
#include "Vec2.h"
#include <math.h>
inline Vec2 vec2_norm(Vec2 v) {
float len = v.x*v.x + v.y*v.y;
if (len) {
len = 1 / sqrtf(len);
v.x *= len;
v.y *= len;
}
return v;
}