Results 1 to 5 of 5

Thread: creating a makefile for C++

  1. #1
    Join Date
    Jul 2012
    Location
    Elmira, NY
    Beans
    283
    Distro
    Ubuntu 14.04 Trusty Tahr

    creating a makefile for C++

    If you have multiple cpp files, h files, or even some in dubdirectories, what do you put in the makefile?

    For example,
    Code:
    metulburr@ubuntu:~/Documents/cplusplus/composition$ ls
    Birthday.cpp  Birthday.h  main.cpp  People.cpp  People.h
    metulburr@ubuntu:~/Documents/cplusplus/composition$

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: creating a makefile for C++

    Start by reading/researching: http://www.gnu.org/software/make/manual/make.html


    For the simple project layout you posted, the following basic/simple Makefile should suffice:
    Code:
    # Application name; change if needed.
    APP = bday
    
    # Listing of source code files
    SRCS = Birthday.cpp main.cpp  People.cpp
    
    # Formulate the names of the respective object files
    OBJS = $(SRCS:.cpp=.o)
    
    # Define appropriate compiler options
    CXXFLAGS = -Wall -pedantic
    
    # Define appropriate link options and libraries
    LDFLAGS =
    LIBS =
    
    
    .PHONY = all clean
    
    
    all : $(APP)
    
    
    $(APP) : $(OBJS)
            $(CXX) $^ $(LDFLAGS) $(LIBS) -o $@
    
    
    clean :
            $(RM) $(OBJS) $(APP)

    Note:
    Code:
    $(APP) : $(OBJS)
    <tab-space>$(CXX) $^ $(LDFLAGS) $(LIBS) -o $@
    
    
    clean :
    <tab-space>$(RM) $(OBJS) $(APP)

  3. #3
    Join Date
    Aug 2012
    Beans
    185

    Re: creating a makefile for C++

    It's slightly off topic, but if you haven't considered cmake yet, imo you should. If you go the make route you'll have to learn/use autotools, also known as autohell, cmake is much simpler (and crossplatform), more modern and faster. (And yes, nothing is ideal, but some things are generally (much) better than others).

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: creating a makefile for C++

    Quote Originally Posted by bird1500 View Post
    ... If you go the make route you'll have to learn/use autotools...
    Your assumption is incorrect. I use Makefiles all the time; never once have I had to use autotools.

  5. #5
    Join Date
    Aug 2012
    Beans
    185

    Re: creating a makefile for C++

    Quote Originally Posted by dwhitney67 View Post
    Your assumption is incorrect. I use Makefiles all the time; never once have I had to use autotools.
    Please check my spelling too. When people say something they don't mean it in absolute terms, unless explicitly told so.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •