创建 linux make/build 文件 [英] Create linux make/build file

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

问题描述

我正在将一个 C++ 项目从 Windows 迁移到 Linux,现在我需要创建一个构建/制作文件.我以前从未创建过构建/制作文件.我还需要包含 Boost 库以使其更复杂.它也必须是一个生成文件,无论如何我都需要学习如何创建生成文件,所以 CMake 和 SCON 被淘汰了.IDE 也因为使用 Boost 而过时了,我所有的 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++ make 文件的基础知识是什么以及如何将 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 看起来像这样.我认为 CFLAGSLDFLAGS 是编译器和优化选项,但并不完全确定.

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
  • 转换器.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 的工作原理以及如何在没有 IDE 的 Linux 中使用 Boost 构建它们.

推荐答案

什么是 Makefile ?(应用于Boost 项目)

Makefile 背后的根本递归思想是:

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

The root recursive idea behind Makefile is:

要构建目标,我们需要先决条件(其他目标!)和构建说明

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.

如果目标没有先决条件或比任何先决条件旧,则需要重建目标.

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

指令是 shell 命令,从一个选项卡开始.每个指令行是一个 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 .

目标是依赖项规则.

依赖:

target : prerequisite1 prerequisite2 prerequisiteN

规则:

target : prerequisite1 prerequisite2 prerequisiteN
    instructions1
    @hidden_batch1 ; 
  hidden_batch2  

在指令开始前有标签.

调试 Makefile 真的很头疼.尝试在 Makefile 中执行以下操作以显示跟踪(带有 warning 的文件和行位置):

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 及其包含文件已被理解(存储在 make 内部数据库中).

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

最后,使用 Boost 将理论应用于这个特定示例并创建假源文件来说明.

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"

转换器.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;
}

生成文件

如果您从 *stack***overflow** 复制 Makefile 源代码,请不要忘记用真正的制表符替换空格:

Makefile

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

sed -i~ -e 's/^    /	/' 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

结果

现在,几乎是最小的 Boost 程序的结果:

Result

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

./converter
2

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

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

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

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