minimum c++ make file for linux

后端 未结 11 1867
眼角桃花
眼角桃花 2020-12-23 17:32

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

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 17:52

    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 $<
    

提交回复
热议问题