visual studio 2010 single project with multiple cpp files with main - c++

I want to examine a lot of little cpp files that have int main() , and run only one of them at one running . something like this -
Is there any way to do that without open new project for every cpp file ?

You can choose not to compile the files. Go to the properties, and exclude the file from build.

No. In a project, you cannot have multiple main() function.
You can rename them to test1(), test2(), test3(), and call them one by one from main(). That is what I do usually.

Related

Why can't I automatically create definition files in Visual Studio?

I am making my first steps in C++ and after creating a class header file I want to automatically create a definition file by pressing Ctrl+., but Visual Studio tells me that it couldn't create one. I always have to manually create one and then it works. Also, when I put the headers and source files in separate folders, Visual Studio tells me that the file is write protected. All files are located in my profile folder, so there should be no write protection.
Any ideas why this could be?
I suggest you could refer to the Doc: Create Declaration / Definition
When: You have a function that needs a declaration, or vice-versa.
As far as I'm concerned, first of all, you should have a function that needs a declaration.

Visual Studio console debugging wrong file

I have created an empty project and added two ( .cpp) items inside the project. Keep in mind that I am a beginner in C++ Visual Studio, so I have not used any code to somehow connect the two files.
Problem: The debugger was fine for debugging my first file, but when I open my second file and start running by clicking "Local Windows Debugger" (clicked when I was inside the second file), it will still keep on running the first file whether there was a bug or not.
When I looked at the debug window after I hit "Local Windows Debugger", I saw the file path pointing to the first file.
I have tried: Closing the first file completely, closing visual studio and opening my second file from my folder path, turning off the break-point in the first file and turning on in the second file.
I would like to know: How can I just run the second file? Do I have to use the command prompt to keep my two items in the empty project separate? I am using Windows 10 by the way.
I searched for my problem, but I had a hard time looking for a guide that gave a solution
Issue:
Dracep cannot debug one of two files they have. This is due to them having a main function definition in both files, causing the editor to favour one over the other.
Solution:
By having a third, dedicated entry point in your application (I.E. having a single point of entry you then include the other files), you can decide which file you are going to debug at any given time.
For example, having a file called main.cpp which then includes the other two files by using #include "filename.h".
From there you can include the file and make you code checks by calling the functions in that file rather than having a main and stepping down through it, causing long term issues of scalability.
Please see this question on separating your logic from your definitions, as the answer marked correct is the standard for most C++ projects you will find.
That way, you could do something like the following:
#include "File1.h"
#include "File2.h"
int main(int argc, char** argv)
{
File1Class file1Class;
File2Class file2Class;
//Do whatever tests you like with either.
}

Multiple main C++ files in a single project in Code::Blocks?

