如何编写bash文件以编译和执行C ++程序

时间:2018-11-14 06:05:58

标签: bash

我的项目实际上已经完成,现在我需要做的是编写一个bash脚本,该脚本可以编译并执行该程序。我的指示是,我们应该提供一个可以编译程序的脚本,并且教师应该能够通过在Project1 Value1 Value2中键入内容来运行该程序。我有点迷失了,所以任何帮助将不胜感激。

我试图通过运行此脚本来编译程序

#!/bin/bash
echo $1
echo $2
g++ -std=c++11 Project2.cpp -lpthread -o project1
./project1 $1 $2

编辑

#!/bin/bash
g++ -std=c++11 Project2.cpp -lpthread -o project1

我考虑的更多了,我认为我的第二个版本更接近我希望脚本执行的工作。除非我希望能够在不使用./的情况下执行程序(如果可能)。

2 个答案:

答案 0 :(得分:0)

一段时间后,我终于弄清楚了。

#!/bin/bash
echo "Enter TP"
read TP
echo "Enter TC"
read TC
g++ -std=c++11 Project_1.cpp -lpthread -o Project_1
./Project_1 $TP $TC

答案 1 :(得分:0)

您花了几个小时努力将某些内容放在一起,但是如果编译失败会怎样?如果没有参数会怎样?如果程序有4个参数,会发生什么?

编写脚本或程序时,最重要的事情是 验证每个必要的步骤! 。如果您期望输入,请验证是否已收到。输入后,请验证它是否符合您的期望。它是在可用范围内的值吗?它是符合我要求的文件名吗?是否存在?我的编译成功还是失败?我会尝试执行程序吗?

所有这些都需要解决。否则,您的脚本(或程序)将偏离一些未定义的路径。

Bash提供了很多条件表达式,可与test(或[...])一起使用,或与bash [[...]]运算符一起使用。参见Bash Manual - 6.4 Bash Conditional Expressions。利用语言功能使代码更健壮。

Bash通过参数扩展 Bash Manual - 3.5.3 Shell Parameter Expansion提供了丰富的字符串处理功能,使用它们检查扩展名以确保其为c, cpp or c++

将这些功能与一些if...then...fi语句一起放置将使您的脚本更加可靠-并且实际上可以执行您要尝试的操作。

以下简短脚本采用参数(bash Positional Parameters ),要求至少提供要编译的文件名,并将任何其他参数作为命令行参数传递给{{1} }(所有bash位置参数均从第二个开始)

${@:2}

现在有几个简短的示例来确保它能正常工作:

#!/bin/bash

if [ -z "$1" ]      ## validate at least one argument given
then
    printf "error: insufficient arguments\n" >&2
    printf "usage: %s file.cpp [args]\n" "${0##*/}" >&2
    exit 1
fi

if [ ! -r "$1" ]    ## validate file is readable
then
    printf "error: file not found '%s'\n" "$1" >&2
    exit 1
fi

src="$1"    ## give source file a handy name

## check the source file ends in '.c', '.cpp' or '.c++'
if [ "${src##*.}" != 'c' -a "${src##*.}" != 'cpp' -a "${src##*.}" != 'c++' ]
then
    printf "error: first argument not a c/c++ file\n" >&2
    exit 1
fi

ext="${src##*.}"        ## save the extension

if [ "${#ext}" = 'c' ]  ## check if ext is 'c' use gcc else use g++
then
    ## always enable compiler warnings, -Wall -Wextra -pedantic, minimum
    #  -Wshadow to catch shadowed variables, -Werror treat warnings as error
    gcc -Wall -Wextra -pedantic -Wshadow -Werror \
        -std=c11 -O3 -o "${src%.*}" "$src"
else
    g++ -Wall -Wextra -pedantic -Wshadow -Werror \
        -std=c++11 -O3 -o "${src%.*}" "$src"
fi

if [ $? -eq '0' ]       ## check the compiler return, run only on success
then
    ./"${src%.*}" ${@:2}
else
    printf "\nAn error occurred, executable not called\n\n" >&2
fi

使用/输出示例

#include <stdio.h>

int main (int argc, char **argv) {

    const char *s = "hello c file.";

    printf ("%s\n", s);

    for (int i = 1; i < argc; i++)
        printf ("arg[%d]: %s\n", i, argv[i]);
}

对于C ++

$ bash compilewargs.sh cfile.c foo bar baz
hello c file.
arg[1]: foo
arg[2]: bar
arg[3]: baz

使用/输出示例

#include <iostream>
#include <string>

int main (int argc, char **argv) {

    std::string s = "hello cpp file.";

    std::cout << s << '\n';

    for (int i = 1; i < argc; i++)
        std::cout << "arg[" << i << "]: " << argv[i] << '\n';
}

错误处理有效吗?

$ bash compilewargs.sh cppfile.cpp foo bar baz
hello cpp file.
arg[1]: foo
arg[2]: bar
arg[3]: baz

使用/输出示例

#include <stdio.h>

int main (void) {

    const char *s = "hello c file.";

    printf ("%s\n", unknown);
}

那不存在的文件呢?

$ bash compilewargs.sh cerrfile.c foo bar baz
cerrfile.c: In function ‘int main()’:
cerrfile.c:7:21: error: ‘unknown’ was not declared in this scope
     printf ("%s\n", unknown);
                     ^
cerrfile.c:5:17: error: unused variable ‘s’ [-Werror=unused-variable]
     const char *s = "hello c file.";
                 ^
cc1plus: all warnings being treated as errors

An error occurred, executable not called

关于文本(或任何其他非$ bash compilewargs.sh myfile.c foo bar baz error: file not found 'myfile.c' )文件呢?

c, cpp or c++

花些时间思考一下,尽管您认识的最笨的用户可以尝试使用脚本做些什么-并加以保护,但您可以编写相当健壮的脚本来省去痛苦。仔细研究一下,如果您还有其他问题,请告诉我。

相关问题