makefile for cpp

cpp在编译的时候将cpp后缀为一个单位进行编译(可看下面带头文件的编译):self.ws.run_forever(sslopt={“cert_reqs”: ssl.CERT_NONE})

1、以cpp后缀为单位,将每个cpp文件编译成obj文件,即.o文件。首先把当前cpp文件需要的头文件全都复制粘贴过来,然后判断函数逻辑,注意这里只判断每个被调用的函数是否有声明,不判断当前声明是否有对应的定义。

2、将第一步中编译的.o文件链接起来。这里就要对第一步的.o文件中的每个声明去寻找定义,若没有对应的定义或重复定义,则会报错。也就是链接头文件和源文件的过程(确切的说是链接声明和定义的过程)

1、首先把当前cpp文件需要的头文件全都复制粘贴过来
2、判断函数逻辑,注意这里只判断每个被调用的函数是否有声明,不判断当前声明是否有对应的定义。
3、

subst、patsubst

在这里插入图片描述

foreach:

S

dir: 创建文件

在这里插入图片描述

常用通用变量

在这里插入图片描述

notdir、filter、bassename

在这里插入图片描述

编译过程



cpp_srcs := $(shell find src -name *.cpp)
cpp_objs    := $(patsubst src/%.cpp,objs/%.o,$(cpp_srcs))

objs/%.o : src/%.cpp
	mkdir -p $(dir $@)
	g++ -c $< -o $@

workspace/exec : ${cpp_objs}
	mkdir -p ${dir $@}
	g++ $^ -o $@

run : workspace/exec
	./$<

objects : $(cpp_objs)

debug:
	@echo $(cpp_objs)
	
clean:
	@rm -rf src/*.i src/*.s objs



.PHONY : debug preprocess clean

运行结果:

[duyunfei@localhost test]$ make run
mkdir -p objs/
g++ -c src/add.cpp -o objs/add.o
mkdir -p objs/
g++ -c src/minus.cpp -o objs/minus.o
mkdir -p objs/
g++ -c src/main.cpp -o objs/main.o
mkdir -p workspace/
g++ objs/add.o objs/minus.o objs/main.o -o workspace/exec
./workspace/exec
a + b = 15
a - b = 5
[duyunfei@localhost test]$

编译选项

在这里插入图片描述

带头文件的编译



cpp_srcs := $(shell find src -name *.cpp)
cpp_objs := $(patsubst src/%.cpp,objs/%.o,${cpp_srcs})

include_path := /home/duyunfei/test/include

I_flags := $(include_path:%=-I%)

compile_options := -g -O3 -w $(I_flags)

objs/%.o : src/%.cpp
	mkdir -p $(dir $@)
	g++ -c $< -o $@ $(compile_options)

workspace/exec : $(cpp_objs)
	mkdir -p $(dir $@)
	g++ $^ -o $@

run : workspace/exec
	./$<

debug:
	@echo ${cpp_srcs}
	@echo ${cpp_objs}
	@echo ${I_flags}
	@echo $(compile_options)

clean:
	rm -rf objs workspace/exec

.PHONY : run debug clean
[duyunfei@localhost test]$ make run
mkdir -p objs/
g++ -c src/add.cpp -o objs/add.o -g -O3 -w -I/home/duyunfei/Test/test/include
mkdir -p objs/
g++ -c src/minus.cpp -o objs/minus.o -g -O3 -w -I/home/duyunfei/Test/test/include
mkdir -p objs/
g++ -c src/main.cpp -o objs/main.o -g -O3 -w -I/home/duyunfei/Test/test/include
mkdir -p workspace/
g++ objs/add.o objs/minus.o objs/main.o -o workspace/exec
./workspace/exec
a + b = 15
a - b = 5

生成静态库(.a)

1、编译静态库文件



lib_srcs := $(filter-out src/main.cpp,$(shell find src -name *.cpp))
lib_objs := $(patsubst src/%.cpp,objs/%.o,$(lib_srcs))

include_paths := ./include

I_options := $(include_paths:%=-I%)

compile_flags := -g -O3 -std=c++11 $(I_options)

objs/%.o : src/%.cpp
	mkdir -p $(dir $@)
	g++ -c $^ -o $@ $(compile_flags)

lib/libxxx.a : $(lib_objs)
	mkdir -p $(dir $@)
	ar -r $@ $^

static_lib : lib/libxxx.a

clean :
	rm -rf objs lib  

debug :
	@echo $(lib_srcs)
	@echo $(lib_objs)

.PHONY : debug static_lib clean


[duyunfei@localhost compile_static_lib]$ make static_lib
mkdir -p objs/
g++ -c src/add.cpp -o objs/add.o -g -O3 -std=c++11 -I./include
mkdir -p objs/
g++ -c src/minus.cpp -o objs/minus.o -g -O3 -std=c++11 -I./include
mkdir -p lib/
ar -r lib/libxxx.a objs/add.o objs/minus.o
ar: 正在创建 lib/libxxx.a
2、运行静态库



lib_srcs := $(filter-out src/main.cpp,$(shell find src -name *.cpp))
lib_objs := $(patsubst src/%.cpp,objs/%.o,$(lib_srcs))

include_paths := ./include

library_paths := ./lib

linking_libs := xxx

I_options := $(include_paths:%=-I%)
l_options := $(linking_libs:%=-l%)
L_options := $(library_paths:%=-L%)

compile_flags := -g -O3 -std=c++11 $(I_options)
linking_flags := $(l_options) $(L_options)

# ======================= 编译静态库文件
objs/%.o : src/%.cpp
	mkdir -p $(dir $@)
	g++ -c $^ -o $@ $(compile_flags)

lib/libxxx.a : $(lib_objs)
	mkdir -p $(dir $@)
	ar -r $@ $^

static_lib : lib/libxxx.a


# ======================= 链接静态库文件
objs/main.o : src/main.cpp
	mkdir -p $(dir $@)
	g++ -c $< -o $@ $(compile_flags)

workspace/exec : objs/main.o
	mkdir -p $(dir $@)
	g++ $< -o $@ $(linking_flags)

run : workspace/exec
	./$<

clean :
	rm -rf objs lib  

debug :
	@echo $(lib_srcs)
	@echo $(lib_objs)

.PHONY : debug static_lib clean


./workspace/exec
a + b = 15
a - b = 5

动态库编译与调用



cpp_srcs := $(shell find src -name *.cpp)
cpp_objs := $(patsubst src/%.cpp,objs/%.o,$(cpp_srcs))
so_objs := $(filter-out objs/main.o,$(cpp_objs))

include_dirs := /home/duyunfei/Test/compile_dynamic_lib/include
library_dirs := /home/duyunfei/Test/compile_dynamic_lib/lib

linking_libs := ddd

I_options := $(include_dirs:%=-I%)
l_options := $(linking_libs:%=-l%)
L_options := $(library_dirs:%=-L%)
r_options := $(library_dirs:%=-Wl,-rpath=%)
linking_options := $(l_options) $(L_options) $(r_options)

compile_options := -g -O3 -w -fpic


objs/%.o : src/%.cpp
	mkdir -p $(dir $@)
	g++ -c $^ -o $@ $(compile_options) $(I_options)

compile : $(cpp_objs)


lib/libddd.so : $(so_objs)
	mkdir -p $(dir $@)
	g++ -shared $^ -o $@

dynamic : lib/libddd.so

workspace/exec : objs/main.o
	mkdir -p $(dir $@)
	g++ $< -o $@ $(linking_options)

run : lib/libddd.so workspace/exec 
	./workspace/exec


clean :
	rm -rf objs lib workspace

debug:
	@echo $(cpp_srcs)
	@echo $(so_objs)

.PHONY : debug clean compile
[duyunfei@localhost compile_dynamic_lib]$ make run
mkdir -p objs/
g++ -c src/add.cpp -o objs/add.o -g -O3 -w -fpic -I/home/duyunfei/Test/compile_dynamic_lib/include
mkdir -p objs/
g++ -c src/minus.cpp -o objs/minus.o -g -O3 -w -fpic -I/home/duyunfei/Test/compile_dynamic_lib/include
mkdir -p lib/
g++ -shared objs/add.o objs/minus.o -o lib/libddd.so
mkdir -p objs/
g++ -c src/main.cpp -o objs/main.o -g -O3 -w -fpic -I/home/duyunfei/Test/compile_dynamic_lib/include
mkdir -p workspace/
g++ objs/main.o -o workspace/exec -lddd -L/home/duyunfei/Test/compile_dynamic_lib/lib -Wl,-rpath=/home/duyunfei/Test/compile_dynamic_lib/lib
./workspace/exec
a + b = 15
a - b = 5
[duyunfei@localhost compile_dynamic_lib]$ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
############################################################################# # Makefile for building: sample 2011-09-26 # # Project: # Template: # Command: # ------基本上简单用法的makefile------- #1. 第一个目标为最终目标 #2. 命令以 Tab开头,可以有多个命令 #3. 分行号\ 后面不可以跟空格 #4、加@可以去掉命令显示 #5. 变量为 abc = efd 访问为 $(abc) echo $abc # # # #缺点,单文件夹 #每次都会重新生成 # #foo.o : foo.c defs.h # foo模块 #cc -c -g foo.c # #多目录 一种方法,在主目录里面include "",然后其里面OBJS += .o,这样其实就是 或用foreach ############################################################################# #target EXECUTABLE := test CC := gcc CXX := g++ STRIP := strip AR := ar cqs LINK := g++ RM := rm -f CFLAGS := -g -Wall CXXFLAGS := $(CFLAGS) CXXFLAGS += -MD LIBS := -lm LIBPATH := -L/usr/local/lib INCPATH := ####### Output directory OBJSPATH := ../Obj/ EXECUTABLEPATH := ../Execute/ #######source Files SOURCE := $(wildcard *.c) $(wildcard *.cpp) OBJS := $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(SOURCE))) DEPS := $(patsubst %.o,%.d,$(OBJS)) #######rule .SUFFIXES: .cpp .c .o .so .a .d $(OBJSPATH)%.o:%.c $(CC) $(CFLAGS) -c $< -o $@ $(OBJSPATH)%.o:%.cpp $(CXX) $(CXXFLAGS) -c $< -o $@ $(OBJSPATH)%.d:%.cpp $(CXX) -MM $ $@ ######main .PHONY : all deps objs clean rebuild all: $(EXECUTABLE) $(CXX) $(CXXFLAGS) $(INCLUDEPATH) $(LIBS) $(LIBPATH) $(addprefix $(OBJSPATH),$(OBJS)) \ -o $(EXECUTABLEPATH)$(EXECUTABLE) deps: $(addprefix $(OBJSPATH),$(DEPS)) objs: $(addprefix $(OBJSPATH),$(OBJS)) clean: @$(RM) $(OBJSPATH)*.o @$(RM) $(OBJSPATH)*.d @$(RM) $(EXECUTABLEPATH)$(EXECUTABLE) rebuild: clean all -include $(addprefix $(OBJSPATH),$(DEPS)) ##.d里面是详细的.o rule 自己会括展开的,然后没有文件就自己去重建 $(EXECUTABLE) : objs
========= Cppcheck ========= About The original name of this program is "C++check" but it was later changed to "cppcheck". Manual A manual is available online: http://cppcheck.sourceforge.net/manual.pdf Compiling Any C++11 compiler should work. For compilers with partial C++11 support it may work. If your compiler has the C++11 features that are available in Visual Studio 2010 then it will work. If nullptr is not supported by your compiler then this can be emulated using the header lib/cxx11emu.h. To build the GUI, you need Qt. When building the command line tool, PCRE is optional. It is used if you build with rules. There are multiple compilation choices: * qmake - cross platform build tool * cmake - cross platform build tool * Windows: Visual Studio * Windows: Qt Creator + mingw * gnu make * g++ 4.6 (or later) * clang++ qmake ===== You can use the gui/gui.pro file to build the GUI. cd gui qmake make Visual Studio ============= Use the cppcheck.sln file. The file is configured for Visual Studio 2013, but the platform toolset can be changed easily to older or newer versions. The solution contains platform targets for both x86 and x64. To compile with rules, select "Release-PCRE" or "Debug-PCRE" configuration. pcre.lib (pcre64.lib for x64 builds) and pcre.h are expected to be in /extlibs then. Qt Creator + mingw ================== The PCRE dll is needed to build the CLI. It can be downloaded here: http://software-download.name/pcre-library-windows/ gnu make ======== Simple build (no dependencies): make The recommended release build is: make SRCDIR=build CFGDIR=cfg HAVE_RULES=yes Flags: SRCDIR=build : Python is used to optimise cppcheck CFGDIR=cfg : Specify folder where .cfg files are found HAVE_RULES=yes : Enable rules (pcre is required if this is used) g++ (for experts) ================= If you just want to build Cppcheck without dependencies then you can use this command: g++ -o cppcheck -std=c++0x -include lib/cxx11emu.h -Iexternals/tinyxml -Ilib cli/*.cpp lib/*.cpp externals/tinyxml/*.cpp If you want to use --rule and --rule-file then dependencies are needed: g++ -o cppcheck -std=c++0x -include lib/cxx11emu.h -lpcre -DHAVE_RULES -Ilib -Iexternals/tinyxml cli/*.cpp lib/*.cpp externals/tinyxml/*.cpp mingw ===== The "LDFLAGS=-lshlwapi" is needed when building with mingw mingw32-make LDFLAGS=-lshlwapi other compilers/ide =================== 1. Create a empty project file / makefile. 2. Add all cpp files in the cppcheck cli and lib folders to the project file / makefile. 3. Compile. Cross compiling Win32 (CLI) version of Cppcheck in Linux sudo apt-get install mingw32 make CXX=i586-mingw32msvc-g++ LDFLAGS="-lshlwapi" mv cppcheck cppcheck.exe Webpage http://cppcheck.sourceforge.net/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值