用于编译和运行 C++ 的 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18419973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:23:23  来源:igfitidea点击:

bash scripts to compile and run C++

c++bashmakefile

提问by user2137452

I'm trying to get into C++ but its annoying to have to run things through the command line with wordy commands so I wanted to make a bash script to simplify the process and run these commands

我正在尝试使用 C++,但是不得不使用冗长的命令通过命令行运行事情很烦人,所以我想制作一个 bash 脚本来简化过程并运行这些命令

#!/bin/bash
if [ "" == "start" ]; then
    cd CCPP
    cd HelloWorld
    g++ -Wall -W -Werror main.cpp -o HelloWorldCPP
    ./HelloWorldCPP

But I've never worked with bash before and hacked it together from someone elses code. Its not working, but I have no idea why as I don't know enough.

但是我以前从未使用过 bash 并从其他人的代码中将其破解。它不起作用,但我不知道为什么,因为我知道的还不够多。

回答by lpapp

You are missing fiat the end, but you could also write a GNU Makefile like this:

fi最后丢失了,但您也可以像这样编写 GNU Makefile:

CXX=g++
CXXFLAGS=-Wall
OBJS=main.o
EXECUTABLE=main

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJS)
        $(CXX) $(CXXFLAGS) $(OBJS) -o $@


clean:
        rm -f $(EXECUTABLE)
        rm -f $(OBJS)

main.o: main.cpp

Note, this is a flexible example, and you could simplify this even further down the line. Here you can find a very good tutorial to read upon:

请注意,这是一个灵活的示例,您可以进一步简化它。在这里你可以找到一个非常好的教程来阅读:

http://mrbook.org/tutorials/make/

http://mrbook.org/tutorials/make/

However, I would strongly suggest to learn cmake instead as writing Makefiles manually are painful.

但是,我强烈建议学习 cmake,因为手动编写 Makefile 很痛苦。

A CMakeLists.txt file could be something like this:

CMakeLists.txt 文件可能是这样的:

add_executable(main main.cpp)

then simply:

然后简单地:

mkdir -p build && cd build && cmake ../ && make VERBOSE=1

and then you would be done in a cross platform way as GNU Makefiles only work on Unices. You would need to adjust that for Windows and so forth to get it work. cmake is freely available pretty much everywhere needed, and it has proper support for native builds, cross compilation.

然后你会以跨平台的方式完成,因为 GNU Makefiles 只适用于 Unices。您需要针对 Windows 等进行调整以使其正常工作。cmake 几乎可以在任何需要的地方免费使用,并且它对本机构建、交叉编译有适当的支持。

This forum also has a cmake tag with several people around to help you.

这个论坛还有一个 cmake 标签,周围有几个人可以帮助你。

回答by usr2564301

I use mrkite's "edit-and-compile" approach as described in http://echochamber.me/viewtopic.php?f=11&t=12301

我使用 mrkite 的“编辑和编译”方法,如http://echochamber.me/viewtopic.php?f=11&t=12301 中所述

It only needs a magic incantation at the top to declare it's a bash file, then gcc is invoked with any and all flags you want and set to read from the current file. A single magic word at the end makes it stop scanning; and after that, you can immediately run the compiled code, or do anything else.

它只需要在顶部使用魔法咒语来声明它是一个 bash 文件,然后使用您想要的任何和所有标志调用 gcc 并设置为从当前文件中读取。最后一个魔法词让它停止扫描;之后,您可以立即运行编译后的代码,或执行其他任何操作。

#!/bin/sh
/usr/bin/gcc -xc -o howareyou - <<EVILEOF
#include <stdio.h>
int main()
{
? ? printf("this is so wrong\n");
}
EVILEOF
./howareyou

回答by test30

@Jongware thanks! I first saw that bitcoin.cwas using that, but version suggested by you makes use of doc-here in bash! great!

@Jongware 谢谢!我第一次看到bitcoin.c正在使用它,但是您建议的版本在 bash 中使用了 doc-here!伟大的!

I have improved it a little. Now you can compile both

我已经改进了一点。现在你可以编译两者

sh program.cpp

and

g++ program.cpp -o program && ./program

 

 

#if 0
#set -e
#process substitution
#ls . | cat EQUALS cat <(ls .)
#But this does not always work, so we have to use regular one
( cat <<COLLECTDATAMAGICKEYWORD
#line 9
#endif
#include <iostream>
#define COLLECTDATAMAGICKEYWORD
using namespace std;
int main(int argc, char const *argv[])
{
    cout<<(char*)(void)"Hello World"; //Intentionally throws warning so you can see #line 9 works! 
    return 0;
}
COLLECTDATAMAGICKEYWORD
#if 0
) |\
 sed '2d' |\
 sed '1a \ "'"$(echo "##代码##" | sed 's#\#\\\\#g')"'"' |\
 sed '1N;s#\n##' |\
 g++ -xc++ -o program3 -
./program3
#endif