C program in Visual Studio
I am new to Visual Studio. I'm trying to run Hello World, but am getting several errors and cannot figure out what the problem is. I typed:
#include<stdio.h>
main()
{
printf("Hello World");
}
into a code file with .c extension. I get this:
Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
d:\Users\...\MSVCRTD.lib(crtexe.obj) Project
Error 2 error LNK1120: 1 unresolved externals
d:\users\...Project.exe开发者_如何转开发 1 1 Project
Anyone know what the problem is? Thanks.
It compiles fine... you need to set it to compile as C code:
Project->Properties->Advanced->Compile As C Code (/TC flag)
#include<stdio.h>
main()
{
printf("Hello World");
}
Output:
1>------ Build started: Project: main,
Configuration: Debug Win32 ------
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped
==========
Reason:
You are compiling as C code and therefore default int is not assumed in C++ code
Update:
As mentioned by Michael Burr your code should use a *.c
extension. However, it will still compile cpp files as c code if you set the project properties. However, if no setting is provided it will compile with the default settings (*.c -> c code)
and (*.cpp -> cpp code)
.
Compiled as C code with CPP extension (successful)
1>------ Build started: Project: main, Configuration: Debug Win32 ------
1> main.cpp
1> main.vcxproj -> c:\users\shane\documents\visual studio 2010\Projects\main\Debug\main.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Compiled as CPP code with C Extension (failed)
1>------ Build started: Project: main, Configuration: Debug Win32 ------
1> main.c
1>c:\users\shane\documents\visual studio 2010\projects\main\main\main.c(4): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
There are two major problems with the code provided. The first is that you did not add a header to be include after "include." Try this instead:
#include <stdio.h>
The second is that main needs a return type. Try:
int main()
{
printf("Hello World");
return 0;
}
精彩评论