Microsoft 开发人员网络 > 论坛主页 > Visual C++ General > Simple Answer for Simple Question
提出问题提出问题
 

已答复Simple Answer for Simple Question

  • 2009年7月2日 18:47cmbrooks08 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi guys-

    I know this is going to be very simple, so I apologize if you feel I've wasted your time.  I'm transitioning from NetBeans for Java to Visual C++.  In NetBeans, I used to keep a project that would just kind of be my testing ground where I would store random code I was working on, or labs for classes, etc.  I'm trying to figure out which project type will let me do the same in Visual C++.  I created an empty Visual C++ solution, added an empty project, and added about 3 source files.  Since they aren't all related, they all have a main function, and because of that, I can't build.  I just want to run one cpp file at a time, but I can't figure out how to to do it without building everything, which gives me the "more than one main declared" error.  I can't believe I couldn't find an answer to this anywhere else, but maybe no one else does this? I find that hard to believe, but anyways, can anyone point me in the right direction?

答案

  • 2009年7月2日 19:40Brian MuthMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    There's no way to just have multiple .cpp files and right click and run (or something of the sort)? I don't want to have to build the whole project, I just want to compile the individual file and run.

    Just create a separate project for each .cpp file. You can have multiple projects in the same solution, which is very convenient.
  • 2009年7月2日 20:02nobugzMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Click, then Shift + Click to select all the files.  Right-click, Properties, General, set Excluded from Build = Yes.  Whatever file you want to try, set it back to No and build.  Avoid this problem in the future by keeping your main() in a separate source file.

    Hans Passant.

全部回复

  • 2009年7月2日 18:54Nishant Sivakumar版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Have a single main() from which you then call the test functions.  So you have main.cpp with the main() function, foo1.cpp with the Foo1() function, and foo2.cpp with the Foo2 class. Now to test foo1, you just call Foo1() from main(), and to test foo2, instantiate Foo2 in main and invoke methods on it. Repeat similarly for other classes/functions.


    http://blog.voidnish.com
  • 2009年7月2日 18:58cmbrooks08 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    There's no way to just have multiple .cpp files and right click and run (or something of the sort)? I don't want to have to build the whole project, I just want to compile the individual file and run.
  • 2009年7月2日 19:02Nishant Sivakumar版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    As far as I know, VS needs a project to build and run - it cannot handle standalone cpp files.
    http://blog.voidnish.com
  • 2009年7月2日 19:40Brian MuthMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    There's no way to just have multiple .cpp files and right click and run (or something of the sort)? I don't want to have to build the whole project, I just want to compile the individual file and run.

    Just create a separate project for each .cpp file. You can have multiple projects in the same solution, which is very convenient.
  • 2009年7月2日 20:02nobugzMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Click, then Shift + Click to select all the files.  Right-click, Properties, General, set Excluded from Build = Yes.  Whatever file you want to try, set it back to No and build.  Avoid this problem in the future by keeping your main() in a separate source file.

    Hans Passant.