bash脚本来编译和运行C ++ [英] bash scripts to compile and run C++

查看:379
本文介绍了bash脚本来编译和运行C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图进入C ++,但它恼人的是必须运行的命令行与wordy命令,所以我想制作一个bash脚本简化过程和运行这些命令

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

但是我从来没有使用过bash,并且从某人的代码中删除它。

解决方案

你缺少 fi ,但你也可以这样编写一个GNU Makefile:

  = 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

注意,这是一个灵活的例子,你可以进一步简化这一点。这里你可以找到一个很好的教程:



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



但是,我强烈建议学习cmake,因为手动编写Makefiles是痛苦的。 / p>

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

  add_executable (main main.cpp)

那么简单:

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

然后你会做一个跨平台的方式作为GNU Makefiles只工作Unices。你需要调整为Windows等,以使其工作。



这个论坛也有一个cmake标签,几个人在周围帮助你。


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

#!/bin/bash
if [ "$1" == "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.

解决方案

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

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/

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

A CMakeLists.txt file could be something like this:

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.

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

这篇关于bash脚本来编译和运行C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