Question

I have a directory called reliability:

ls reliability
analyze.c  appl.sh  constr.c  creer.c  greedys.c  Makefile

Now I want to install and compile it:

First of all the content of Makefile is:

CC = gcc
CFLAGS = -g -Wall -pthread

SRCS = constr.c creer.c analyze.c greedys.c
PROG = constr creer analyze greedys

all: $(PROG)

constr: constr.c
        $(CC) $(CFLAGS) -o constr constr.c

creer: creer.c
        $(CC) $(CFLAGS) -o creer creer.c

analyze: analyze.c
        $(CC) $(CFLAGS) -o analyze analyze.c

greedys: greedys.c
        $(CC) $(CFLAGS) -o greedys greedys.c

clean: 
        rm -f $(PROG)

When I start compiling it:

make -f Makefile 

The error occurs:

gcc -g -Wall -pthread -o constr constr.c
/tmp/cca4NKQl.o: In function `main':
/home/t1/Desktop/reliability/constr.c:89: undefined reference to `exp'
/home/t1/Desktop/reliability/constr.c:89: undefined reference to `exp'
/home/t1/Desktop/reliability/constr.c:89: undefined reference to `pow'
/home/t1/Desktop/reliability/constr.c:89: undefined reference to `log'
collect2: ld returned 1 exit status
make: *** [constr] Error 1

I pass these errors by adding -lm to gcc,

Even when I change CFLAGS from Makefile to CFLAGS = -g -Wall -pthread -lm there is also that error.

My question:

Should I compile all .c programs separately now?

Thank you I really need help,

UPDATE

When I compile creer.c there is an error:

creer.c:39:10: warning: unused variable ‘val2’ [-Wunused-variable] creer.c:38:7:
               warning: unused variable ‘val’ [-Wunused-variable]

Part of creer.c is:

void values(){
  int val;
  double val2;
  FILE *fp;
  fp = fopen("instances","r");
  fseek(fp,0,SEEK_SET);
  if(fscanf(fp,"%d",&p)==EOF){printf("EOF\n");}
  if(fscanf(fp,"%d",&m)==EOF){printf("EOF\n");}
  if(fscanf(fp,"%d",&K)==EOF){printf("EOF\n");}
  if(fscanf(fp,"%lf",&lambda_com)==EOF){printf("EOF\n");}
  if(fscanf(fp,"%d",&num_inst)==EOF){printf("EOF\n");}
  if(fscanf(fp,"%d",&boundl)==EOF){printf("EOF\n");}
  if(fscanf(fp,"%d",&boundp)==EOF){printf("EOF\n");}
  fclose(fp);
}
Was it helpful?

Solution

You need to add that linker option -lm to the end of your command line.

Do something like this:

LDFLAGS=-lm

...

constr: constr.c    
        $(CC) $(CFLAGS) -o constr constr.c $(LDFLAGS)

Or you could define a pattern rule:

%: %.c
        $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

and use it to replace all those rules for constr, creer, and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top