I do not regularly write code. There are times I write code daily for 6 months, and then do not code for up to 2 years. This approach has forced me to keep a bunch or reference code that I (and other much, much better programmers) have written. I refer to this "library" when writing code after a long period; I read it, I execute it, and that is a massive help in refreshing myself. This system has served me very well with Eclipse & Java over the past 5 years.
I am now learning C++ and am using Code::Blocks. I would like to somehow stuff a bunch of C++ files that have main methods into a single Code::Blocks project. I am willing to rewrite the code to achieve this task (if it's reasonable...)
I am not the first to look for a meaningful answer for this issue: https://stackoverflow.com/questions/35917504/how-can-i-make-multiple-programs-in-a-single-project-in-codeblocks and https://www.reddit.com/r/learnprogramming/comments/3opp5r/how_to_run_multiple_cpp_files_separately_in_same/
I do not want to change the IDE or compare it to other IDEs. What I am looking for, is the ability to execute one of hundreds of tiny programs that are in a well organized in an expandable file tree in C::B quickly and easily. If I put each C++ file with a main in it's own project I will have so many C::B projects that it will be unreasonable.
I do understand that C::B is not Eclipse and C++ is not Java, and that C::B is intended to have a single c++ file with a main function per project.
Any answers, and even very creative answers would be very appreciated! Scripts, settings, how to rewrite my code, whatever - if you have a suggestion I would love to hear it so I may consider it.
In the interest of full disclosure, currently I am keeping all my tiny programs in directories and use the O/S to drill through the directories and simply double click on the .cpp file which C::B opens. I am willing to dramatically modify my code to be able to achieve the objective.
Thanks for your time.
To compile and run a single main() file in a Code::Blocks project with multiple main() files:
In the "Projects" tab on the left, right-click on a file that you do not want to compile.
In the menu that appears, point to Options, and uncheck both Compile file and Link file.
This has to be done for all the files that you do not want to compile.
Now, when the project is built and run with F9, only the desired file will be compiled; the others will be ignored.
Note: It is not necessary to create a project in Code::Blocks to compile and run single file. To compile and run single files (without creating projects)
Just click on File -> Empty -> New file.
Save the file with .cpp extension anywhere (not in a project).
To compile and run the file, just press F9 or Build -> Build and run
However, such files (without projects) cannot be debugged. The most appropriate thing would be to just have a project with multiple main() files, as explained in the early part of this answer.
I've learned Java before, and am starting to learn C++ now.
When using Code::Blocks as my primary C++ IDE, I met the same problem as you. Although I found nothing useful on the Internet, I managed to figure it out on my own. Here is my solution.
In your Management panel, find your current project and right click, choose Close Project. (We won't use project to manage our code for this purpose.)
Still in the Management panel, find the Files tab, navigate to your working directory, right click on it and choose Make Root.
And we are done!
When you want to add a new code file, right click on your folder and choose New file..., enter your file name with the extension of .cpp. Then you can just use your C::B as before. Without the limit of project, C::B will just compile your current C++ file and run it on its own. Keyboard shortcuts F9 and the Run and Build button still works.
The only disadvantage is that you can see .exe and .o files in your file list, which is a little untidy. I'm still trying to find how to hide them in the list.
Hope this will help you.
How about using the precompiler? You can surround each main with:
#ifdef EXECUTE_EXAMPLE_1
int main() { return 0; } // example of one of the "mains" in one cpp
#endif
#ifdef EXECUTE_EXAMPLE_2
int main() { return 0; } // another "main" in an other cpp
#endif
#ifdef EXECUTE_EXAMPLE_3
int main() { return 0; } // yet another "main" somewhere else
#endif
And creating a header, included by all "mains" where you can define one to run:
#ifndef _EXECUTION_HEADER_H_
#define _EXECUTION_HEADER_H_
// Uncomment one and only one
#define EXECUTE_EXAMPLE_1
//#define EXECUTE_EXAMPLE_2
//#define EXECUTE_EXAMPLE_3
#endif // _EXECUTION_HEADER_H_
This could be a quick and dirty "build system" for your usecase.
I think this is what they were trying to accomplish.
No, you can't have two main files in the same Project, but you can have a lot of smaller project programs saved to a workspace which allows you to run and test them each individually.
https://www.youtube.com/watch?v=cHGIIp3rGO8

Is there a way to compile and run a single .cpp file in a Visual Studio Express '12 project?

I have just started learning c++ and I am using Microsoft Visual Studio Express 2012. I started a project where I was planning to have all my .cpp files but I have now run into a problem where when I try to compile and run a specific .cpp file it doesn't work.
VS seems to just compile and run the .cpp file with the main function in it and it makes a .exe and runs it. So since my first .cpp file (that holds the main()) is a simple hello world program I am only getting that one when I try to compile and run now.
I have another .cpp file with a int age() function that is supposed to ask for a users age and then output it. It's very simple and I just want to run it to see it in action but I can't figure out how to compile that particular .cpp file in my project since it only seems to want to compile the main .cpp file with the main() function.
How can I compile a specific .cpp in the project?
All c++ programs start in the main function. Why don't you try calling age() from main?
Of course, in order to do so, you will need your main.cpp to be aware that there is a function called age. This is where header files come in.
In total, you will therefore need the following:
main.cpp
#include "age.h"
int main() {
age();
return 0;
}
age.h
#ifndef AGE_H
#define AGE_H
int age();
#endif
age.cpp
#include "age.h"
int age() {
// Do age stuff.
return 42;
}
Try dividing your .cpp files into Projects if you really have to compile them separately. but for that too you will need a main in each of the projects.
Another choice you have is creating dll projects.
But since you told you want to keep it simple i wont suggest it.
For too simple console programs use some more easier & simpler IDEs.
But what ever be the IDE, ccp files (even c) programs can be run only from main.

Choosing which main function to use in Visual C++ 2010

I'm porting a C++ application from Unix and the original developer created several files with main() functions, then use Makefile to choose which main() to use.
How do choose which file contains the main() function in Visual C++ 2010?
Currently, when I compile I get a linker error due to duplicate main() symbols.
The only thing I can think of is macro conditional.
Any other ideas?
Multiple main functions mean that the original code does not create a single executable, but rather a set of them. You should figure out what parts belong to each one of the executables (read the Makefile) and then create different projects inside the solution one for each one of the executables (do the same for the libs). Then you can use the IDE to select which executable you want to compile/run.
in the Configuration Properties for each source file (right-click in Solution Explorer) you can select 'Excluded From Build'. As this is a per-configuration setting, you can add some configurations and mutually exclude the files with main(). For instance for configuration 'MainA' you include maina.cpp and exclude mainb.cpp and mainc.cpp, for 'MainB' include mainb.cpp and exclude maina.cpp and mainc.cpp, etc.
Another option would be to have only one main() and select the appropriate source using arguments or a configuration file. Or, maybe the best solution, create one project for each main file and put the common parts in a static or shared library.