Search code examples
cmakefilecc

Is it possible to compile multiple target files using CC compiler?


I am using Minix 2.0.4 so I can't use gcc. I want to use one make file to compile multiple C programs, with multiple targets.

Here is the current state of my Makefile

CFLAGS  = -D_POSIX_SOURCE
LDFLAGS =
CC      = cc
LD      = cc

PROG    = prog1 prog2 
OBJS    = prog1.o prog2.o

$(PROG): $(OBJS)
         $(LD) $(LDFLAGS) $(OBJS) -o $(PROG)

clean:
         rm -rf $(PROG) $(OBJS)

However, when I try and use my makefile like this is get an error that says "prog2: can't compile, not transformation applies". Any ideas on what I'm doing wrong?


Solution

  • Split it up this way:

    PROG1 = test 
    PROG2 = test2
    OBJ1 = test.o
    OBJ2 = test2.o
    
    
    all: $(PROG1) $(PROG2)
    
    $(PROG1): $(OBJ1) 
              $(LD) $(LDFLAGS) $(OBJ1) -o $(PROG1)
    $(PROG2): $(OBJ2) 
              $(LD) $(LDFLAGS) $(OBJ2) -o $(PROG2)
    

    etc

    If all that subsitution makes you nervous, you can more simply say

    all: test test1
    
    test: test.o
          $(LD) $(LDFLAGS) test.o -o test
    test2: test2.o
          $(LD) $(LDFLAGS) test2.o -o test2
    

    And remove this from the beginning:

    PROG1 = test 
    PROG2 = test2
    OBJ1 = test.o
    OBJ2 = test2.o
    

    There are other shortcuts, but this more specific and obvious.