C++项目自动生成脚本


ccnew

$vim ccnew
#!/bin/bash
# version v0.0.1
if [ -n "$1" ]; then
    mkdir -p "$1"/{include,lib,src,tools}
    cat << EOF > "$1"/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project($1)
set(EXECUTABLE_OUTPUT_PATH \${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH \${PROJECT_SOURCE_DIR}/lib)
aux_source_directory(src SRC_LIST)
aux_source_directory(tools TOOLS_LIST)
include_directories(include)
link_directories(lib)
#add_library(tools SHARED \${TOOLS_LIST})
#link_libraries(tools)
#target_link_libraries(tools)
add_executable($1 \${SRC_LIST})
EOF

    cat << EOF > "$1"/.gitignore
cmake-build-debug/
.vscode/
.vs/
.cache/
.idea/
build/
bin/
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
*.ko
*.elf
# Precompiled Headers
*.gch
*.pch
# Linker output
*.ilk
*.map
*.exp
# Libraries
*.lib
*.a
*.la
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
# Cmake
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
EOF

    cat << EOF > "$1"/src/main.cpp 
#include <iostream>
using namespace std;

int main(int argv, char *argc[]) {
  std::cout <<"Hello CPP"<< std::endl;
  return 0;
}
EOF

else
    echo "ccnew ProjectName"
fi
$chmod +x ccnew
$sudo mv ccnew   /usr/local/bin
$ccnew  demo1
$tree -a 
.
└── demo1
    ├── CMakeLists.txt
    ├── .gitignore
    ├── include
    ├── lib
    ├── src
       └── main.cpp
    └── tools

$cd demo1
$cmake -B build -G '\''Unix Makefiles'\'' && cmake --build build
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.4s)
-- Generating done (0.0s)
-- Build files have been written to: /home/zuojie/Code/CLL/CPP/NET/test/demo1/build
[ 50%] Building CXX object CMakeFiles/demo1.dir/src/main.cpp.o
[100%] Linking CXX executable /home/zuojie/Code/CLL/CPP/NET/test/demo1/bin/demo1
[100%] Built target demo1

$./bin/demo1
Hello CPP