创建CMake工程

转载:https://www.jianshu.com/p/53256ed46a50

create.sh脚本代码

#! /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@dell11:~# sh create.sh 
making c++ project files ...
input a project name
root@dell11:~#

输入工程名称后,运行

root@dell11:~# sh create.sh test
making c++ project files ...
inpputed project is testc
making CMakeLists.txt
making main.cpp
making run.sh
root@dell11:~#

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")

编译运行工程

root@dell11:~/test# sh buildrun.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@dell11:~/test#

完成
转载链接:https://www.jianshu.com/p/53256ed46a50

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值