为静态构建编辑 Makefile

Posted

技术标签:

【中文标题】为静态构建编辑 Makefile【英文标题】:editing Makefile for static build 【发布时间】:2020-04-21 21:33:48 【问题描述】:

我正在玩这个项目:https://github.com/chfritz/step2stl

我可以构建它并且运行良好。但是,我正在查看 Makefile,我想知道如何更改它以创建一个静态构建的二进制文件,该二进制文件将在没有安装依赖项的情况下在 linux 机器上运行。

我已经读过在 clang 命令的末尾添加 -static 会起作用,但是我迷失了查看 makefile 并试图弄清楚要更改的内容...

代码如下:

UNAME := $(shell uname -a)
OCEINCLUDE := $(shell if test -d /usr/local/include/oce; then echo "/usr/local/include/oce"; else echo ""; fi)

OPENCASCADEINC?=/usr/include/opencascade
OPENCASCADELIB?=/usr/lib/opencas

$(info Using OPENCASCADEINC as "$OPENCASCADEINC")
$(info Using OPENCASCADELIB as "$OPENCASCADELIB")

OCCLIBS=-lTKBRep -lTKG2d -lTKG3d -lTKGeomBase \
-lTKMath -lTKMesh -lTKSTEP -lTKSTEP209 \
-lTKSTEPAttr -lTKSTEPBase -lTKSTL -lTKXSBase -lTKernel \

ifeq "$(OCEINCLUDE)" ""

#CXX=gcc-4.4
CXXFLAGS += -I$(OPENCASCADEINC)
LDFLAGS += -L$(OPENCASCADELIB) -L/usr/lib $OCCLIBS

else

CXXFLAGS += -I/usr/local/include/oce -I/usr/include
LDFLAGS += -L/usr/local/lib -L/usr/lib $OCCLIBS

endif

# Required to compile on gentoo systems.

ifeq (gentoo,$(findstring gentoo,$(UNAME)))
CXXFLAGS = -I/usr/lib64/opencascade-6.5/ros/lin/inc
LDFLAGS = -L/usr/lib64/opencascade-6.5/ros/lin/lib64 $OCCLIBS -lGL
endif

# note that in ubuntu oneiric you will want to use gcc-4.4, since the
# opencascade libraries don't seem wotk work with 4.6 (because they
# were probably compiled with 4.4). on ifab.parc.com i just made 4.4
# the default.

#ifeq (Ubuntu,$(findstring Ubuntu,$(UNAME)))
#UBUNTUDISTRO := $(shell lsb_release -c)
#ifeq (oneiric,$(findstring oneiric,$(UBUNTUDISTRO)))
#CXX=g++-4.9
CXX=clang++ -std=c++11 -stdlib=libc++
#endif
#endif

#---------------------------------------------------------------------
#targets
#---------------------------------------------------------------------

ifeq "$(MAKECMDGOALS)" ""
 CXXFLAGS += -ggdb3
endif
ifeq "$(MAKECMDGOALS)" "profile"
 CXXFLAGS += -pg -ggdb3
 LDFLAGS += -pg
endif

# Determine where we should output the object files.
OUTDIR = debug
ifeq "$(MAKECMDGOALS)" "debug"
 OUTDIR = debug 
 CXXFLAGS += -ggdb3
endif
ifeq "$(MAKECMDGOALS)" "release"
 OUTDIR = release
 CXXFLAGS += -O3
endif
ifeq "$(MAKECMDGOALS)" "profile"
 OUTDIR = profile
endif

# Add .d to Make's recognized suffixes.
SUFFIXES += .d

# We don't need to clean up when we're making these targets
NODEPS:=clean

#Find all the C++ files in the src/ directory
#SOURCES:=$(shell find * -name '*.cpp' | sort)
SOURCES:=$(shell ls *.cpp | sort)
OBJS:=$(patsubst %.cpp,%.o,$(SOURCES))

