Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Multiple definition of main() /function()

Posted on 2008-11-09
8
Medium Priority
?
1,861 Views
Last Modified: 2012-05-05
Hi, I am using eclipse cdt, it is a managed c++ project, I have added a second x.cpp file and pasted some code and main function, now the compiler is using the same make for both of them, how to go around this?

thx
0
Comment
Question by:TheSilverBullet
  • 3
  • 3
  • 2
8 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22917470
Create a new project for the other file, and that should work better :)
0
 

Author Comment

by:TheSilverBullet
ID: 22917923
nope I like to have more than one classe each with its main, that was dump easy in MS visual studio!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22917941
>> that was dump easy in MS visual studio!

You can only have one main in each project.

You can have multiple classes though, but again : only one main.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Accepted Solution

by:
evilrix earned 750 total points
ID: 22928701
>> You can only have one main in each project
Just to expand on this, The function 'main', when defined int he global namespace is the entry point for the application. An application can only have one entry point, if the compiler finds more than one entry point the code is malformed and the compiler will generate an error. A project is a conceptual container for all resources and files that are used to generate an application, so if you have more than one main in a project you will have duplicated entry points.

It is perfectly legal; however, to define main in other namespaces. I would; though, advise against this as it will make for confusing code. Note, this only applies to C++, in C you don't have a concept of namespaces, but since you are referring to classes I presume you are referring to C++.
namespace
{
	void main(){} // This is legal, it's in an anonymous namespace
}
 
namespace SomeNS
{
	void main(){} // This is legal, it's in a named namespace
}
 
int main(){} // Entry point -- the one and only!!!
 
int main(){} // Not legal, already defined!!!
             // Not that even if this is in a different translation
             // unit this is still not legal!

Open in new window

0
 

Author Comment

by:TheSilverBullet
ID: 22943290
Maybe I didnt make it clear, in VS one could add a classe with main on the fly (not necessarly to a project), just for probing something, that is the goal, scratching. but with eclipse everything is difficult, and buggy! I spent several ours for integrating a computer vision library, and at end could not use the debugger...
0
 

Author Comment

by:TheSilverBullet
ID: 22943366
and guess what? most of the problems because my workspace path had a space!! kinda c:\documents and settings...
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 150 total points
ID: 22943371
If you want to debug your code, you can just run it in a debugger. No need to add a main or anything like that.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 22944739
>> Maybe I didnt make it clear, in VS one could add a classe with main on the fly (not necessarly to a project)
Was my post above, that explained you can have as many mains as you like but only 1 global, not clear enough? The same principle as shown in namespaces in my example also extends to classes (since classes are really just a special type of namespace -- honest).

To be honest I am not really seeing what the problem is here, maybe you could post an example that doesn't work for analysis? I suspect your observations on "main" are a red herring... this would not affect your ability to invoke and use the debugger (assuming the come compiles).
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question