Deniz Cetinalp
Deniz Cetinalp

Reputation: 911

Creating a Makefile that includes a header only library

This is my first c++ program, and also my first time creating a Makefile so im very unfamiliar with it all.

I have my main file raytracer.cpp that includes the glm library like this:

#include "glm/glm.hpp"

raytracer.cpp and the glm directory are in the the same directory.

How can I create a makefile for this project?

The glm library is a header only library. So do I have to compile all the files in the glm directory? Or just the glm.hpp?

Upvotes: 1

Views: 3206

Answers (2)

Etan Reisner
Etan Reisner

Reputation: 81052

If you have a single raytracer.cpp file that is your application and some (local) header files the only makefile contents you need (and this is only to get your binary rebuilt if the headers change) is.

raytracer: raytracer.hpp glm/glm.hpp

That's it.

Just create the Makefile and run make raytracer. (You can run that without creating Makefile also but make won't know to rebuild your binary if only the header changes.)

If you need any special flags/etc. you can set those with the built-in make variables. Like CFLAGS, LDFLAGS, LDLIBS, CPPFLAGS, etc.

See 10.3 Variables Used by Implicit Rules in the manual for the list and more information about them.

The accepted answer on the ticket that midor linked to contains a lot of useful information as well.

Upvotes: 1

midor
midor

Reputation: 5557

IF, and only if, all the code is in the header and nothing is defined in a corresponding source, you don't have to specify anything in the Makefile, but the rule for your raytracer.cpp. The header code will automatically be inserted where the include statement is. You only have to specify it if you need other objects or shared objects (libraries as in .so .a etc.) to be included)

For more information how to create simple makefiles see here:How to make SIMPLE C++ Makefile?

Upvotes: 2

Related Questions