是否有必要为.c和.o文件使用相同的名称?

时间:2014-08-19 05:52:06

标签: c makefile object-files

CC=gcc -Wall
CFLAGS = -Wno-pointer-sign
LDFLAGS= -lipq

all: mtu rtu obj

mtu: flexiBitw_mtu
rtu: flexiBitw_rtu

flexiBitw_mtu: packetCapture.o mtu_decodePkt.o encrypt_decrypt.o sha1.o crypt.o file_parse.o
        $(CC) $(CFLAGS) packetCapture.o mtu_decodePkt.o encrypt_decrypt.o sha1.o crypt.o file_parse.o -o flexiBitw_mtu $(LDFLAGS)

flexiBitw_rtu: packetCapture.o rtu_decodePkt.o encrypt_decrypt.o sha1.o crypt.o file_parse.o
        $(CC) $(CFLAGS) packetCapture.o rtu_decodePkt.o encrypt_decrypt.o sha1.o crypt.o file_parse.o -o flexiBitw_rtu $(LDFLAGS)

obj:
        rm -f *.o

packetCapture.o: packetCapture.c
        $(CC) $(CFLAGS) -c packetCapture.c file_parse.c 
mtu_decodePkt.o: mtu_decodePkt.c
        $(CC) $(CFLAGS) -DCOMPILE_MTU -c mtu_decodePkt.c
rtu_decodePkt.o: mtu_decodePkt.c
        $(CC) $(CFLAGS) -DCOMPILE_RTU -c mtu_decodePkt.c
encrypt_decrypt.o: encrypt_decrypt.c sha1.c crypt.c
        $(CC) $(CFLAGS) -c encrypt_decrypt.c sha1.c crypt.c

clean:
        rm -rf *.o flexiBitw_mtu
        rm -rf *.o flexiBitw_rtu

它输出为:

gcc -Wall  -Wno-pointer-sign -c packetCapture.c file_parse.c    
gcc -Wall  -Wno-pointer-sign -DCOMPILE_MTU -c mtu_decodePkt.c
gcc -Wall  -Wno-pointer-sign -c encrypt_decrypt.c sha1.c crypt.c
gcc -Wall  -Wno-pointer-sign packetCapture.o mtu_decodePkt.o encrypt_decrypt.o sha1.o crypt.o file_parse.o -o flexiBitw_mtu -lipq
gcc -Wall  -Wno-pointer-sign -DCOMPILE_RTU -c mtu_decodePkt.c
gcc -Wall  -Wno-pointer-sign packetCapture.o rtu_decodePkt.o encrypt_decrypt.o sha1.o crypt.o file_parse.o -o flexiBitw_rtu -lipq
gcc: rtu_decodePkt.o: No such file or directory
make: *** [flexiBitw_rtu] Error 1

我正在通过在mtu_decodepkt.c文件中启用不同的宏来进行条件编译 但它给出错误rtu_decodePkt.o:没有这样的文件或目录。  我不得不更改.o文件名,以便在制作两个可执行文件时不一致。

请回复......

2 个答案:

答案 0 :(得分:1)

您需要告诉编译器您希望调用目标文件:

rtu_decodePkt.o: mtu_decodePkt.c
        $(CC) $(CFLAGS) -DCOMPILE_RTU -c mtu_decodePkt.c -o rtu_decodePkt.o
                                                         ^^^^^^^^^^^^^^^^^^

答案 1 :(得分:1)

不,没有必要。您可以使用GCC的-o选项指定目标文件的名称,并根据需要链接.o文件。

但在这种情况下,您一次只能编译一个.c文件。