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)


Well 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
   #include "myheader.h"
 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.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2017 About.com. All rights reserved.