2

I've been working on this for a while now. I have these files:

main.c
zSim.h 
zSim.c
zDynamicArray.h 
zDynamicArray.c
zOptions.h 
zOptions.c
zMain.h 
zMain.c

All of the files and headers are located within the same folder.

My main has the following includes:

#include "zDynamicArray.h"
#include "zOptions.h"
#include "zMain.h"
#include "zSim.h"
#include <stdio.h>
#include <math.h>

I am using header guards. Just for example, this is from my zSim.h file:

#ifndef SIM_H
#define SIM_H
#include "zDynamicArray.h"
#include "zOptions.h"

//This Header is accountable
//for all Simulation Related things. 

int Simulate(SIMOPT so,PRINTOPT po);

#endif

and this is a snippet from my zSim.c code (maybe I am doing something wrong here?):

#define M_PI 3.14159265358979323846
#include "zSim.h"
#include "zDynamicArray.h"
#include "zOptions.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

I am compiling using this gcc command:

gcc main.c zSim.c zOptions.c zDynamicArray.c zMain.c -o TEST

and I also used:

cc -c main.c
cc -c zSim.c
cc -c ... etc
cc *.o -o TEST

They all result in an undefined reference for anything in the math.h library. Also when I use the gcc -I command, math is still undefined.

If i compile using gcc main.c, I get unresolved references for anything in my header files.

What should I do?

Ren
  • 1,111
  • 5
  • 15
  • 24
zellwwf
  • 83
  • 1
  • 8

2 Answers2

4

try ading -lm at the end : gcc -c main.c cc -c zSim.c cc -c ... etc cc *.o -o TEST -lm

You have to link the math library with the -lm option. The reason why is explained here : Why do you have to link the math library in C?

Community
  • 1
  • 1
lucasg
  • 10,734
  • 4
  • 35
  • 57
2

they all result in an undefined reference for anything in the math.h library

math.h is only the header file which contains prototypes and type declarations. You also need to link against the math library when creating your executable. Add -lm to your gcc call:

gcc main.c zSim.c zOptions.c zDynamicArray.c zMain.c -o TEST -lm

See also Undefined reference to `sin`.

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123