You are here:

C++/Compile cpp file without void main

Advertisement


Question
Hello !!

How to compile cpp files without void main(). And after successful compilation how to link all the files to make a project in C++.

Please answer.

thank You.


Answer
Only one C++ implementation (.cpp) file requires a main (assuming you wish to build a standard console application). The _standard_ forms of main are:

   int main()
   {
   /*..*/
   }

Or:

   int main( int argc, char * argv[] )
   {
   /*..*/
   }

The void main variant is a Microsoft perversion. Hence I am guessing you might well be using a version of Microsoft Visual C++.

Also despite the standard forms of main requiring an int return type for main (and main only) the return statement can be left out, in which case the compiler will assume return 0 for you. Note that older Microsoft compilers (e.g. MSVC++ 6.0) did not accept this behaviour which may be why they added the non-standard void main form.

Hence you can write most project implementation files that have no main function. This is the usual case. The file with main in it is the entry point into your program and typically hands off work to other project functions, class objects etc.

These files can then be compiled one at a time to produce object files (.obj files or on some platforms .o files). You may also be able to pass more than one source file to the compiler at a time. The object files are then passed as input to the linker, together with any libraries that your program uses. Again such information might be able to be passed on the same compilation command line.

In Microsoft Visual Studio Visual C++ projects the project object files and a default set of libraries should be included as inputs to the linker by default.

Note that if you are writing a MS Windows program rather than a console program then the entry point to your program is called WinMain rather than main. If you are using a framework such as the MFC then the WinMain function is part of the framework, so you should not supply one.

The exact syntax of compiler and linker commands varies depending on the compiler and linker used. For Visual C++ the command line compiler/linker driver program is called cl. You have to execute a batch file at the command prompt first to set up the environment. For 32-bit x86 compilers this can be located in the Visual Studio Bin directory (e.g. for VC6 it might be C:\Program Files\Microsoft Visual Studio\VC98\Bin) and is called VCVARS32.BAT. Once executed you can get quick help on the cl command's options using the /? switch. For more detailed information on using Microsoft Visual C++ refer to the product documentation. This can be found in the MSDN library. The online version is located at http://msdn2.microsoft.com/, specifically http://msdn2.microsoft.com/library. Look under the Development Tools and Languages section then drill down into the sections for the version of Visual Studio your version of Visual C++ is part of. Note that older versions of the product may not have complete documentation on line anymore, in which case refer to the documentation supplied with the product. The information on building programs using Visual C++ 2005 can be found at:

   http://msdn2.microsoft.com/en-us/library/z7kx322x(VS.80).aspx.

[Note: links valid at time or writing as far as I can tell!]

In most projects of any size you generally compile and link in separate stages. Most compilers have an option to compile only - for the Microsoft Visual C++ cl command it is the /c switch. Then once all source files have been compiled you can then link them together using the linker (link for Visual C++). Again refer to the documentation for detailed information on the exact syntax required, and yes I tend to do this as well as I do not keep exact details of the syntax of all tools I have ever used in my head!!

Note that I have tried to give you some pointers as to what to do. As you give no specific details of the compiler, linker, platform, etc. you are using nor details on the exact problems you are facing I cannot really be more specific.

Hope this gives you some pointers anyway and allows you to proceed.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

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.

Experience

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/.

Education/Credentials

©2016 About.com. All rights reserved.