如何在 Visual Studio 2012 中创建和运行简单的 C++ 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17251966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create and run simple C++ programs in Visual Studio 2012?
提问by Sangeeth Sudheer
I'm currently a C.Sc student in India. We have lots of C++ to study but still is limited. Even if it goes the farthest, the toughest programs would be to make a library management system or a bank management system. The problem with the current syllabus is that it relies on the old C++ standards and we work on the old Turbo C++ compiler.
我目前是印度的 C.Sc 学生。我们有很多 C++ 需要学习,但仍然有限。即使它走得最远,最艰难的计划还是制作图书馆管理系统或银行管理系统。当前教学大纲的问题在于它依赖于旧的 C++ 标准,而我们在旧的 Turbo C++ 编译器上工作。
Sine I have a Windows 8 system, I can't make Turbo C++ to work seamlessly in my system. I would have to use DOSBox to emulate the executables. Moreover, I would like to switch to a newer compiler/IDE that's based on the latest C++ standards and fits for future education.
因为我有一个 Windows 8 系统,我不能让 Turbo C++ 在我的系统中无缝工作。我将不得不使用 DOSBox 来模拟可执行文件。此外,我想切换到基于最新 C++ 标准并适合未来教育的较新编译器/IDE。
I tried Visual Studio and NetBeans and both are powerful and excellent. But I can't cope with these IDEs as I don't find ways to practice the silly codes I learn from school. We don't learn C++ to an extended level as I said before. The options in Visual Studio is to create a new project and I did that but it seems these 'Projects' are for app developers for developing programs based on C++. For a beginner like me, it's not helping.
我尝试了 Visual Studio 和 NetBeans,它们都非常强大且出色。但是我无法应付这些 IDE,因为我找不到方法来练习我从学校学到的愚蠢代码。我们不会像我之前所说的那样深入学习 C++。Visual Studio 中的选项是创建一个新项目,我这样做了,但这些“项目”似乎是为应用程序开发人员开发基于 C++ 的程序设计的。对于像我这样的初学者来说,这没有帮助。
I see a lot of people recommending Visual Studio Express for learning C++ but I can't seem to understand the working principles yet. Whenever I create multiple files in Visual Studio and debug them, all those files debug at once and I can't use main
function for each and every C++ file as it reports an error.
我看到很多人推荐 Visual Studio Express 来学习 C++,但我似乎还不能理解其工作原理。每当我在 Visual Studio 中创建多个文件并对其进行调试时,所有这些文件都会同时进行调试,并且我无法main
对每个 C++ 文件使用函数,因为它会报告错误。
I need to know how I can seamlessly create, edit and organize my C++ files in Visual Studio 2012. Or, if these are for app developers and enthusiasts, can you suggest me some other IDEs for practicing my codes without going into the complex parts?
我需要知道如何在 Visual Studio 2012 中无缝地创建、编辑和组织我的 C++ 文件。或者,如果这些文件是为应用程序开发人员和爱好者设计的,您能否建议我一些其他的 IDE 来练习我的代码,而无需深入研究复杂的部分?
采纳答案by Floris Velleman
What I think you are trying to achieve is that you can have multiple small programs in a project. Now while visual studio doesn't allow this with these exact words it is possible to achieve this.
我认为您想要实现的是,您可以在一个项目中拥有多个小程序。现在虽然visual studio不允许使用这些确切的词来实现这一点。
Using this setup:
使用此设置:
Solution
Project
Project
etc.
This is done by going to: File -> new -> Project.. where you initially select the first project you want, for example: an empty project as this is probably what you are looking for. Now in the Solution Explorer you will see your project which allows you to add a new source file: "main.cpp" for example.
这是通过转到:文件 -> 新建 -> 项目.. 在这里您最初选择您想要的第一个项目,例如:一个空项目,因为这可能是您正在寻找的。现在在解决方案资源管理器中,您将看到您的项目,该项目允许您添加新的源文件:例如“main.cpp”。
When you want to add another "main" you simply right click the solution and add a new project (which can be an empty project or any project).
当您想添加另一个“主要”时,您只需右键单击解决方案并添加一个新项目(可以是空项目或任何项目)。
Now when you want to run the other project you can simply rightclick the project file and select: "Set as startup project".
现在,当您想要运行其他项目时,您只需右键单击项目文件并选择:“设置为启动项目”。
回答by sbhal
To have multiple self compilable single files in a project, you can exclude all other files except one which you want to compile.
要在一个项目中有多个可自编译的单个文件,您可以排除所有其他文件,但要编译的文件除外。
Eg. Lets assume this hierarchy:
例如。让我们假设这个层次结构:
-Solution1
-Project1
-file1.c
-file2.c
Now if one wants to compile only 'file2.c', thus excluding 'file1.c' from compilation, one can go properties of 'file1.c' and change 'Exclude from Build' field to 'Yes'.
现在,如果只想编译“ file2.c”,从而将“ file1.c”排除在编译之外,则可以转到“ file1.c”的属性并将“ Exclude from Build”字段更改为“ Yes”。
回答by Eduardo
This guy does great videos, hh did a video on getting familiar with Visual Studio: http://thenewboston.org/watch.php?cat=39&number=3The rest of the videos are for a different programming language but the IDE layout is all the same.
这家伙做了很棒的视频,hh 做了一个关于熟悉 Visual Studio 的视频:http: //thenewboston.org/watch.php?cat=39&number=3 其余的视频是针对不同的编程语言,但 IDE 布局是全部都一样。
To start a new project: File -> Select C++ on the left hand side - > Console Application Give it a name and click ok. on the right hand side there will be the folder layout, right click src, add new file, select .cpp and give it a name.
要开始一个新项目:文件 -> 在左侧选择 C++ -> 控制台应用程序 给它一个名称并单击确定。在右侧将有文件夹布局,右键单击 src,添加新文件,选择 .cpp 并为其命名。
This is your main
这是你的主要
I hope this is what you mean and that video explains what you need to know!
我希望这就是您的意思,并且该视频解释了您需要了解的内容!
回答by Tarang Garg
This is my first answer, kindly bear with me. Improving the previous answer..
这是我的第一个答案,请耐心等待。改进以前的答案..
Eg. Lets assume this hierarchy:
例如。让我们假设这个层次结构:
-Solution1
-Project1
-file1.cpp
-file2.cpp
-file3.cpp
-source.cpp
Create functions in file1.cppor file2.cppwith whatever name you want instead of main function. Eg in file1.cpp
在file1.cpp或file2.cpp 中使用您想要的任何名称而不是 main 函数创建函数。例如在file1.cpp 中
#include <>
int helloworld()
{
//your code here
return 0;
}
In file2.cpp
在file2.cpp 中
#include <>
int fibonacci()
{
//code here
return 0;
}
Now Write the Source function as:
现在将 Source 函数写为:
#include <iostream>
//declare the functions defined in other cpp
int helloworld();
int fibonacci();
//Write the main function here and call the appropriate function from here
int main()
{
int i;
cin>>i;
switch(i)
{
case 1: helloworld(); break;
case 2: fibonacci(); break;
// etc etc..
default: break;
}
return 0;
}
Voila! It will be like turbo c++. This method has 1 drawback. All the cpps will get compiled. So, take a note of this. Good luck!
瞧!它会像涡轮增压 C++。这种方法有 1 个缺点。所有的 cpps 都将被编译。所以,请注意这一点。祝你好运!