【发布时间】:2014-05-06 05:09:35
【问题描述】:
我有一个名为reliability的目录:
ls reliability
analyze.c appl.sh constr.c creer.c greedys.c Makefile
现在我要安装和编译它:
首先Makefile的内容是:
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)
当我开始编译它时:
make -f Makefile
发生错误:
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
我通过将-lm 添加到 gcc 来传递这些错误,
即使我将 CFLAGS 从 Makefile 更改为 CFLAGS = -g -Wall -pthread -lm 也会出现该错误。
我的问题:
我现在应该单独编译所有.c程序吗?
谢谢你,我真的需要帮助,
更新
当我编译creer.c时出现错误:
creer.c:39:10: warning: unused variable ‘val2’ [-Wunused-variable] creer.c:38:7:
warning: unused variable ‘val’ [-Wunused-variable]
creer.c 的一部分是:
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);
}
【问题讨论】:
-
在将 CFLAGS 放入 Makefile 之前,让您的编译命令在命令行上运行。
-
你能解释一下吗?没看懂,谢谢
-
google for
undefined reference to 'exp' math.h揭示了 Undefined reference to exp on Ubuntu (including math.h and linking with -lm) 的可能重复项 -
是的,它有:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include <ctype.h> #include <math.h> -
有关于未使用变量的警告。不是错误。
标签: c linux compilation compiler-errors makefile