I am moving a C++ project from Windows to Linux and I now need to create a build/make file. I have never created a build/make file before. I also need to include Boost libraries to make it more complicated. It must also be a makefile and I need to learn how to create makefile anyway, so CMake and SCON are out. IDEs are also out because of the use of Boost and all my IDEs (Eclipse, VS, etc.) are only on windows. I must generate a makefile from scratch.
So what are the basics of creating a Linux c++ make file and how to incorporate the Boost libraries in it to have it properly link?
So far, my makefile looks like this. I think CFLAGS and LDFLAGS are compiler and optimization options, but not totally sure.
CC = g++
CFLAGS = -wall -o3 - c
LDFLAGS = -03 -mfp-rounding-mode=n
I am offering a bounty because I am still very lost. In case anyone is feeling adventurous, I need to compile the following in linux
- simple_ls.h
- simple_ls.cpp
- 2dquicksort.h
- rawr.h
- rawr.cpp
- converter.cpp
The headers in simple_ls.h:
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/lexical_cast.hpp"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
The headers in 2dquicksort.h:
#include <stdio.h>
#include <ctype.h>
#include <iostream>
The headers in rawr.h:
#include <iostream> // not required by most systems
#include <fstream>
#include <iomanip>
#include <cstdlib> // or (stdlib.h) for exit()
#include <cmath>
#include <vector>
#include <limits>
#include <string>
An acceptable answer would be a step-by-step explanation of how makefiles work and how to build them with Boost in Linux without an IDE.
What is a Makefile ? (applied to a Boost project)
The root recursive idea behind Makefile is:
Prerequisites
They are either files, folders or fake targets (usually in
.PHONY). Files/folders are tested for existence and modification date.The target needs to be rebuilt if it has no prerequisite or if older that any of the prerequisites.
Instruction
An Instruction is shell commands, starting with one tab. Each instruction line is one shell instance. The shell command can be continued on next line when the current one ends with backslash
\.Target definition
A target is either a dependency or a rule.
Dependency:
Rule:
With tabs in front of instruction start.
Debug
Debugging a Makefile can become a real headache.
Try the following in your Makefile to show traces (with file and line location for
warning):This is helpful when your Makefile contains lots of nested
if/else/endifand you’re not sureanymore what is the current path.
Makefile Structure
The ideal makefile structure is:
The real target-instructions processing starts once the whole Makefile and its include files
has been understood (stored in
makeinternal database).Example
Finally, apply theory to this specific example using Boost and create fake source files
to illustrate.
rawr.cpp
simple_ls.cpp
converter.cpp
Makefile
Don’t forget to replace spaces with real Tabs if you copy
Makefilesource from *stack***overflow** :Makefile source:
File list
Compilation
Result
And now, the result of nearly the tiniest Boost program:
No excuse not to use it ! Boost is really a featured C++ toolbox 🙂