C++: Difference between revisions

From Rixort Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 27: Line 27:
  $(EXECUTABLE): $(OBJECTS)
  $(EXECUTABLE): $(OBJECTS)
   $(CC) $(LDFLAGS) $(OBJECTS) -o $@
   $(CC) $(LDFLAGS) $(OBJECTS) -o $@
 
  .cpp.o:
  .cpp.o:
   $(CC) $(CFLAGS) -c $< -o $@
   $(CC) $(CFLAGS) -c $< -o $@
 
  clean:
  clean:
   rm -rf *.o $(EXECUTABLE)
   rm -rf *.o $(EXECUTABLE)

Revision as of 16:20, 28 October 2018

Member initialisation lists

Instead of writing a constructor like this:

Widget::Widget(const std::string &name)
{
  widgetName = name;
}

you can write this:

Widget::Widget(const std::string &name)
:widgetName(name)
{}

Sample Makefile

CC=clang++
CFLAGS=-Weverything -std=c++11 -stdlib=libc++
LDFLAGS=
SOURCES=readfile.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=readfile

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
  $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
  $(CC) $(CFLAGS) -c $< -o $@

clean:
  rm -rf *.o $(EXECUTABLE)

Links