AndroidDev
AndroidDev

Reputation: 21237

Makefile for simple C++ OpenGL program

I'm trying to write a makefile for an OpenGL program written in C++ (OSX).

Right now, there is only the single file chess.cpp, but I expect to add other files to the project, so I'm trying to create a makefile that I can expand to handle new files as needed. I'm getting these errors:

clang: error: no such file or directory: 'chess.o'

clang: warning: -framework GLUT: 'linker' input unused

clang: warning: -framework OpenGL: 'linker' input unused

make: *** [chess.o] Error 1

This is the makefile that I created. It's borrowed from something that I normally use for C programs, so if it looks strange, that could be why. How can I make this work for my C++ project?

CC = g++
CFLAGS = -c -g -Wall -Wextra
DEPS =
LDFLAGS = -framework GLUT -framework OpenGL

all: chess

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

chess.o: chess.cpp
        $(CC) -c chess.cpp chess.o $(LDFLAGS)

chess: chess.o
        $(CC) -o chess.o (LDFLAGS)

clean:
        rm chess
        rm *.o

Upvotes: 0

Views: 4978

Answers (1)

Felix Glas
Felix Glas

Reputation: 15524

There's a couple of errors:

First:

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

This compiles all .cpp files and everything in $(DEPS) into their respective .o files.

This makes the following line redundant: (although this line appends the linker flags, which the previous does not)

chess.o: chess.cpp
    $(CC) -c chess.cpp chess.o $(LDFLAGS)

Even so, the line has an error. It is missing the output file option -o. The correct syntax is:

chess.o: chess.cpp
    $(CC) -c chess.cpp -o chess.o $(LDFLAGS)

And finally:

chess: chess.o
    $(CC) -o chess.o (LDFLAGS)

This line is missing an input argument. Make doesn't know what to compile. Also you are using the dependency file name as output argument. The filename directly after option -o specifies the output. Also a $ is missing at (LDFLAGS). The correct syntax should read:

chess: chess.o
    $(CC) chess.o -o chess $(LDFLAGS)

Upvotes: 2

Related Questions