创建Linux的化妆/ build文件 [英] Create linux make/build file

查看:236
本文介绍了创建Linux的化妆/ build文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移动从Windows到Linux的一个C ++项目,我现在需要创建一个构建/制作文件。我从来没有创建了一个构建/前作文件。我还需要包括Boost库,使之更加复杂化。 这还必须是一个生成文件,我需要学习如何创建反正makefile文件,所以cmake并SCON都出来了。 IDE是也,因为使用升压和我所有的IDE(Eclipse中,VS等)都只能在Windows上。我必须从头开始生成一个makefile。

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.

那么,什么是创建一个Linux的C ++使文件,以及如何纳入Boost库中它有它正确链接?的基本知识

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?

到目前为止,我的makefile文件看起来是这样的。我觉得 CFLAGS LDFLAGS 的编译器和优化选项,但不能完全确定。

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


因为我还是很失落我提供赏金。如果任何人冒险的感觉,我需要编译在linux下面


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

在simple_ls.h的头文件:

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>

在2dquicksort.h的头文件:

The headers in 2dquicksort.h:

#include <stdio.h>
#include <ctype.h>
#include <iostream>

在rawr.h的头文件:

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>

一个可以接受的答案将是makefile文件是如何工作的,以及如何将它们与在Linux中Boost构建没有一个IDE一步一步的解释。

推荐答案

递归背后的Makefile的想法是:

What is a Makefile ? (applied to a Boost project)

The root recursive idea behind Makefile is:

要建设,我们需要一个目标的 prerequisites (其他目标!)和说明,以建立

To build a target we need prerequisites (other targets!) and instructions to build

他们是文件,文件夹或假目标(通常在 .PHONY )。文件/文件夹的生存和修改日期进行测试。

Prerequisites

They are either files, folders or fake targets (usually in .PHONY). Files/folders are tested for existence and modification date.

目标需要它有没有prerequisite,不得重建,如果旧的,任何的prerequisites的。

The target needs to be rebuilt if it has no prerequisite or if older that any of the prerequisites.

指令被 shell命令,从一个标签。每个指令行是一个壳实例。 Shell命令可以在下一行继续在当前与反斜杠结尾 \\

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 \.

一个目标或者是一个依赖规则

A target is either a dependency or a rule.

相关性:

target : prerequisite1 prerequisite2 prerequisiteN

规则:

target : prerequisite1 prerequisite2 prerequisiteN
    instructions1
    @hidden_batch1 ; \
  hidden_batch2  

随着指令开始前的标签。

With tabs in front of instruction start.

