Optional programming module
writing C++ on the cluster
Some students are having issues installing an IDE on their computers. It is possible to go through the module using the compiler on the cluster. Mark Holder recommends:
mkdir programming cd programming nano program1.cpp # to write the code g++ -Wall program1.cpp -o myprogram && ./mypgrogram
as a starting-to-program method. You can edit the files with any text editor (not just nano).
Every time that you save changes, you'll need to recompile and then run the program. the g++ -Wall program1.cpp -o myprogram tells the compiler (g++) to compile with all warning notifications turned on (that is the -Wall part). It needs to know the source code files. So you can type all of their names (e.g. program1.cpp here), or use a syntax like *.cpp to compile all of the .cpp files in your directory. You can only compile a group of files like that if there is exactly 1 main function in the entire set of files. The -o option tells the compiler that the next word on the command-line should be the name of the executable.
When I am editing using a command-line tool, I tend to open one terminal window for editing. In a second terminal window (also logged into the cluster with the same working directory), I run the:
g++ -Wall *.cpp -o myprogram && ./mypgrogram
command. Then whenever I save in the editing terminal, I don't exit my editor. I just change focus to other terminal, and using up arrow to rerun the compilation command.
The && ./myprogram syntax tells bash to run the compiled executable ./myprogram, but only if the previous command (the compilation in this case) succeeds.