Search code examples
cmakefileobject-files

is it necessary to have same names for .c and .o 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

it gives output as :

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

I am doing conditional compilation by enabling different macros in mtu_decodepkt.c file but it is giving error rtu_decodePkt.o: No such file or directory. I had to change the .o file name so that it should not coincide in making both the executables.

please reply ...


Solution

  • You need to tell the compiler what you want the object file to be called:

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