简体   繁体   English

Visual Studio中的C程序

[英]C program in Visual Studio

I am new to Visual Studio. 我是Visual Studio的新手。 I'm trying to run Hello World, but am getting several errors and cannot figure out what the problem is. 我正在尝试运行Hello World,但遇到了几个错误,无法弄清楚问题出在哪里。 I typed: 我键入:

#include<stdio.h>

main()
{
    printf("Hello World");


}

into a code file with .c extension. 到.c扩展名的代码文件。 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. 谢谢。

There are two major problems with the code provided. 提供的代码存在两个主要问题。 The first is that you did not add a header to be include after "include." 第一个是你没有在“include”之后添加要包含的标题。 Try this instead: 试试这个:

#include <stdio.h>

The second is that main needs a return type. 第二个是主要需要返回类型。 Try: 尝试:

int main()
{
   printf("Hello World");

   return 0;
}

It compiles fine... you need to set it to compile as C code: 它可以正常编译...您需要将其设置为以C代码编译:

Project->Properties->Advanced->Compile As C Code (/TC flag) 项目 - >属性 - >高级 - >编译为C代码(/ TC标志)

#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 您正在编译为C代码,因此在C ++代码中不假定为int

Update: 更新:

As mentioned by Michael Burr your code should use a *.c extension. 正如Michael Burr所提到的,您的代码应该使用*.c扩展名。 However, it will still compile cpp files as c code if you set the project properties. 但是,如果设置项目属性,它仍将编译cpp文件作为c代码。 However, if no setting is provided it will compile with the default settings (*.c -> c code) and (*.cpp -> cpp code) . 但是,如果没有提供设置,它将使用默认设置(*.c -> c code)(*.cpp -> cpp code)

Compiled as C code with CPP extension (successful) 编译为带有CPP扩展名的C代码(成功)

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) 编译为带有C扩展名的CPP代码(失败)

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 ==========

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM