目录
一、多文件项目源代码
// include/add.hpp
#pragma once
int add(int a, int b);
// include/sub.hpp
#pragma once
int sub(int a, int b);
// src/add.cpp
#include "add.hpp"
int add(int a, int b) {
return a + b;
}
// src/sub.cpp
#include "sub.hpp"
int sub(int a, int b) {
return a - b;
}
// src/main.cpp
#include <iostream>
#include "add.hpp"
#include "sub.hpp"
int main() {
std::cout << "10 + 5 = " << add(10, 5) << std::endl;
std::cout << "10 - 5 = " << sub(10, 5) << std::endl;
return 0;
}
二、静态库编译链接
# Makefile文件
lib_srcs := $(filter-out src/main.cpp,$(shell find src -name *.cpp))
lib_objs := $(patsubst src/%.cpp,obj/%.o, $(lib_srcs))
include_paths := $(shell pwd)/include/
library_paths := $(shell pwd)/lib/
library_names := mymath
I_options := $(include_paths:%=-I%)
l_options := $(library_names:%=-l%)
L_options := $(library_paths:%=-L%)
compile_flags := $(I_options) -w -O3 -std=c++11
linking_flags := $(l_options) $(L_options)
# ================ 编译静态库 ===============
obj/%.o : src/%.cpp
g++ -c $^ -o $@ $(compile_flags)
lib/libmymath.a : $(lib_objs)
@mkdir -p $(dir $@)
ar -r $@ $^
static_lib : lib/libmymath.a
# ================ 链接静态库 ===============
obj/main.o : src/main.cpp
@mkdir -p $(dir $@)
g++ -c $^ -o $@ $(compile_flags)
workspace/math.exe : obj/main.o
@mkdir -p $(dir $@)
g++ $^ -o $@ $(linking_flags)
# ================ 运行与测试 ===============
run : workspace/math.exe
@./$<
test :
@echo "lib_srcs: " $(lib_srcs)
@echo "lib_objs: " $(lib_objs)
@echo "include_paths: " $(include_paths)
@echo "compile_opts: " $(compile_opts)
clean:
rm -rf obj/* workspace
.PHONY: test clean run
# 静态库编译链接过程
(base) [root@localhost 08_test]# tree .
.
├── include
│ ├── add.hpp
│ └── sub.hpp
├── makefile
├── obj
└── src
├── add.cpp
├── main.cpp
└── sub.cpp
3 directories, 6 files
(base) [root@localhost 08_test]# make static_lib
g++ -c src/add.cpp -o obj/add.o -I/root/gitee/Test/Make_Learn/08_test/include/ -w -O3 -std=c++11
g++ -c src/sub.cpp -o obj/sub.o -I/root/gitee/Test/Make_Learn/08_test/include/ -w -O3 -std=c++11
ar -r lib/libmymath.a obj/add.o obj/sub.o
ar: 正在创建 lib/libmymath.a
(base) [root@localhost 08_test]# tree .
.
├── include
│ ├── add.hpp
│ └── sub.hpp
├── lib
│ └── libmymath.a
├── makefile
├── obj
│ ├── add.o
│ └── sub.o
└── src
├── add.cpp
├── main.cpp
└── sub.cpp
4 directories, 9 files
(base) [root@localhost 08_test]# make run
g++ -c src/main.cpp -o obj/main.o -I/root/gitee/Test/Make_Learn/08_test/include/ -w -O3 -std=c++11
g++ obj/main.o -o workspace/math.exe -lmymath -L/root/gitee/Test/Make_Learn/08_test/lib/
10 + 5 = 15
10 - 5 = 5
(base) [root@localhost 08_test]# tree .
.
├── include
│ ├── add.hpp
│ └── sub.hpp
├── lib
│ └── libmymath.a
├── makefile
├── obj
│ ├── add.o
│ ├── main.o
│ └── sub.o
├── src
│ ├── add.cpp
│ ├── main.cpp
│ └── sub.cpp
└── workspace
└── math.exe
5 directories, 11 files
(base) [root@localhost 08_test]#
三、动态库编译链接
# Makefile文件
lib_srcs := $(filter-out src/main.cpp,$(shell find src -name *.cpp))
lib_objs := $(patsubst src/%.cpp,obj/%.o, $(lib_srcs))
include_paths := $(shell pwd)/include/
library_paths := $(shell pwd)/lib/
library_names := mymath
I_options := $(include_paths:%=-I%)
l_options := $(library_names:%=-l%)
L_options := $(library_paths:%=-L%)
compile_flags := $(I_options) -w -O3 -std=c++11 -fpic
linking_flags := $(l_options) $(L_options)
# ================ 编译动态库 ===============
obj/%.o : src/%.cpp
g++ -c $^ -o $@ $(compile_flags)
lib/libmymath.so : $(lib_objs)
@mkdir -p $(dir $@)
g++ -shared $^ -o $@
shared_lib : lib/libmymath.so
# ================ 链接动态库 ===============
obj/main.o : src/main.cpp
@mkdir -p $(dir $@)
g++ -c $^ -o $@ $(compile_flags)
workspace/math.exe : obj/main.o
@mkdir -p $(dir $@)
ln -s $(library_paths)/libmymath.so /lib64/libmymath.so
g++ $^ -o $@ $(linking_flags)
# ================ 运行与测试 ===============
run : workspace/math.exe
@./$<
test :
@echo "lib_srcs: " $(lib_srcs)
@echo "lib_objs: " $(lib_objs)
@echo "include_paths: " $(include_paths)
@echo "compile_opts: " $(compile_opts)
clean:
rm -rf obj/* workspace lib /lib64/libmymath.so
.PHONY: test clean run
# 动态库编译链接过程
(base) [root@localhost 09_test]# tree .
.
├── include
│ ├── add.hpp
│ └── sub.hpp
├── makefile
├── obj
└── src
├── add.cpp
├── main.cpp
└── sub.cpp
3 directories, 6 files
(base) [root@localhost 09_test]# make shared_lib
g++ -c src/add.cpp -o obj/add.o -I/root/gitee/Test/Make_Learn/09_test/include/ -w -O3 -std=c++11 -fpic
g++ -c src/sub.cpp -o obj/sub.o -I/root/gitee/Test/Make_Learn/09_test/include/ -w -O3 -std=c++11 -fpic
g++ -shared obj/add.o obj/sub.o -o lib/libmymath.so
(base) [root@localhost 09_test]# make run
g++ -c src/main.cpp -o obj/main.o -I/root/gitee/Test/Make_Learn/09_test/include/ -w -O3 -std=c++11 -fpic
ln -s /root/gitee/Test/Make_Learn/09_test/lib//libmymath.so /lib64/libmymath.so
g++ obj/main.o -o workspace/math.exe -lmymath -L/root/gitee/Test/Make_Learn/09_test/lib/
10 + 5 = 15
10 - 5 = 5
(base) [root@localhost 09_test]# tree .
.
├── include
│ ├── add.hpp
│ └── sub.hpp
├── lib
│ └── libmymath.so
├── makefile
├── obj
│ ├── add.o
│ ├── main.o
│ └── sub.o
├── src
│ ├── add.cpp
│ ├── main.cpp
│ └── sub.cpp
└── workspace
└── math.exe
5 directories, 11 files
(base) [root@localhost 09_test]#