繁体   English   中英

如何编写一个c ++程序,通过从shell运行来编译自己?

[英]How do I write a c++ program that will compile itself by being run from the shell?

我经常想在c ++中尝试一下,而不用去编写Makefile,创建项目或输入复杂的命令行。

我想知道是否可以创建一个也是bash脚本的.cpp文件,因此它可以编译并运行自己。

我还希望能够在脚本中指定命令行选项,以防有像boost这样的依赖项。

为什么不制作一个脚本来监视cpp文件(或它所在的dir)并重新编译cpp文件以进行任何新的更改,而不是制作一个也可以编译自身的cpp文件的脚本? inotifywaitinotifywait而做的。 虽然这并不能完全满足您的问题要求,但它会使您的代码无法承载所有脚本行李。 例:

.cpp文件

#include <iostream>

using namespace std;

int main (void) {
    cout << endl << "Code Running (ver. 10)" << endl << "and being timed." << endl;
    return 0;
}

inotifywait脚本

#!/bin/bash

# watchdir=${1:-$PWD}
src=${1:-app.cpp}   ## set the source and output (exe) names
out=${src%.cpp}

while inotifywait -q -e modify -e modify -e close_write -e delete "$PWD"; do

    printf "\n compiling 'g++ %s -o %s'\n\n" $src $out

    [ -f $out ] && rm $out      ## remove existing exe before building

    g++ -o $out $src            ## build new exe

    [ -f $out ] || {            ## validate new exe created
        printf "error: compilation failed. exiting script.\n\n"
        printf "usage:   %s source.cpp (default: app.cpp)\n\n"
        exit 1
    }

    [ -x $out ] || {            ## validate it is executable
        printf "error: file produced from compilation is not executable.\n"
        exit 1
    }

    time ./$out 2>&1                        ## compute elapsed time
    exesz=$(du -h $out | awk '{print $1}')  ## store disk usage (removing name)

    ## print results
    printf "\n Source file:  %s\n" "$src"
    printf " output file:  %s\n" "$out"
    printf " size of exe:  %s bytes\n\n" "$exesz"

done

您可以在前台或后台运行脚本。 例如:

示例(app.cpp上的输出保存)

$ ./watch.sh
/home/david/scr/tmp/stack/dat/tmp/bld/ MODIFY app.cpp

 compiling 'g++ app.cpp -o app'


Code Running (ver. 10)
and being timed.

real    0m0.003s
user    0m0.001s
sys     0m0.001s

 Source file:  app.cpp
 output file:  app
 size of exe:  16K bytes

......结果证明有可能:

/*/../bin/ls > /dev/null
 filename=$(basename $BASH_SOURCE)
 dirname=$(cd $(dirname $BASH_SOURCE) && pwd)
 if [ "${dirname}" == "$(pwd)" ]
 then
    mkdir -p ${dirname}/bin > /dev/null
    filename="${dirname}/bin/${filename}"
 else
    filename="./${filename}"
 fi
 filename=${filename%.*}
 if [ $0 -nt ${filename} ]
 then
        c++ -o "${filename}" -std=c++1y $BASH_SOURCE || exit
 fi
 ("${filename}" && exit) || echo $? && exit
 exit
*/
#include <iostream>

using namespace std;

auto main() -> int
{
        cout << "Hello, World" << endl;
        return 0;
}

在这种情况下,我将文件命名为skeleton.cpp

$ chmod +x skeleton.cpp
$ ./skeleton.cpp
Hello, World

这是关于Rosetta CodeMultiline Shebang任务的主题。 2011年9月,我写了C解决方案

#!/bin/bash
sed -n -e '7,$p' < "$0" | /usr/bin/gcc -x c -o "$0.$$.out" -
$0.$$.out "$0" "$@"
STATUS=$?
rm $0.$$.out
exit $STATUS
#include <stdio.h>

int main(int argc, char **argv)
{
  int i;
  for (i = 0; i < argc; i++)
    printf("argv[%d] -> %s\n", i, argv[i]);
  return 0;
}

这应该很容易适应C ++。 基本上只需将-xc参数更改为gcc即可。

在某些时候,我有一个版本的这个只会编译可执行文件,如果它不存在或源是更新的; 以上总是编译。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM