Main function already defined in visual studio project - c

I created windows console program project in visual studio, and made two c language files. Both has same source:
#include<stdio.h>
int main() {
printf("hello");
}
When I try to compile, compiler screams about having two main()s. See below:
I can't understand. I thought two files in one project works separately. Am I wrong?

Yes, you are wrong. IIRC, the whole project is compiled and then linked to form a single executable.
In a single executable, there can be only one main() function.

These two main functions are within the same Project. You should have only one main function in your program.
Try to implement a new project, and then add the main function in there.

Related

XCODE error: new file not built

So first I made a new project by going to File> OS X > Command Line Tool and chose C as the language. This is what I got:
Then I made a new C file called program.c:
Then I pressed run. It didn't build successfully:
However, when I deleted the first default file (main.c) and then pressed run program1.c....it built successfully.
My problem: Why can't I have several files under the same project and run them successfully?
Because you will have the main function declared in both files. I do not know how to tell XCode to build two separate applications..
You can't have 2 main functions
Edit: each c program has to contain only one main function.
Other files of that program can not implement another main function.

multiple definition of main first defined here

I'm new to programming and currently I'm learning C programming. I'm writing codes on the code blocks and in it using GCC compiler. When I create a new project, (as you know it creates main.c file with it) and due to that I'm not able to compile another file in that project.
File 1:
#include<stdio.h>
int main()
{
int a,b,c,d;
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
d=a;
if(b>d)
d=b;
if(c>d)
d=c;
printf("\n The maximum of three numbers is %d",d);
}
File 2: main.c
#include <stdio.h>
int main()
{
printf("Hello world!\n");
return 0;
}
When I compile the first programme, it shows the following error:
multiple definition of 'main'
first defined here
I've searched every where I could and I'm not able to solve this. In one of the answers here on stack overflow, someone had suggested to write this in
(Project->Build options...->Linker settings (tab))
-Wl,--allow-multiple-definition
When I wrote it, there were no errors. But it wasn't able to run my File 1 and instead, it runs that main.c file. Even when I close the main.c file, it opens there again and runs main.c file which gives the output "Hello World!".
Initially when I was using code blocks there were no such errors. I don't know why this is happening and I've not much knowledge about compilers.
As noted in comments you can only have one main function.
So when you start a new project you need to replace the main.c file with the main.c file you want to use. Or you can edit the 'hello world' main.c program.
When you start a new project in code::blocks you can get a new directory with a simple program that prints 'Hello World'. This file is usually main.c. You need to edit this file or replace it. The reason that code::blocks puts this simple main.c program in the new project is so that you can compile it and test your system without having to write a new program.
Some computer languages allow you to use the same function name for different functions ( which are identified by their parameters and sometimes return types ). That's called overloading. C does not allow this. Functions in C must have unique names.
The main() function is a special one in C as it is used as the standard entry point for applications. That is, the main() function will be called first and your application should start and (typically) end in that function.
As a beginner I would suggest you avoid automated editor features that create and build projects for you. You will miss out on learning how things work doing that. Use an editor to start from empty files and learn how they all connect and how to use the compiler from the command line. The command line is something every beginner should start from, IMO.
It may be harder to learn, but it will give you a much better feel for what is going on.
I guess what you maybe trying to do is have multiple sandbox "gists" that you may wanna run all as their own main function. If that is the case, then just close your project and open the files directly. As long as they are not in a project, they will run fine.

CLion IDE doesn't recognize my C symbols

Just installed the new CLion IDE and wanted to try it.
Wrote a simple C program in a single source file that has 2 functions: foo() and bar(). main() calls foo and foo calls bar.
Tried to find references of both of the funcs and got nothing. Same goes for call hierarchy. I assume I forgot to configure something.
The C file compiles with gcc.
I googled the problem but found nothing helpful.
Any ideas?
Thanks

Two main() in different source files under same project

Hey guys can we use two or more main() in different C source files under same project in eclipse? What am I actually trying is to write a server and a client source file under same project with main() in each of it. I am getting an error main() redeclaration. is there any way to do this? If yes please tell me how to run that successfully in eclipse CDT Kepler. Regards,
You could also simulate having two main functions in the same project by having main call either mainClient or mainServer (your two main functions renamed) depending on a condition of your choice.
Yes, you just have ton include each one selectively when linking your two programs.
A build system (Makefile, IDEā€¦) helps.
Example
If you have these source files:
Client only:
main-client.c
source1-client.c
Server only:
main-server.c
source1-server.c
Common sources:
source1-common.c
source2-common.c
source3-common.c
Then a simple (stupid) Makefile is:
all: client server
client:
gcc -o client main-client.c source1-client.c source1-common.c source2-common.c source3-common.c
server:
gcc -o server main-server.c source1-server.c source1-common.c source2-common.c source3-common.c
Hey guys we can actually do it by following what Simon and Brandin suggested. Also see this example if anyone still have doubts. Thankyou!
#define my main()
my() {
printf("hello frnz");
}
So we can have as many mains as we wish. Vola

Visual Studio: Create a Hello World app in C?

How can I create a basic C app in Visual Studio, be it 2010 Ultimate or 2008 Professional? I have searched through the project templates, and can find plenty for C++, but none for C.
(I'm hoping that the compiler and debugger will be built in.)
New project/Win32 Console Application/Empty project.
Add a file called "hello.c" (important that it's .c)
Type out a basic hello-world:
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
Compile, execute... PROFIT!
Visual Studio doesn't have a separate compiler for C, it uses the C++ compiler for C code. You can tell it to restrict itself to legal C syntax by using a compiler switch or by renaming the .cpp file to .c
Edit: I'm still using 2005 on this machine, so it might not be in the same place, but try this
Right click on the main.cpp in the solution explorer pane (on the right).
Choose Properties (bottom of the menu)
Open up the C/C++ group of properties
choose Advanced page
Change "Compile As" property to "Compile as C Code (/TC)"
In addition to changing the file extension, you may need to change the compiler settings:
Settintgs of Application->C/C++->addition->compiler such...
You must take /TC for basic C and /TP for C++.
https://msdn.microsoft.com/ru-ru/library/032xwy55.aspx
Good Luck.

Resources