Sie sind auf Seite 1von 4

g++ with Linux

Every C++ program you turn in for this class must run correctly under two compilers:
Visual C++ 2012 and either clang++ or g++. This requirement helps you detect errors
in two ways:
Different compilers might look for different questionable things to warn you
about. For example, if instead of the character literal '\n' you mistakenly
write '/n', Visual C++ says nothing, while g++ warns you about a multi-
character constant.
Different compilers might produce code that behaves differently if your
program does something with undefined behavior, such as using the value of an
uninitialized variable or accessing an element outside the bounds of an array. If
running your program under two different compilers produces different
behavior, there's a very good chance your program is doing something
undefined, which we prohibit.
This writeup is directed toward people who will be developing primarily using Visual
C++. If you're doing the bulk of your development using Xcode on a Mac, you're
already using clang++, so this writeup is not of critical importance to you. If you're
already a Linux developer, you probably already know what to do.
What we're presenting here is a minimal set of instructions for helping a Windows
user transfer a C++ program to a SEASnet Linux server to build and run. There are
other ways to do this that 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++ project consisting of the source file from
the Visual C++ 2012 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 a SEASnet Linux server:
1. Copy your C++ source file to the Windows desktop on a SEASnet machine.
Make sure you copy the file; don't just create a shortcut linking to it. Note: A
few people each quarter find that step 6 below produces the error message cp:
cannot stat 'Desktop/hello.cpp': No such file or directory. If that
happens to you, return to this step and copy your source file to Z:\Desktop or
to \\labsamba2.seas.ucla.edu\yourUserName\Desktop instead of just dragging
the file's icon to the desktop.
2. From your Windows desktop on a SEASnet machine, double click the icon
labelled "putty" (if you're in a SEASnet lab) or "SSH LnxSrv" (if you're
connecting remotely to the SEASnet Windows server). In the former case,
type lnxsrv for the Host Name in the dialog box. If a dialog box titled "PuTTY
Security Alert" appears, click its Yes button.
3. In the PuTTY console window (titled "lnxsrv.seas.ucla.edu - PuTTY"), log in
to the Linux server with your SEASnet login name. When prompted for your
password, type it in; the characters you type for your password will not produce
anything on the screen, not even bullets.
4. You will now be interacting with the command interpreter program (the shell),
whose default prompt is something like bash-4.1$ or [yourname@lnxsrv01 ~]$.
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.
About two or three people per quarter discover that a mistake was made when
their SEASnet account was set up. To verify you're not one of them, run the
following command to list the contents of your desktop:
ls ~/Desktop
If it produces the output ls: cannot access Desktop: No such file or
directory, or if it does not show the name of the zip file you put on your
desktop (e.g., VM.zip), then either you did not follow these instructions
correctly or you are one of the rare people whose SEASnet account setup
wasn't done quite right. In the latter case, contact the SEASnet Help Desk in
Boelter 2684 or at help@seas.ucla.edu; don't do so in the former case, or you'll
be wasting their time with your foolishness.
5. If you have never previously run the following command, run it now; you will
never have to run it again. If you do run it again, it will confirm that your setup
for running g++ on the SEASnet Linux server is correct.
6. curl -s http://cs.ucla.edu/classes/fall13/cs31/Utilities/setupg++ |
bash
7. Copy your program source file (let's say it's named hello.cpp) into the current
directory (denoted by a dot):
8. cp Desktop/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.
9. Verify that the file is present by listing the contents of the current directory:
10. ls
11. Build an executable file from the source files. If we would like the executable
file to be named hello, we'd say
12. g++ -o hello hello.cpp
(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++11 -Wall -Wextra -
fmudflap -lmudflap -Wl,--rpath=/usr/local/cs/lib64 -o hello
hello.cpp instead. The/usr/local/cs/bin/g++ invokes g++ version 4.8.1; the
SEASnet server's default is version 4.4.7. The -std=c++11 enables C++11
language features. The -Wall option asks the compiler to warn you about many
questionable constructs; the -Wextra asks for warnings about even more
questionable constructs. 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++, you
should make changes to your original Windows files. After checking in Visual
C++ that the modified program works as you expect it to, go back to step 1.
13. To execute the program hello that you built, you'd just say
14. hello
(If that doesn't work, try
./hello
instead.)
15. To exit the shell, say
16. exit
If you want to examine a file under Linux, a simple text editor you can use is Pico.
pico 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 just test your program under Linux once only
after you're satisfied with it under Visual C++. 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 g++ compiler would have immediately given them a warning that
pointed to the mistake. Also, sometimes a program that appeared to work correctly
under Visual C++ 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 under
g++ might have shown different behavior, which would have suggested to the student
that something was amiss.

Das könnte Ihnen auch gefallen