3

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.

8
  • 8
    Have you considered using a makefile? Commented Aug 24, 2013 at 15:08
  • 3
    Use an IDE, it does that kind of stuff automatically Commented Aug 24, 2013 at 15:09
  • 2
    Also, the script is correct. What error are you getting? Commented Aug 24, 2013 at 15:10
  • This is the error /home/adam/CCPP/HelloWorld/crunner.sh: line 9: syntax error: unexpected end of file Commented Aug 24, 2013 at 15:14
  • 6
    fi is missing at the end Commented Aug 24, 2013 at 15:16

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

4

I use mrkite's "edit-and-compile" approach as described in 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.

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

Comments

1

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

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 "${0}" | sed 's#\\#\\\\\\\\#g')"'"' |\
 sed '1N;s#\n##' |\
 g++ -xc++ -o program3 -
./program3
#endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.