调试一个Makefile可以成为一个真正的头痛。
试试下面的Makefile中显示的痕迹(有文件和行位置警告

Debugging a Makefile can become a real headache. Try the following in your Makefile to show traces (with file and line location for warning):

$(info Shell: $(SHELL))
$(warning CXX: $(CXX))

这是有益的,当你的Makefile中含有大量的嵌套的的if / else / ENDIF 键,你不知道
了什么是当前的路径。

This is helpful when your Makefile contains lots of nested if/else/endif and you're not sure anymore what is the current path.

理想的makefile结构是:

The ideal makefile structure is:


  1. 变量设置

  2. 目标/依赖声明

真正的目标的指令处理开始一次全Makefile文件及其包含文件
已经知道(存储在制作内部数据库)。

The real target-instructions processing starts once the whole Makefile and its include files has been understood (stored in make internal database).

最后,使用升压应用理论于这个具体的例子,制造假的源文件
来说明。

Finally, apply theory to this specific example using Boost and create fake source files to illustrate.

#include "rawr.h"

simple_ls.cpp

#include "rawr.h"

converter.cpp

#include <iostream>

#include "rawr.h"
#include "simple_ls.h"
#include "2dquicksort.h"

#include <boost/array.hpp>   // Boost! 

int main(int argc, char **argv)
{
    boost::array<int,4> a = { { 1, 2, 3, 4} };
    std::cout << a[1] << std::endl;
    return 0;
}

的Makefile

不要忘了,如果你从复制的Makefile 源与真正的制表符代替空格的

Makefile

Don't forget to replace spaces with real Tabs if you copy Makefile source from stackoverflow :

sed -i~ -e 's/^    /\t/' Makefile

Makefile中源:

Makefile source:

## Makefile for C++ project using Boost
#
# @author Cedric "levif" Le Dillau
#
# Some notes:
# - Using ':=' instead of '=' assign the value at Makefile parsing time,
#   others are evaluated at usage time. This discards
# - Use ':set list' in Vi/Vim to show tabs (Ctrl-v-i force tab insertion)
#

# List to '.PHONY' all fake targets, those that are neither files nor folders.
# "all" and "clean" are good candidates.
.PHONY: all, clean

# Define the final program name
PROGNAME := converter

# Pre-processor flags to be used for includes (-I) and defines (-D) 
CPPFLAGS := -DUSE_BOOST

# CFLAGS is used for C compilation options.
CFLAGS := -Wall -O0

# CXXFLAGS is used for C++ compilation options.
CXXFLAGS += -Wall -O0

# LDFLAGS is used for linker (-g enables debug symbols)
LDFLAGS  += -g

# Which Boost modules to use (all)
BOOST_MODULES = \
  date_time     \
  filesystem    \
  graph         \
  iostreams     \
  math_c99      \
  system        \
  serialization \
  regex

# Boost libraries' type (a suffix)
BOOST_MODULES_TYPE := -mt

# Define library names with their type
BOOST_MODULES_LIBS := $(addsuffix $(BOOT_MODULES_TYPE),$(BOOST_MODULES))

# Define the linker argument to use the Boost libraries.
BOOST_LDFLAGS := $(addprefix -lboost_,$(BOOST_MODULES_LIBS))

# Feed compiler/linker flags with Boost's
CPPFLAGS += $(BOOST_CPPFLAGS)
LDFLAGS += $(BOOST_LDFLAGS)

# List the project' sources to compile or let the Makefile recognize
# them for you using 'wildcard' function.
#
#SOURCES = simple_ls.cpp rawr.cpp converter.cpp
SOURCES = $(wildcard *.cpp)

# List the project' headers or let the Makefile recognize
# them for you using 'wildcard' function.
#
#HEADERS = simple_ls.h 2dquicksort.h rawr.h
HEADERS = $(wildcard %.h)

# Construct the list of object files based on source files using
# simple extension substitution.
OBJECTS = $(SOURCES:%.cpp=%.o)

#
# Now declare the dependencies rules and targets
#
# Starting with 'all' make it  becomes the default target when none 
# is specified on 'make' command line.
all : $(PROGNAME)

# Declare that the final program depends on all objects and the Makfile
$(PROGNAME) : $(OBJECTS) Makefile
    $(CXX) -o $@ $(LDFLAGS) $(OBJECTS)

# Now the choice of using implicit rules or not (my choice)...
#
# Choice 1: use implicit rules and then we only need to add some dependencies
#           to each object.
#
## Tells make that each object file depends on all headers and this Makefile.
#$(OBJECTS) : $(HEADERS) Makefile
#
# Choice 2: don't use implicit rules and specify our will
%.o: %.cpp $(HEADERS) Makefile
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(OUTPUT_OPTION) $<

# Simple clean-up target
# notes:
# - the '@' before 'echo' informs make to hide command invocation.
# - the '-' before 'rm' command to informs make to ignore errors.
clean :
    @echo "Clean."
    -rm -f *.o $(PROGNAME)

文件列表

2dquicksort.h
converter.cpp
Makefile
rawr.cpp
rawr.h
simple_ls.cpp
simple_ls.h

编辑

make clean all
Clean.
rm -f *.o converter
g++ -Wall -O0 -DUSE_BOOST  -c -o converter.o converter.cpp
g++ -Wall -O0 -DUSE_BOOST  -c -o rawr.o rawr.cpp
g++ -Wall -O0 -DUSE_BOOST  -c -o simple_ls.o simple_ls.cpp
g++ -o converter -g -lboost_date_time -lboost_filesystem -lboost_graph -lboost_iostreams -lboost_math_c99 -lboost_system -lboost_serialization -lboost_regex converter.o rawr.o simple_ls.o

结果

而现在,几乎最小的加速程序的结果是:

Result

And now, the result of nearly the tiniest Boost program:

./converter
2

没有理由不使用它! 升压 确实是一个有特色的C ++工具箱:)

No excuse not to use it ! Boost is really a featured C++ toolbox :)

这篇关于创建Linux的化妆/ build文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