Implementing shell program in C language---(1) Overall framework

Learn C language file IO operations through a small project. Developed based on the Linux platform, the Win platform does not have the lstat() function, so the ls function cannot be implemented.

Source code gitee address:

tinyShell: Implement a basic shell program

  • Implement a basic shell program that mainly completes the functions of three commands: cp, ls and tree.
    • The main implementation of the cp command is:
      • File copy
      • Directory copy
    • The ls command mainly implements:
      • ls -l command function
    • tree command implementation

Use makefile or cmake to manage projects.

Final effect: 

First write out the basic management project documents.

If you use Makefile to manage your project,

OBJS := main.o cmd_ls.o cmd_cp.o cmd_handle.o # 所有依赖目标文件
TARGET := tinyshell  # 目标文件

$(TARGET): $(OBJS)  
     @gcc   $^ -o $@  # $^ : 依赖的所有文件  $@ : 目标文件
     @echo "Done."


%.o:%.c   # 自动推导
    @gcc -c $< -o $@  # $< :依赖的第一个文件  $@ : 目标文件


clean:  # 清除目标 
    rm -rf *.o $(TARGET)  # 删除相关依赖文件

 If you use cmake to manage the project, CMakeList.txt

cmake_minimum_required(VERSION 3.25)
project(tiny_shell C)

set(CMAKE_C_STANDARD 11)

# 添加源文件
file(GLOB SOURCES "*.c")
# 添加头文件
file(GLOB HEADERS "*.h")

# 将源文件添加到可执行目标
add_executable(YourExecutable ${SOURCES} ${HEADERS})

Guess you like

Origin blog.csdn.net/qq_23172985/article/details/130694754
Recommended