C程序不会在Visual Studio 2010编译 [英] C Program Won't Compile in Visual Studio 2010
问题描述
我有一个简单的C程序,我可以通过命令行编译Linux上很容易的,但是,要使用Visual Studio调试程序,这样我可以更容易地看到它在做什么,但我不能得到它的编译。
I have a simple C program that I can compile via command line on Linux easily enough but want to use Visual Studio to debug the program so that I can more easily see what it's doing but I cannot get it to compile.
我创建了一个新的cpp Win32控制台应用程序。加了我的.c和.h文件,但我不能让它编译。我得到100+的错误,主要似乎是熏鲱鱼。比如我有一个错误说一个变量没有声明时,我可以看到它之前立即就行了声明。例如:
I have created a new cpp Win32 console application. Added my .c and .h file but I cannot get it to compile. I get 100+ errors that primarily seem to be red herrings. For instance I have an error saying a variable isn't declared when I can see it declared on the line immediately before. For instance:
int i;
for (i = 0; i < unpacked_length; i++)
{
在为行,我得到一个错误:错误C2065:'我':未声明的标识符
on the "for" line I get an error: "error C2065: 'i' : undeclared identifier"
那么别的东西显然是怎么回事。这似乎合法的一个错误是这个:
So something else is obviously going on. One error that seems legit is this one:
static unsigned char* Base64_Charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
智能感知:类型的值为const char *不能用于初始化类型的实体无符号的字符*
IntelliSense: a value of type "const char *" cannot be used to initialize an entity of type "unsigned char *"
由于该工程通过命令行编译时,我不知道为什么智能感知会在这里给这个错误,除非这个编译器有比我通过命令行中使用的另一种不同的规则。我没有做任何C约15年,不知道这可能是在编译器中的差或如果有别的事情在这里。
Since this works when compiling via command line I don't know why the intellisense would be giving this error here unless this compiler has different rules than the other one I used via command line. I haven't done any C for about 15 years and don't know if this could be a difference in compiler or if there is something else going on here.
的只包括在源是:
#include "dtcrypt.h"
#include <string.h>
#include <stdlib.h>
鸭preciate任何帮助!
Appreciate any help!
推荐答案
Visual Studio 2010中只支持ANSI C(又名C89),它不支持更现代的C99或C11语言标准。 ANSI C要求您声明所有的变量在函数的顶端,你不能在使用之前,他们一个函数的中间定义它们。因此,尝试重写你的code是这样的:
Visual Studio 2010 only supports ANSI C (aka C89), it does not support the more modern C99 or C11 language standards. ANSI C requires you to declare all of your variables at the top of a function, you cannot define them in the middle of a function just before they are used. So try rewriting your code like this:
void my_function()
{
/* Variable declarations go here */
int i;
...
/* Now teh codez */
for (i = 0; i < unpacked_length; i++)
{
...
}
}
对于第二个错误,智能感知抱怨它不能一个指针转换为为const char
的指针 const的无符号字符
。字符串文字具有类型的char []
(字符数组)和字符
默认情况下签署的。所以,你应该声明的变量类型为const char *
而不是 const的无符号字符*
。或者,你可以忽略它,因为它不是一个编译器错误,但它是在一定的警告级别警告。
For the second error, Intellisense complaining that it can't convert a pointer to const char
to a pointer to const unsigned char
. String literals have type char[]
(array of characters), and char
is signed by default. So, you should declare your variable as type const char *
instead of const unsigned char *
. Or, you can ignore it, since it's not a compiler error, but it is a warning at certain warning levels.
这篇关于C程序不会在Visual Studio 2010编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!