Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

c++ - Create linux make/build file

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A very simple GNU makefile follows:

CPPFLAGS += -Isome_include_path
CXXFLAGS += -O3
LDFLAGS  += -Lsome_link_path -lsome_lib -lboost_filesystem

all: binary_name
binary_name: foo.o bar.o john.o
  • CPPFLAGS are flags that are applied to the C Preprocessor. Things like include paths.
  • CXXFLAGS are flags that are applied to the C++ compiler. Things like optimization levels.
  • LDFLAGS are flags that applied to the linker. Things like external libraries (boost_filesystem) and other libraries. Also, the paths to those libraries.
  • Convention says that there should be a make all rule, which is the default. In make, the first rule is the default.
  • binary_name is the name of your binary.
  • binary_name depends on 3 files: foo.o, bar.o, john.o.
  • We don't include rules for *.o because gnu make has an implicit rule for those.

To use make, you would create a file named Makefile with the contents listed above. To build, you would run make in that directory.

As an aside (and as others have mentioned), I would recommend moving away from make if possible. There are better systems out there. The main benefit of make is that it's everywhere. However, if management requires it, then management requires it.

(Note that the GNU Make += notation is not always portable to other versions of Make. However, make on Linux is GNU Make.)


Given your edit, here's an example with the files that you note. The one caution is that the line beginning with $(CXX) should begin with a TAB character!

LDFLAGS  := -lboost_filesystem
CXXFLAGS := -O3 -Wall
CPPFLAGS :=

all: program
program: simple_ls.o converter.o rawr.o
    $(CXX) -o $< $^ $(LDFLAGS)

simple_ls.o: simple_ls.cpp rawr.h simple_ls.h 2dquicksort.h
converter.o: converter.cpp rawr.h simple_ls.h 2dquicksort.h
rawr.o: rawr.cpp rawr.h simple_ls.h 2dquicksort.h

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...