[英]compiling C project with makefile
I have a directory called reliability
: 我有一个名为
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: 首先,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)
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, 我通过在-gcc中添加
-lm
来传递这些错误,
Even when I change CFLAGS
from Makefile
to CFLAGS = -g -Wall -pthread -lm
there is also that error. 即使当我将
CFLAGS
从Makefile
更改为CFLAGS = -g -Wall -pthread -lm
,也存在该错误。
My question: 我的问题:
Should I compile all .c
programs separately now? 我现在应该分别编译所有
.c
程序吗?
Thank you I really need help, 谢谢,我真的需要帮助,
When I compile creer.c
there is an error: 当我编译
creer.c
出现错误:
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: 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);
}
You need to add that linker option -lm
to the end of your command line. 您需要将该链接器选项
-lm
添加到命令行的末尾。
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. 并用它代替所有这些有关构造,压料等的规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.