Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple makefile generation utility?

Tags:

c++

makefile

Does anyone know of a tool that generates a makefile by scanning a directory for source files?

It may be naive:

  • no need to detect external dependencies
  • use default compiler/linker settings
like image 670
StackedCrooked Avatar asked Aug 26 '10 15:08

StackedCrooked


People also ask

What is makefile utility?

Make - a tool that automatically determines which source files of a program need to be recompiled and/or linked. Make gets its knowledge of how to build your program from a file called the makefile (or Makefile).

Can Visual Studio generate a makefile?

To create a makefile project in Visual StudioFrom the Visual Studio main menu, choose File > New > Project and type "makefile" into the search box. If you see more than one project template, select from the options depending on your target platform.

How is makefile generated?

Creating a Makefile A Makefile typically starts with some variable definitions which are then followed by a set of target entries for building specific targets (typically .o & executable files in C and C++, and . class files in Java) or executing a set of command associated with a target label.

What is $@ in makefile?

The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.


2 Answers

You can write a Makefile that does this for you:

SOURCES=$(shell find . -name "*.cpp")
OBJECTS=$(SOURCES:%.cpp=%.o)
TARGET=foo

.PHONY: all
all: $(TARGET)

$(TARGET): $(OBJECTS)
        $(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@

.PHONY: clean
clean:
        rm -f $(TARGET) $(OBJECTS)

Just place this in root directory of your source hierarchy and run make (you'll need GNU Make for this to work).

(Note that I'm not fluent in Makefileish so maybe this can be done easier.)

like image 78
mtvec Avatar answered Nov 15 '22 19:11

mtvec


CMake does it and it even creates makefiles and Visual Studio projects. http://www.cmake.org/

All you need to do is creating a CMakeLists.txt file containing the follwing lines:

file(GLOB sources *.h *.c *.cxx *.cpp *.hxx)
add_executable(Foo ${sources})

Then go into a clean directory and type:

cmake /path/to/project/

That will create makefiles on that clean build directory.

like image 30
tibur Avatar answered Nov 15 '22 21:11

tibur