临时想要运行一个c/c++代码,大学的时候就是直接打开VC,创建一个工程。但现在经常用Linux,因此想要有一个工具自动创建一个c/c++工程,用过CLion,确实不错,但要收费,所以想着能不能在Linux下用一个脚本自动创建一个cmake工程。
代码更新github:https://github.com/junbenjun/hello_word_jun
脚本代码:
#! /bin/bash
echo 'making c++ project files ...'
project=default
if [ ! $1 ]; then
echo 'input a project name'
exit
else
echo 'inpputed project is' $1
project=$1
fi
rm -rf ./$project
mkdir ./$project
mkdir ./$project/src
mkdir ./$project/src/include
mkdir ./$project/build
echo 'making CMakeLists.txt'
echo 'set(PROJECT CProject_main)' >> ./$project/CMakeLists.txt
echo 'message("Making ${PROJECT} ...")' >> ./$project/CMakeLists.txt
echo 'cmake_minimum_required(VERSION 3.16)' >> ./$project/CMakeLists.txt
echo 'project(${PROJECT})' >> ./$project/CMakeLists.txt
echo 'aux_source_directory(./src SRC_FILE)' >> ./$project/CMakeLists.txt
echo 'add_executable(run ${SRC_FILE})' >> ./$project/CMakeLists.txt
echo 'message("Make ${PROJECT} ok, please execute run")' >> ./$project/CMakeLists.txt
echo 'making main.cpp'
echo '#include <iostream>' >> ./$project/src/main.cpp
echo '' >> ./$project/src/main.cpp
echo 'int main()' >> ./$project/src/main.cpp
echo '{' >> ./$project/src/main.cpp
echo ' std::cout << "Hello world" << std::endl;' >> ./$project/src/main.cpp
echo ' return 0;' >> ./$project/src/main.cpp
echo '}' >> ./$project/src/main.cpp
echo 'making rebuild_run.sh'
echo 'rm -rf ./build/*' >> ./$project/rebuild_run.sh
echo 'cd ./build' >> ./$project/rebuild_run.sh
echo 'cmake ../' >> ./$project/rebuild_run.sh
echo 'make' >> ./$project/rebuild_run.sh
echo './run' >> ./$project/rebuild_run.sh
echo 'making buildrun.sh'
echo 'cd ./build' >> ./$project/buildrun.sh
echo 'make' >> ./$project/buildrun.sh
echo './run' >> ./$project/buildrun.sh
运行
- 在Linux下执行脚本
root@592a3d1e06e8:~# sh create.sh
making c++ project files ...
input a project name
root@592a3d1e06e8:~#
- 输入工程名称后,再运行
root@592a3d1e06e8:~# sh create.sh testc
making c++ project files ...
inpputed project is testc
making CMakeLists.txt
making main.cpp
making run.sh
root@592a3d1e06e8:~#
- 工程文件
root@592a3d1e06e8:~/testc# ll
total 28
drwxr-xr-x 4 root root 4096 Jan 2 12:25 ./
drwx------ 1 root root 4096 Jan 2 12:25 ../
-rw-r--r-- 1 root root 236 Jan 2 12:25 CMakeLists.txt
drwxr-xr-x 2 root root 4096 Jan 2 12:25 build/
-rw-r--r-- 1 root root 41 Jan 2 12:25 run.sh
drwxr-xr-x 3 root root 4096 Jan 2 12:25 src/
root@592a3d1e06e8:~/testc#
root@592a3d1e06e8:~/testc/src# ll
total 16
drwxr-xr-x 3 root root 4096 Jan 2 12:25 ./
drwxr-xr-x 4 root root 4096 Jan 2 12:25 ../
drwxr-xr-x 2 root root 4096 Jan 2 12:25 include/
-rw-r--r-- 1 root root 95 Jan 2 12:25 main.cpp
root@592a3d1e06e8:~/testc/src#
- CMakeLists.txt代码
root@592a3d1e06e8:~/test# vi CMakeLists.txt
set(PROJECT CProject_main)
message("Making ${PROJECT} ...")
cmake_minimum_required(VERSION 3.16)
project(${PROJECT})
aux_source_directory(./src SRC_FILE)
add_executable(run ${SRC_FILE})
message("Make ${PROJECT} ok, please execute run")
- main.cpp代码
#include <iostream>
int main()
{
std::cout << "Hello world" << std::endl;
return 0;
}
- 编译运行工程
root@592a3d1e06e8:~/testc# sh run.sh
Making CProject_main ...
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
Make CProject_main ok, please execute run
-- Configuring done
-- Generating done
-- Build files have been written to: /root/testc/build
Scanning dependencies of target run
[ 50%] Building CXX object CMakeFiles/run.dir/src/main.cpp.o
[100%] Linking CXX executable run
[100%] Built target run
Hello world
root@592a3d1e06e8:~/testc#
默认工程就创建出来了