minimum c++ make file for linux

2019-01-31 08:47发布

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.

11条回答
Root(大扎)
2楼-- · 2019-01-31 09:09

If your issues are because autoconf thinks the .h file is a c file, try renaming it to .hpp or .h++

查看更多
Deceive 欺骗
3楼-- · 2019-01-31 09:10

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 $<
查看更多
闹够了就滚
4楼-- · 2019-01-31 09:10

Have you looked at SCons?

Simply create a SConstruct file with the following:

Program("t.cpp")

Then type:

scons

Done!

查看更多
闹够了就滚
5楼-- · 2019-01-31 09:11

Assuming no preconfigured system-wide make settings:

CXX = g++
CPPFLAGS =        # put pre-processor settings (-I, -D, etc) here
CXXFLAGS = -Wall  # put compiler settings here
LDFLAGS =         # put linker settings here

test: test.o
    $(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) test.o

.cpp.o:
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<

test.cpp: test.h
查看更多
闹够了就滚
6楼-- · 2019-01-31 09:12

SConstruct with debug option:

env = Environment()

if ARGUMENTS.get('debug', 0):
    env.Append(CCFLAGS = ' -g')

env.Program( source = "template.cpp" )
查看更多
放荡不羁爱自由
7楼-- · 2019-01-31 09:14

a fairly small GNU Makefile, using predefined rules and auto-deps:

CC=c++
CXXFLAGS=-g -Wall -Wextra -MMD
LDLIBS=-lm
program: program.o sub.o
clean:
    $(RM) *.o *.d program
-include $(wildcard *.d)
查看更多
登录 后发表回答