C++/lynux
Expert: Ralph McArdell - 10/17/2004
Questionhello
My name is carl and i am trying to compile a program. this is what i type to compile it( cpp Wall {name}.cpp -O {name} ) and when i press enter it says (too many input files)
thanks!
AnswerWell I assume you mean that your operating system is Linux _not_ Lynux.
Your first mistake is using cpp as the program to compile your C++ code. Maybe you assumed that the extension indicates the name of the program used to compile your code - whatever, it does not. The UN*X and Linux program cpp is the C Pre-Processor program - hence cpp. It is used by various utilities as well as C and C++ compilers. In the case of Linux you are probably using the GNU C-Compatible Compiler Preprocessor. The utility you should be using is probably the GNU C++ compiler and is called g++ or gcc. If you do not believe me try reading the manual (man) page for cpp (assuming you have them installed on your system). Try entering:
man cpp
and see what it says. If it says something about no manual entry for cpp then the man page is not installed on your system.
Now try:
man gcc
and see what it says.
Your next mistake is that your command line is incorrect for either cpp or gcc. Options are prefixed by a hyphen or (for long option names) two hyphens so your command line should probably look more like:
gcc -Wall mysourcefile.cpp -o myoutputfilename
Note also that the output file name option is a lower case o not an uppercase O - uppercase O (-O) is a code optimisation option.
Here is a test file I created for you to check out the differences when using cpp and gcc:
// cpp vs. gcc output test example
// Test #1: Use the following command line
//
// cpp -Wall test.cpp -o cppout
//
//
// Test #2: Use the following command line:
//
// gcc -Wall test.cpp -o gccout
//
//
// Now try:
//
// ls -l
//
// to see which output file is executable,
//
// cat cppout
//
// to see what cpp did and
//
// gccout
//
// to see what gcc did.
int main()
{
int i=0;
return i;
}
The expected results are that only gccout is executable and (as you can see) when run just returns 0 so produces no output. cppout is a text file that is obviously a modified version of the input file test.cpp. gcc runs the input file through cpp and compiles the output. In fact you can get it to do the compilation for you from cpp ouptut, try:
gcc -x cpp-output -Wall cppout -o gcccppout
Here we get gcc to compile the previous output from cpp. It is necessary to tell gcc which language cppout contains as it has no helpful extension gcc can use to deduce it, hence the -x cpp-output option - which must occur _before_ the input file is specified. Note that we could have used an extension of .ii in our output file name in the original cpp command line as this indicates to gcc that the file contains pre-processed C++.
In fact there are several stages to produce an executable program from C++ source code: Basically we compile our source file(s) then link the resulting object code files together, usually also with libraries of code such as the C runtime library.
However, as you have seen compiling itself has more than one stage. The first stage is pre-progressing: In this stage all pre-processor directives are resolved. These are the directives with names starting with a # such as #include, #define, #ifdef, #endif etc. As you will have seen comments are also removed at this stage. The output of this stage (such as cppout in our example) is called a translation unit and is passed to the compiler proper. The result of the compilation is either directly an object code file which contains a mix of housekeeping, symbols and target machine code or it is code that can be run through another translation program - C or assembler are common choices.
Finally, programs like gcc can perform some or all these stages, passing the command line options and files through to the relevant programs that handle each stage such as cpp for pre-processing or ld for linking. Note also that exact details vary from compiler to compiler, however there seems to be a core of common options and tool names for many UN*X and Linux C and C++ build tools.
For another way of working you could look at the Microsoft or Borland products. Microsoft's equivalent of gcc is called cl and it has much less in common with UN*X style tools. Their linker is called link and the compiler has various stages handled by dlls with names like c1.dll and c2.dll. Further their command line options are prefixed, as is the MS standard, by forward slash rather than hyphen. By the way, this is why MS pathnames use backslashes to separate directories. Internally or in quotes on a command line you can generally use either forward or back slashes in MS pathnames. On the command line using forward slashes in a pathname without quotes gets them confused with command options.
If you wish to see what effect the pre-processor has on source code before it is compiled you might like to try modifying my extremely simple example in various ways, for example by:
- adding some header includes
e.g. #include "myheader.h"
(note: this assumes myheader.h exists can cpp can locate it)
- adding some #define macros
e.g #define ZERO 0 and replacing int i=0; with int i=ZERO;
- adding some conditional compilation directives
e.g:
#ifdef INCLUDE_MYHEADER
#include "myheader.h"
#endif
then run the original cpp command line and check the contents of cppout.
Next add -DINCLUDE_MYHEADER to the cpp command line, run and check the
contents of cppout.