make 随笔

# --with--cc-opt flag导致./configure时找不到对应库文件?
checking for --with-ld-opt="-Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E" ... found


checking for --with-ld-opt="-Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E" ... not found


'--with-cc-opt=-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong 
-grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables
 -fstack-clash-protection -fcf-protection 
 
 
 '--with-cc-opt=-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong 
 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables 
 -fstack-clash-protection -fcf-protection 
 -floop-unroll-and-jam  -ftree-loop-distribution  --param early-inlining-insns=160 --param inline-heuristics-hint-percent=800 --param inline-min-speedup=50
 --param inline-unit-growth=256  --param max-average-unrolled-insns=500 --param max-completely-peel-times=32 --param max-completely-peeled-insns=800
 --param max-inline-insns-auto=128 --param max-inline-insns-small=128 --param max-unroll-times=16 --param max-unrolled-insns=16'

gcc

  1. 预处理
    gcc -E x.cpp >test.ii将include代码导进来
  2. 编译
    gcc -S test.ii 生成汇编代码
  3. 汇编
    gcc -c test.s 生成.o (二进制文件)
  4. 链接
    g++ test.o -o test
  5. 运行 (运行时才会加载动态链接库)
    ./test

makefile

# first_make
# $^ 依赖 不重复
# $@ 目标
# @ 不显示命令执行过程 -失败不停止
# LD_LIBRARY_PATH=../xthread 运行的时候去哪里找链接库
TARGET=first_make
LIBS=-lpthread
#有的.o文件与要编译的文件不同目录,所以定义CXXFLAGS
CXXFLAGS=-I../test_gcc  
OBJS=first_make.cpp xdate.cpp
$(TARGET):$(OBJS)
	@#-@rm test
	@echo "begin build $(TARGET)"
	$(CXX) $^ -o $@ $(LIBS)
	@echo "a$(TARGET) build success"

clean:
	$(RM) $(OBJS) $(TARGET)
#尾部标对应的清理
.PHONY: clean *clean

-----------------
make clean
make

begin build first_make
gcc first_make.cpp xdate.cpp -o first_mak -lpthread
first_make build success

ldd xx.so

静态库 ar -crv libmylib.a mylib.o

  • 跟普通的链接一样,-o 会把库的代码复制过来
  • 动态库链接时只会把动态库当中函数地址复制过来,具体的二进制代码不会复制过来,所以链接动态库,整个文件大小会小一点

动态库

  • g++ -shared -fPIC mylib.cpp libmylib.so
  • g+= test.cpp -lmylib -L/root/cpp
  • LD_LIBRARY_PATH=./
  • export LD_LIBRARY_PATH
  • ./testjin't
posted @ 2022-02-27 20:51  绣幕  阅读(157)  评论(0编辑  收藏  举报