#These are the dependency files, which make will clean up after it creates them
DEPFILES:=$(patsubst %.cpp,%.d,$(SOURCES))

#Don't create dependencies when we're cleaning, for instance
ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))
    -include $(DEPFILES)
endif

EXE = step2stl


ifeq (Darwin,$(findstring Darwin,$(UNAME)))
    SHARED=-dynamiclib -flat_namespace
    SHAREDLIB=step2stl.so.dylib
else
    SHARED=-fPIC -shared
    SHAREDLIB=step2stl.so
endif



all:    $(EXE)

lib:
    g++ -I/usr/local/include/oce -O3 -L/usr/local/lib $(OCCLIBS) -o $(SHAREDLIB) $(SHARED) lib.cpp
    ffi-generate -f lib.hpp -l $(SHAREDLIB) -m step3stl -L /Library/Developer/CommandLineTools/usr/lib > node-ffi.js

debug:  $(EXE)

release:$(EXE)

profile:$(EXE)

$(EXE): $(OBJS)
    $(CXX) $(LDFLAGS) -o $@ $(OBJS)

#This is the rule for creating the dependency files
deps/%.d: %.cpp
    $(CXX) $(CXXFLAGS) -MM -MT '$(patsubst src/%,obj/%,$(patsubst %.cpp,%.o,$<))' $< > $@

#This rule does the compilation
obj/%.o: %.cpp %.d %.h
    @$(MKDIR) $(dir $@)
    $(CXX) $(CXXFLAGS) -o $@ -c $<

# make clean && svn update
clean:
    bash -c 'rm -f *.o $(OBJS) $(EXE)'

install:
    cp $(SHAREDLIB) /usr/local/lib
    ldconfig

【问题讨论】:

【参考方案1】:

安装GNU remake 并运行remake -X

它会让你进入调试器,然后你可以运行step 来一步一步地查看makefile 正在做什么。这是应用于您的 Makefile 的内容:

$ remake -X -f /tmp/Makefile
Reading makefiles...
Using OPENCASCADEINC as "/usr/include/opencascade"
Using OPENCASCADELIB as "/usr/lib/opencas"
ls: cannot access '*.cpp': No such file or directory
Updating makefiles...
Updating goal targets...
 File 'all' does not exist.
-> (/tmp/Makefile:117)
step2stl:
remake<0> where
=>#0  step2stl at /tmp/Makefile:117
  #1  all at /tmp/Makefile:105
remake<1> step
   File 'step2stl' does not exist.
  Must remake target 'step2stl'.
/tmp/Makefile:118: target 'step2stl' does not exist
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
clang++ -std=c++11 -stdlib=libc++ -L/usr/lib/opencas -L/usr/lib -lTKBRep -lTKG2d -lTKG3d -lTKGeomBase -lTKMath -lTKMesh -lTKSTEP -lTKSTEP209 -lTKSTEPAttr -lTKSTEPBase -lTKSTL -lTKXSBase -lTKernel  -o step2stl
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
++ (/tmp/Makefile:117)
step2stl
remake<2> help
remake: That's all, folks...

查看视频链接。或https://github.com/rocky/remake 获取一些屏幕截图

它会让你进入调试器,然后你可以运行step 来一步一步地查看makefile 正在做什么。请参阅视频链接。或https://github.com/rocky/remake 获取一些屏幕截图

【讨论】:

以上是关于为静态构建编辑 Makefile的主要内容,如果未能解决你的问题,请参考以下文章

MakefileMakefile 中 LINUXINCLUDE 的作用

MakefileMakefile 中 LINUXINCLUDE 的作用

MakefileMakefile 中 LINUXINCLUDE 的作用

MakefileMakefile 中 LINUXINCLUDE 的作用

使用静态库构建OpenCV应用程序

猫鼬CRUD应用程序:是否可以将收藏夹存储为静态文档以及参考?