1

I'm EXTREMELY new to Visual Studio and C++ in general and I wanted to run a file within a project with other files in it. The file contains the simple code to print "Hello, World!". When I run this file, an error claims that main() has been defined in a .obj file which contains simple addition code as well. These two files are in the same project.

For some reason (maybe because of my ignorance) it seems nobody else has this problem.

Here's the error:

Error LNK2005 _main already defined in addition.obj

Scene
  • 489
  • 4
  • 16
  • 1
    ***These two files are in the same project.*** There is your bug. You need to create a new project for each application. Each project can contain at most 1 `int main()` – drescherjm Oct 22 '19 at 00:33
  • Ah, I see. It seems I confused a "solution" with a project. It makes much more sense to create a new project now. – Scene Oct 22 '19 at 00:46
  • 1
    Sorry man. A C++ (or C for that matter) program can have exactly one entry point. But your problem isn't a problem with C++. C++ doesn't give a smurf about projects. You can easily compile and link individual files from the command line or any one of hundreds of build automation tools or IDEs. When I want to bang out a quick test, often I get out of the IDE and slap the test code down in a simple text editor and compile the file all on its lonesome. – user4581301 Oct 22 '19 at 00:46
  • 1
    It sounds like you've figured it out. Yes, a VS project defines a single program and duplicate symbols (like function names) in a project can cause compiler or linker errors. – Blastfurnace Oct 22 '19 at 00:49
  • A program starts running in only 1 main function. You could rename the main functions, and create one main function that calls them both. – Jeaninez - MSFT Oct 22 '19 at 01:25

1 Answers1

1

C++ can only have one main() per program as the compiler says.

You need Visual Studio to treat the files as separate programs. To do that you need to create separate projects for each of the files and compile them separately.