杨庆荣的博客

接兼职私活:ecshop、zencart二次开发、discuz x2插件开发、spring boot、go、laravel、think php 5/6、uniapp。13714715608

alpine clang++ musl makefile 编译链接简单测试

点击 27461    创建时间 2022-09-28 11:52:25

add.h

int add(int a, int b);

add.cpp

int add(int a, int b){
   return a+b;
}

main.cpp

#include <stdio.h>
#include "add.h"
int main(){
  printf("the result is: %d \n",add(1,3));
  return 0;
}

Makefile


CXX_FLAGS=-fuse-ld=lld --rtlib=compiler-rt
CXX=clang++ 

.PHONY: clean

make: clean all


clean:
    rm -rf ./*.o
    rm -rf ./main

all:main


main:main.o add.o
    $(CXX) $(CXX_FLAGS)   add.o main.o    -o main
    ./main

add.o: add.cpp 
    $(CXX) $(CXX_FLAGS) -c add.cpp  

main.o: main.cpp
    $(CXX) $(CXX_FLAGS) -c main.cpp

编译

make

输出

rm -rf ./*.o
rm -rf ./main
clang++  -fuse-ld=lld --rtlib=compiler-rt -c main.cpp
clang-14: warning: argument unused during compilation: '-fuse-ld=lld' [-Wunused-command-line-argument]
clang-14: warning: argument unused during compilation: '--rtlib=compiler-rt' [-Wunused-command-line-argument]
clang++  -fuse-ld=lld --rtlib=compiler-rt -c add.cpp
clang-14: warning: argument unused during compilation: '-fuse-ld=lld' [-Wunused-command-line-argument]
clang-14: warning: argument unused during compilation: '--rtlib=compiler-rt' [-Wunused-command-line-argument]
clang++  -fuse-ld=lld --rtlib=compiler-rt   add.o main.o    -o main
./main
the result is: 4
cat make.sh >make
chmod a+x make

清理

make clean

上一篇: 很羡慕notepad++,哪天我也能开发一个?