2011-05-19 112 views
4

我试图编译C代码VS C++快讯2010年,但我得到了下面的错误跟踪:如何编译在Visual C C++编写代码表示2010

1>------ Build started: Project: test4, Configuration: Release Win32 ------ 
1>cl : Command line error D8045: cannot compile C file 'test4.c' with the /clr option 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

当我试着和CPP扩展我得到编译此错误跟踪:

1>------ Build started: Project: test4, Configuration: Release Win32 ------ 
1> test4.cpp 
1>c:\documents and settings\rkelly1\desktop\io\test4\test4\pt_ioctl.c(86): error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR' 
1>   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 
1>c:\documents and settings\rkelly1\desktop\io\test4\test4\pt_ioctl.c(98): error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR' 
1>   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 
1>c:\documents and settings\rkelly1\desktop\io\test4\test4\pt_ioctl.c(138): error C2664: 'OpenServiceW' : cannot convert parameter 2 from 'const char [9]' to 'LPCWSTR' 
1>   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 
1>c:\documents and settings\rkelly1\desktop\io\test4\test4\pt_ioctl.c(196): error C2664: 'GetSystemDirectoryW' : cannot convert parameter 1 from 'CHAR [80]' to 'LPWSTR' 
1>   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 
1>c:\documents and settings\rkelly1\desktop\io\test4\test4\pt_ioctl.c(203): error C2664: 'lstrcatW' : cannot convert parameter 1 from 'CHAR [80]' to 'LPWSTR' 
1>   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 
1>c:\documents and settings\rkelly1\desktop\io\test4\test4\pt_ioctl.c(208): error C2664: 'CopyFile' : cannot convert parameter 1 from 'const char [13]' to 'LPCTSTR' 
1>   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 
1>c:\documents and settings\rkelly1\desktop\io\test4\test4\pt_ioctl.c(236): error C2664: 'CreateServiceW' : cannot convert parameter 2 from 'const char [9]' to 'LPCWSTR' 
1>   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 
1>test4.cpp(27): error C3861: 'kbhit': identifier not found 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+0

你的错误与.cpp扩展编译时说明您是混合了'wchar_t'和' char'。即使您将该文件编译为C文件,您也需要修复这些错误。 – 2011-05-19 17:17:26

回答

4

当您创建项目,请确保您创建为Win32程序,而不是一个CLR的应用程序。该设置可以在项目属性下更改。

Propeties - >通用语言运行时支持是您正在寻找的设置。 将其设置为无。

+0

谢谢,现在我知道clr代表什么:d – Richard 2011-05-19 17:30:20

5

/clr表示使用.Net运行时 - 不是c语言运行库!

只要调用文件.c,它会工作,有一个标志来阻止你使用任何C++功能。

转到属性 - > C/C++ - >高级 - >编译的,并选择“C”

+0

我按照你的建议做了,而且我仍然得到命令行错误D8045:无法用/ clr选项编译C文件'test4.c' – Richard 2011-05-19 17:03:14

+1

删除/ clr optiuon - 这是MSFT奇怪的C++与C#/。的混合使用网 – 2011-05-19 18:17:47

+0

请注意,默认情况下,Visual C++编译器会将.c扩展名为C的文件和扩展名为.cpp的文件编译为C++。如果您使用这两个标准扩展名,则不需要更改此设置。 – 2011-05-20 16:51:30

1

首先,你应该删除/clr,这不是你所追求的。

为了应对这一问题的问题底层:要传递一个char阵列到LPWSTR和LPCWSTR和LPCTSTR,它们都是双字节字符数组,所以某种形式或其他(的wchar_t*如果UNICODE被定义,否则最后一个只是简单的char*样)。

你将不得不取消您的UNICODE定义(不推荐)或转换char[]char*wchar_t基于类型