About your compiler

Colin Peters colin@bird.fu.is.saga-u.ac.jp
Tue Aug 5 21:17:00 GMT 1997


Note to mailing list: I only cc'd this to the list because I am not sure about
some of my comments, and because I would like to hear any feedback anyone
wishes to make about my "newbie guide" appendix included at the end of this
mail. I apologize for the bandwidth usage.

Christine Behrens[SMTP:yadda@msn.com] wrote:
>	Hi, my name is Tony Philippsen.  I have recently decided to delve into the 
>world of computer programming.  I have need of a C++ compiler for an online 
>course in this language, and I was given your name as a source for a compiler. 
> Unfortunately, my knowledge in this area is nothing less than pathetic, and I 
>am completely confused by your download page (not your fault).  The course 
>requires a compiler that can "write to standard output", "it must support 
>templates", and "must support exceptions".  I was wondering if yours met all 
>of these criteria.  I read your "Read Me" file, and could not discern if your 
>compiler met these requirements because I have no idea what they mean.  I 
>would appreciate some help in answering these questions if you have the time.  
>Also, if this compiler ( I don't even know what a compiler really is!) meets 
>these requirements, I sure could use any help you would be willing to give me 
>regarding downloading the proper files to my computer.  As I said, I was able 
>to access your download area, but have no idea which files I need to insure a 
>working compiler is installed at my end.  

Hi there,

First of all, note that the mailing list you sent to is read by both the
Cygnus developers and a large number of active users. I'm a user, so I am
not the final authority on anything at all. However, I will try to give
you some hints based on my own knowledge.

Tip number one is that you have your work cut out for you. The GNU compiler
is not a model of user-friendliness by any stretch of the imagination. However,
I believe it is worth learning how to use, so just try to be patient and
persistent.

About your needs (remember, I am NOT an authority. I have the strong feeling
that someone will flame me for my lack of knowledge on points two and three.):

 - Must be able to write to standard output.

   I know of no C/C++ compiler on Earth that cannot do that. No problem.

 - Support templates.

   GCC supports templates. In fact it was one of the first compilers to
   do so. There are, however, still some odd bits in the implementation
   which may make it tricky to use. Still, it's probably no worse than
   any other C++ compiler, and you can always ask the mailing list or
   GCC newsgroups for help. Reading the documentation might be helpful
   as well.

 - Supports exceptions.

   GCC was also one of the first compilers to support exceptions. However,
   I am not sure myself exactly how to use them under GCC, and I don't
   know if they are entirely stable. You can always hope.

Now, downloading the compiler itself:

By following links or using an FTP program you should be able to get to
the ...gnu-win32/gnu-win32-b18/ directory on Cygnus' FTP server or one
of the mirrors. There is a list of mirrors on the Cygwin32 Project page
and on my homepage ( http://www.geocities.com/Tokyo/Towers/6162/gcc.html ).

In that directory you will find a file README.txt, which you should
download, since it contains instructions for setting up the compiler,
among other things. Then you can either download the extremely large
"cdk.exe" file from that directory, or download all the files in the
directory "cdk-split". Remember to download these files in BINARY
mode. They should be the exact same size on your hard drive as shown
in the directory listing in your browser.

If you downloaded the files from cdk-split do the following at a DOS
prompt (i.e. console window on Win NT or 95):

 copy /b  xaa + xab + xac + ... + xag + xah cdk.exe

(replace the ... with the other filenames in alphabetical order).

Then, if that worked, you can delete xa*.*.

Now run cdk.exe, that will start an installation program which should
uncompress the compiler and copy it to the directory you want. After
that you still have to edit your autoexec.bat (or system environment
variables on NT) to set environment variables and the PATH for your
system so that the compiler will work. See the README.txt file for
details.

Test out the compiler by compiling a simple program like "hello world".
Use your favorite text editor (not a word processor, unless you make
sure you save in ASCII text; notepad works fine, but vim or ntemacs
is better) to create a file with these contents:

#include <stdio.h>

int main ()
{
  printf("Hello, world.\n");
}

And save it as "hello.c", then, in the directory where you saved it
(again using a DOS-console) run the following command:

gcc -o hello.exe hello.c

This should produce a file hello.exe and when you type hello you should
get the line:

Hello, world.

as your output. If it didn't work there is some problem with your
configuration. Check whether you set up all the environment variables
correctly (you must reboot for changes to take effect on Windows 95)
and if you can't figure it out note the exact error messages and mail
me or the mailing list.

The remainder of this document is an appendix to the README for my own
extensions to GNU-Win32, the Minimalist GNU-Win32, and it describes some
of the options and other programs you will need to use to make your own
programs. One more thing though: if you are compiling C++ you might want
to use "g++" instead of "gcc" (I think the command line options are
basically the same) because this automatically includes some extra
libraries which your online course probably assumes are being included
(specifically "iostreams").

Good luck,
Colin.


Appendix A: A Newbie's Guide to Using GNU-Win32

This is just a little section I threw together because I keep getting messages like "I just started learning C/C++ and I can't figure out how to use the compiler." Hopefully this will help cut down on those a bit. There is complete, if somewhat outdated, documentation for the GNU compiler and tools (especially of interest are the "binary utilities") both online at the Cygnus Technical Library and included with the cdk.exe download (unfortunately in man file format). This section is just an attempt to get you started.

A.1 Definitions

First of all here are the definitions of a few terms you need to understand in order to get anything out of the rest of the discussion.

Source code (or "source") - Text written, generally, by a human being containing one or more statements, definitions, expressions or functions written in a programming language, such as C or C++. Source code also refers to a text file containing such text. You can usually tell what language a source file is by the file name. If it ends in (i.e. it's "extension" is) ".c" then it is C language code, if the extension is ".cpp" then it is C++.

Object code - Series of instructions and/or data in a form which could be executed by a computer, but not necessarily a complete program. Object code is stored in files that end with ".o" (or sometimes ".obj", but that is not the GNU convention). Usually one creates a single object file to match each source file by compiling the source files (see below). Object files are not complete programs because they can contain calls to functions or data not defined in the object file and they may have no single "starting point" where the program could begin.

Compiler - A program (or set of programs) that converts source code into object code, generally creating one object file for each source file input into it. Gcc is usually called the GNU "compiler" but actually it is a program that figures out which of several compilers to call to actually compile your source (there are separate C and C++ compilers) and also calls the linker (see below) to link the executable.

Executable - Another word for a program or a file that contains a program. In the Win32 world an executable file ends with the extension ".exe".

Linker - A program that takes a set of object files and builds a complete program from them by resolving all the undefined references in one object file to where the data or function is actually defined in other object files which are part of the program. The GNU linker is "ld", but generally you run it indirectly using gcc.

Archive or Library - A collection of object files put together as a single file for easy access. Usually a library has a file name like "libxxx.a" where xxx is the 'real' name of the library. For example, the basic Mingw32 library is libmingw32.a, which is automatically linked to all Mingw32 programs.


A.2 Compiling

The simplest way to make an executable is to invoke gcc like this:

	gcc -o foo.exe foo.c

This command builds the executable foo.exe from the source file foo.c. The "-o" option tells gcc that the next item on the command line is the name of the new file to create (or [o]utput). If you don't use this option then your executable will end up with a name like "a.out".

Gcc decides which compiler to use based on the name of the files supplied to it. Foo.c is compiled using the C compiler, if the file was named foo.cpp or foo.cc then it would be compiled with the C++ compiler. (Actually some people use a capitol 'C' for C++ and lower case 'c' for C, but that is not a good idea on Win32 systems for obvious reasons, and I personally don't think it's a good idea under UNIX either.)

That's fine, but generally you will want to compile more complex programs with several source files. You could just add all the source file names to gcc, but that would mean every time you changed one file you would have to re-compile all of them. So usually you will compile object files and link them later. To compile an object file use a command line like this:

	gcc -c -o foo.o foo.c

The -c option tells gcc to [c]ompile only. This will create an object file foo.o from the C language source file foo.c.


A.3 Linking

Now you have a bunch of object files and you want to make an executable. You can do this using a command line like this:

	gcc -o foo.exe foo.o bar.o foobar.o

Gcc knows that .o files are object files, and sees that there are no source files in the input, so it simply calls the linker to build the executable foo.exe from the object files foo.o bar.o and foobar.o. If all goes well you should be able to execute foo.exe simply by typing "foo" at the command prompt, or by double clicking on the foo program's icon in Explorer.


A.4 Libraries

For large projects it is convenient to put together sets of related utility functions and data in libraries. You also will want to use libraries supplied by other companies or programmers. Given a library libgoo.a that contains some utility functions you want to use in your foo program you can link foo.exe like this:

	gcc -o foo.exe foo.o bar.o foobar.o -lgoo

This assumes that libgoo.a is located in one of the directories on your LIBRARY_PATH. On the other hand, if you need to add a directory to the library path you can use the -L option. For example if goo was in the directory C:\goolib (or /goolib in UNIX-speak) then you could do this:

	gcc -o foo.exe foo.o bar.o foobar.o -L/goolib -lgoo

Or if libgoo.a was in the current directory you could use this:

	gcc -o foo.exe foo.o bar.o foobar.o -L. -lgoo

You can also include libraries directly using their full file names, like this:

	gcc -o foo.exe foo.o bar.o foobar.o libgoo.a

Linking with a library is just like including all the object files inside the library on your link command line, except for one difference. If none of the object files (or other libraries) on the command line before a library contain references to an object file inside the library then that object file will not be added to the program at all. This means that this will not work:

	gcc -o foo.exe -lgoo foo.o bar.o foobar.o

Because none of the objects in libgoo.a will be added to your program and all the references from foo.o bar.o and foobar.o will fail to be resolved. You need to put a library on the command line AFTER all other objects or libraries that might reference it have been included.

To create your own libraries use the ar program. The following command adds foo.o and bar.o to the foobar library, replacing old copies if they exist, and creating the library from scratch if it didn't exist in the first place:

	ar -ru libfoobar.a foo.o bar.o

That's it for now. Happy programming.
Colin Peters (colin@bird.fu.is.saga-u.ac.jp)



-- Colin Peters - Saga Univ. Dept. of Information Science
-- colin@bird.fu.is.saga-u.ac.jp - finger for PGP public key
-- http://www.fu.is.saga-u.ac.jp/~colin/index.html
-- http://www.geocities.com/Tokyo/Towers/6162/

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".



More information about the Cygwin mailing list