#目录结构
.
├── app
├── include
│ ├── cls_add.h
│ ├── cls_div.h
│ ├── cls_mul.h
│ └── cls_sub.h
├── lib
├── Makefile
├── obj
│ ├── cls_add.o
│ ├── cls_div.o
│ ├── cls_mul.o
│ ├── cls_sub.o
│ └── main.o
└── src
├── cls_add.c
├── cls_div.c
├── cls_mul.c
├── cls_sub.c
└── main.c
#makefile文件1
source = ./src
include = ./include
objc = ./obj
src = $(wildcard $(source)/*.c)
obj = $(patsubst $(source)/%.c, $(objc)/%.o, $(src))
target = app
myArgs = -Wall -g
CC = gcc
ALL:$(target)
# 链接
$(target):$(obj)
$(CC) $^ -o $@ $(myArgs)
# 预处理 编译 汇编
$(obj):$(objc)/%.o:$(source)/%.c
$(CC) -c $< -o $@ $(myArgs) -I $(include)
clean:
-rm -rf $(obj) $(target)
# 伪目标
.PHONY: clean ALL
# makefile文件2
# 定义变量
source = src
include = include
objc = obj
# 查找所有源文件和目标文件
src = $(wildcard $(source)/*.c)
obj = $(patsubst $(source)/%.c, $(objc)/%.o, $(src))
# 定义目标和编译选项
target = app
myArgs = -Wall -g
CC = gcc
# 默认目标
ALL:$(target)
# 链接目标文件生成可执行文件
$(target):$(obj)
$(CC) $^ -o $@ $(myArgs)
# 编译规则
$(obj):$(objc)/%.o:$(source)/%.c
$(CC) -c $< -o $@ $(myArgs) -I $(include)
# 清理规则
clean:
-rm -rf $(obj) $(target)
# 伪目标
.PHONY: clean all
#cmakelists.txt
.
├── CMakeLists.txt
├── include
│ ├── cls_add.h
│ ├── cls_div.h
│ ├── cls_mul.h
│ └── cls_sub.h
├── lib
├── obj
└── src
├── cls_add.c
├── cls_div.c
├── cls_mul.c
├── cls_sub.c
└── main.c
4 directories, 10 files
# CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(app_test)
# 指定源文件目录 GLOB 搜索指定目录 GLOB_RECURSE 递归搜索指定目录
file(GLOB SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
# aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRC)
# 指定头文件目录
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
# 指定库文件生成的路径
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib)
# add_executable(app ${SRC})
add_library(calc STATIC ${SRC})
add_library(calc_s SHARED ${SRC})
#[[
usage:
mkdir build
cd build
cmake ..
make
]]
makefile cmake
于 2024-07-26 22:12:42 首次发布