g++ with Linux

Every program for which you submit a source file must run successfully on the SEASnet Linux server cs31.seas.ucla.edu using the g31 command we have set up, which is merely a special way of running g++ that catches more kinds of errors during execution.

What we're presenting here is a minimal set of instructions for helping a Windows or Mac user transfer a C++ program to that SEASnet Linux server to build and run. There are other ways to do this than what we show; the more you know about Linux, the more you can simplify or eliminate certain steps.

Let's assume you have a Visual C++ or an Xcode project consisting of the source file from the Visual C++ writeup or the Xcode on a Mac writeup: hello.cpp. (Note: It is inconvenient in Linux if the name of a file contains space characters, so avoid giving your files such names.) Here's what to do to run the program on the SEASnet cs31 Linux server:

  1. First, note that you will be able to log into that SEASnet Linux server only from

  2. Windows users: Install PuTTY on your own computer; a SEASnet writeup tells you how.

  3. Copy your C++ source file to the SEASnet Linux server.

  4. Log in to the SEASnet cs31 Linux server. Here's how:

    You will now be interacting on the remote Linux server with the command interpreter program (the shell), whose default prompt is something like bash-5.2$ or [yourname@lnxsrv16 ~]$. For each command you type, the shell will execute it and then prompt you for the next command. The shell is case-sensitive, so pay attention to the distinction between lower and upper case.

    Windows users who used https://file.io: To complete the file transfer, run the command

    	curl -s -L https://file.io/XXXXXX > hello.cpp
    

    where XXXXXX are the characters at the end of the link you were told in response to your upload.

  5. Our special way of running g++ is via a new command, g31, which you will have to set up. If you have never previously run the following command, run it now on the SEASnet Linux server; you will never have to run it again. If you do run it again, it will confirm that you are set up for running g31 on the SEASnet Linux server.

    	setupg31
    
  6. Verify that the expected file is present by listing the contents of the current directory:

    	ls
    
  7. Build an executable file from the source files. If we would like the executable file to be named hello, we'd say

    	g31 -o hello hello.cpp
    

    Like many Linux commands, if this command works, it doesn't say anything, so the shell would then just print its next prompt.

    (You don't have to know this, but the setup process in step 5 modified the file ~/.profile to create a bash function that causes the above command to be executed as if it were /usr/local/cs/bin/g++ -std=c++17 -Wall -Wextra -Wno-sign-compare -Werror=return-type -Wl,--rpath=/usr/local/cs/lib64 -fsanitize=address -fsanitize=undefined -fsanitize=bounds -fno-omit-frame-pointer -o hello hello.cpp instead. The /usr/local/cs/bin/g++ invokes g++ version 14.2.0; the SEASnet server's default is version 11.5.0. The -std=c++17 enables C++17 language features. The -Wall option asks the compiler to warn you about many questionable constructs; the -Wextra -Wno-sign-compare asks for warnings about even more questionable constructs. The -Werror=return-type causes a certain warning to be treated as an error. The other added options cause certain runtime errors to terminate your program with an error message instead of silently continuing and wreaking havoc.)

    Compiler diagnostic messages are of the form

    	fileName:lineNumber:columnNumber:message
    

    If the compiler detects any problems that you want to fix, then since we're assuming you're doing your primary development using Visual C++ or Xcode, you should make changes to your original Windows or Mac file. After checking in Visual C++ or Xcode that the modified program works as you expect it to, go back to step 1.

  8. To execute the program hello that you built, you'd just say

    	hello
    

    (If that doesn't work, try

    	./hello
    

    instead.)

  9. To exit the shell, say

    	exit
    

If you want to examine a file under Linux, a simple text editor you can use is Nano.

	nano hello.cpp

You can navigate with the arrow keys. The bottom two lines of the display show you some commands you can type. For example, control-C (indicated in the bottom display as ^C) shows you what line number the cursor is on. Control-O saves any changes you make to the file, and control-X exits the editor.

We strongly recommend that you don't test your program using g31 just once, only after you're satisfied with it under Visual C++ or Xcode. Instead, do it periodically during your development, and certainly do it when you're trying to find an elusive bug. Sometimes people have spent hours trying to track down a problem during execution using Visual C++ when the g31 command would have immediately given them a warning that pointed to the mistake. Also, sometimes a program that appeared to work correctly under Visual C++ or Xcode relied on undefined behavior, but happened to get lucky for the student in a way that wasn't reproduced when we tested the program; testing using g31 might have shown different behavior, which would have suggested to the student that something was amiss.