Can I restore a makefile for a C project with the correct link order and dependencies?

I have the source code that I worked in at the end of the 90s-2000, and all of its backups, except for the make file (yes, kill, bad backups are almost as good as no backups): so .. I wonder if there is any automated way to create a makefile or a good way to quickly analyze dependencies?

In particular, I am looking for:

  • a tool that can analyze dependencies and adjust the order of links for me.
  • If this does not exist, then the council greatly appreciates how best to approach this problem for someone who has had a similar problem in the past
  • without completing either of the above two options, I think the best approach is to create a tool for creating analysis / creating files that can automatically generate the order of dependencies for binding (I settled on this approach, because time is always a short sentence to compress in another project )

The reason for this search for reference / advice is that the codebase is 300,000 lines of code (excluding comments) and covers hundreds of C / O files and as often as I tried to create a make file manually, it frustrates and mixes, therefore my last try to ask for help and ask here.

For reference: I tried Cmake , AutoMake, GenMake and similar tools in the past to create a makefile, all to no avail, because the dependencies are terrible.


script

, makefile, C ++, :

$(VERBOSE).SILENT:
PROGRAMNAME = prog
CC = gcc
CC += -c
CPP = g++
CPP += -c
ASM = nasm
ASM += -f elf -d ELF_TYPE
LD = g++
OBJFILES = $(patsubst %.c,%.o,$(wildcard *.c))
OBJFILES += $(patsubst %.s,%.o,$(wildcard *.s))
OBJFILES += $(patsubst %.cpp,%.o,$(wildcard *.cpp))

all: $(PROGRAMNAME)

clean:
    @echo "Cleaning object files"
    @echo "    rm -f     *.o"
    rm -f *.o
    @echo "Cleaning backups"
    @echo "    rm -f     *~"
    rm -f *~
    @echo "Removing program file"
    @echo "    rm -f     "$(PROGRAMNAME)
    rm -f $(PROGRAMNAME)

%.o: %.s
    @echo "Assembling ASMs "$@
    @echo "    ASM       "$<
    $(ASM) $<

%.o: %.c
    @echo "(C)ompiling "$@
    @echo "    CC        "$<
    $(CC) $<

%.o: %.cpp
    @echo "(C++)ompiling "$@
    @echo "    CPP       "$<
    $(CPP) $<

$(PROGRAMNAME): $(OBJFILES)
    @echo "Get ready...."
    @echo "Linking "$@
    @echo "    LD        -o "$(PROGRAMNAME)"        "$(OBJFILES)
    $(LD) -o $(PROGRAMNAME) $(OBJFILES)
    @echo "Cry if it worked! Scream swear and cry if it did not..."

strip: $(PROGRAMNAME)
    @echo "Stripping "$(PROGRAMNAME)
    echo -n "Size of "$(PROGRAMNAME)" before stripping is "
    ls -sh $(PROGRAMNAME) | cut -d' ' -f1
    @echo "    Stripping     "$(PROGRAMNAME)
    strip $(PROGRAMNAME)
    echo -n "Size of "$(PROGRAMNAME)" after stripping is "
    ls -sh $(PROGRAMNAME) | cut -d' ' -f1

nothing:
    @echo "Nothing to do; see you later - I'm going home!!!"
    @echo "Hey, try some of these:"
    @echo "make all   - this would be the one you want"
    @echo "make strip - does not work in the real world, only in computers"
    @echo "make clean - will help clean your mind up"
+5
4

Unix MIT, makedepend.

+4
+4

linux/unix:

find . -name "*.c" -print > sources .

find . -name "*.c" -print|sed s/\.c/\.o > objects , "OBJECTS =" [, ].

cat sources|xargs gcc -M > myprog.deps , include myprog.deps make . [1]

  TARGET = myprog # , !

OBJECTS = ... # from "objects" file above. 
SOURCES = ... # from "sources" file above

INCLUDES = -I subdir1 -I subdir2 ...     # include directories used by this product

CFLAGS = ... ${INCLUDES} # Some suitable settings
CC = gcc
LD = ${CC}
LDFLAGS =  ...    # I don't know what this needs to be - usually nothing complicated.

all: ${TARGET}

clean:
    rm -f ${TARGET} ${OBJECTS}

${TARGET}: ${OBJECTS}
    ${LD} -o $@ ${OBJECTS}

.c.o:
    ${CC} -o $@ $<

, , , - , , "main" - Makefile include .

[1] make , .

myprog.deps: ${SOURCES}
    ${CC} -MM ${SOURCES} > myprog.deps

include myprog.deps
+1

Thank you for the excellent answer: concise and very informative. Based on the answers, I now use a mixture of manual effort and GNU AutoMake (the modern successor to makedepend) before trying to recompile, and still seems pretty effective.

Then the pleasure will come and porting games to OO-code in C ++ ... that is a task that I would gladly avoid, but needs should.

Thanks again!

0
source

All Articles