Find the answer to your Linux question:
Results 1 to 2 of 2
I am banging my head trying to get a certain kind of Makefile to work. Here is what I have so far. Code: CXX = g++ LIBS = -L$(SYSTEMC_HOME)/lib-$(TARGET_ARCH) -lsystemc ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2005
    Posts
    8

    Makefile


    I am banging my head trying to get a certain kind of Makefile to work. Here is what I have so far.

    Code:
    CXX  = g++
    LIBS = -L$(SYSTEMC_HOME)/lib-$(TARGET_ARCH) -lsystemc
    INC  = -I$(SYSTEMC_HOME)/include
    SOURCES = file1.cpp file2.cpp
    OBJS:=$(SOURCES:%.cpp=%.o)
    
    all:$(OBJS)
    
    file1.o:
    	$(CXX) $(INC) -c file1.cpp
    
    file2.o:
    	$(CXX) $(INC) -c file2.cpp
    
    .PHONY: clean
    clean:
    	rm -f *.o
    Note I am only building objects at this point. I want to make this work in a general way so I only have to add new source files (i.e. file3.cpp) to the $SOURCES variable instead of adding a new "file3.o:" line. I've tried various things I have googled but it isn't working how I want. I am either seeing strange results (errors) or compiling each file at once which isn't what I want either. Can someone help please?

  2. #2
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    173
    Here's a Makefile I came up with a couple of months ago that seems related to what you're trying to do. I can just add a program name to the list at the time and all the rules to compile and link work for whichever one I "make". You might be able to copy bits and pieces out.

    Code:
    PROGS = capture-udp-port send-udp-message capture-udpv6-port send-udpv6-message capture-udp-port-hex capture-udpv6-port-hex
    
    # ---
    
    CC = gcc
    LD = gcc
    CFLAGS = -g -Wall -pedantic
    LDFLAGS = 
    
    # ---
    
    default:
    	@echo "You can make any of the following programs: $(PROGS)"
    
    # ---
    
    .PHONY: all
    all: $(PROGS)
    
    # ---
    
    %.o : %.c Makefile
    	$(CC) -c -o $@ $(CFLAGS) $<
    
    $(PROGS) : % : %.o
    	$(CC) -o $@ $(@).o $(LDFLAGS)
    
    # ---
    
    clean:
    	rm $(PROGS) *.o

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •