Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minimum c++ make file for linux

Tags:

I've looking to find a simple recommended "minimal" c++ makefile for linux which will use g++ to compile and link a single file and h file. Ideally the make file will not even have the physical file names in it and only have a .cpp to .o transform. What is the best way to generate such a makefile without diving into the horrors of autoconf?

The current dir contains, for example

t.cpp t.h

and I want a makefile for that to be created. I tried autoconf but its assuming .h is gcc instead of g++. Yes, while not a beginner, I am relearning from years ago best approaches to project manipulation and hence am looking for automated ways to create and maintain makefiles for small projects.

like image 300
RichieHH Avatar asked Nov 13 '08 15:11

RichieHH


People also ask

What is in C make file?

Makefile is a set of commands (similar to terminal commands) with variable names and targets to create object file and to remove them. In a single make file we can create multiple targets to compile and to remove object, binary files.

What is makefile in Linux?

Makefile is a program building tool which runs on Unix, Linux, and their flavors. It aids in simplifying building program executables that may need various modules. To determine how the modules need to be compiled or recompiled together, make takes the help of user-defined makefiles.

What is $@ in makefile?

The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.


1 Answers

If it is a single file, you can type

make t 

And it will invoke

g++ t.cpp -o t 

This doesn't even require a Makefile in the directory, although it will get confused if you have a t.cpp and a t.c and a t.java, etc etc.

Also a real Makefile:

SOURCES := t.cpp # Objs are all the sources, with .cpp replaced by .o OBJS := $(SOURCES:.cpp=.o)  all: t  # Compile the binary 't' by calling the compiler with cflags, lflags, and any libs (if defined) and the list of objects. t: $(OBJS)     $(CC) $(CFLAGS) -o t $(OBJS) $(LFLAGS) $(LIBS)  # Get a .o from a .cpp by calling compiler with cflags and includes (if defined) .cpp.o:     $(CC) $(CFLAGS) $(INCLUDES) -c $< 
like image 95
hazzen Avatar answered Nov 01 '22 18:11

hazzen