Thread: Visual C++

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Visual C++

    Hi,

    I'm new to C++ and have chosen Visual C++ as my IDE.

    When I create a project, for example to practise with variables. Can I only save one file with each project if I need to use the main function for each practise file?

    For example I have two files open in one project, writing two seperate programs to practise. Both use int main (). When I build I get this error: error LNK2005: _main already defined which is due to using _main in each file.

    If I want to practise and save each piece of work that I do, do I need to create a project for each?

    Hope this makes sense.

    Thanks,

    Darren.

  2. #2
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    You can only have one main function per project I think. main() is the driver for your program, you shouldn't need to use it twice :S

    EDIT:
    I'm not quite sure about having more than one main function in a solution though, but other programmers on this board definitely will!
    Last edited by legit; 06-17-2009 at 08:10 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If the two programs aren't related, they shouldn't be in the same project I wouldn't think.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks guys, thats what I thought. Will have to create new projects for each.

  5. #5
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    I have three projects named "Blank1," "Blank2" and "Blank3" which I use for trivial experimentation purposes. It's easy enough to switch between them in VC++.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Another way to do it, if you think separate projects is overkill, is by switching between different build configurations. Suppose you had a single main() function in a file called Main.cpp:

    Code:
    int main( int argc, char **argv )
    {
        return MAIN( argc, argv );
    }
    Then suppose you had three test programs called Prog1, Prog2, and Prog3. Instead of these programs having a main(), they have a function called Prog1(), Prog2(), or Prog3() respectively.

    Now, in the build configuration manager, you can create three separate builds for each of these, with the difference between the builds being the preprocessor definition MAIN=Prog1, MAIN=Prog2, or MAIN=Prog3 respectively.

    The unused ProgX.cpp modules won't get linked into the final executable, because their symbols are not (shouldn't be) referenced.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM