C/C++教程

写了一个无脑创建简单cmake项目的脚本

本文主要是介绍写了一个无脑创建简单cmake项目的脚本,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

我之前写了几个脚本,用于cmake构建项目,清除,运行,但是CMakeLists.txt却是每次都要搬过去改一下名字,所以就把几个脚本集成成一个bascp.sh

#!/bin/bash
#BASCP:build a simple C project,项目如其名
if [ ! -f CMakeLists.txt ];then
read -p "请输入项目名称> " pname
echo "cmake_minimum_required(VERSION 3.10)" >CMakeLists.txt
echo "project($pname)" >>CMakeLists.txt
echo "include_directories(\${CMAKE_SOURCE_DIR}/include)" >>CMakeLists.txt
echo "aux_source_directory(\${CMAKE_SOURCE_DIR}/src SRC)" >>CMakeLists.txt
echo "add_executable(main \${SRC})" >>CMakeLists.txt
echo "set(EXECUTABLE_OUTPUT_PATH \${CMAKE_SOURCE_DIR}/bin)" >>CMakeLists.txt
echo "CMakeLists.txt创建完成"
fi

if [ ! -d include ];then
mkdir -p include
echo "include目录创建完成。"
fi

if [ ! -d src ];then
mkdir -p src
echo "src目录创建完成。"
fi

if [ ! -f build.sh ];then #创建构建脚本
echo "#!/bin/bash" >build.sh
echo "if [ ! -d build ];then" >>build.sh
echo "mkdir -p build" >>build.sh
echo "fi" >>build.sh
echo "cd build" >>build.sh
echo "cmake .." >>build.sh
echo "make" >>build.sh
chmod 755 build.sh
echo "build.sh构建脚本创建完成"
fi

if [ ! -f run.sh ];then #创建运行脚本
echo "#!/bin/bash" >run.sh
echo "if [ -f bin/main ];then" >>run.sh
echo "bin/main" >>run.sh
echo "fi" >>run.sh
chmod 755 run.sh
echo "run.sh运行脚本创建完成"
fi

if [ ! -f clean.sh ];then #创建清除脚本
echo "#!/bin/bash" >clean.sh
echo "if [  -f build/Makefile ];then" >>clean.sh
echo "cd build" >>clean.sh
echo "make clean" >>clean.sh
echo "rm -f CMakeCache.txt" >>clean.sh
echo "echo \"build目录清理完成。\"" >>clean.sh
echo "fi" >>clean.sh
chmod 755 clean.sh
echo "clean.sh清除脚本创建完成"
fi

if [ ! -f src/main.c ];then
cd src
echo "#include<stdio.h>" >main.c
echo "int main(){" >>main.c
echo "	printf(\"Hello World\n\");" >>main.c
echo "	return 0;" >>main.c
echo "}" >>main.c
echo "main.c源文件创建完成"
fi

创建一个项目目录,把bascp.sh拖进去,执行,输入项目名就会自动生成一个简单的cmake项目,然后执行build.sh就可以构建一个helloworld程序,run.sh就可以运行,如果把项目移动了或改名了,就执行clean.sh

这篇关于写了一个无脑创建简单cmake项目的脚本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!