4

I am trying to compile c code in VS C++ express 2010 but I get the following error trace:

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

When I try and compile with cpp extension I get this error trace:

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 ==========
Richard
  • 15,152
  • 31
  • 85
  • 111
  • Your errors when compiling with a .cpp extension indicate that you are mixing up `wchar_t` and `char`. Even if you compile this file as a C file, you'll need to fix those errors. – James McNellis May 19 '11 at 17:17

3 Answers3

5

/clr means use the .Net runtime - not the c language runtime!

Just call the file .c and it will work, there is a flag to stop you using any c++ features.

Go to properties -> c/c++ ->advanced -> compile as and select 'c'

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • I did as you suggested and I still am getting Command line error D8045: cannot compile C file 'test4.c' with the /clr option – Richard May 19 '11 at 17:03
  • 1
    remove the /clr optiuon - it's for MSFT's strange mixture of c++ with C#/.net – Martin Beckett May 19 '11 at 18:17
  • Note that by default the Visual C++ compiler compiles files with a .c extension as C and files with a .cpp extension as C++. You shouldn't need to change this setting if you use those two standard extensions. – James McNellis May 20 '11 at 16:51
  • @James - I think the compile-as-c-flag warns on things like "//" comments even with a .c file (been a few versions since I used it) – Martin Beckett May 20 '11 at 16:52
4

When you create your project, make sure you create it as a Win32 program and not a CLR application. This setting can be changed under project properties.

Propeties -> Common Language Runtime Support is the setting you are looking for. Set it to none.

Zeta Two
  • 1,776
  • 1
  • 18
  • 37
1

First, you should remove /clr, it's not what you're after.

To respond to the problem underlying the question: you are passing a char array to an LPWSTR and LPCWSTR and LPCTSTR, which are all double byte char arrays, so wchar_t* of some form or other (if UNICODE is defined, otherwise the last one is just plain char*-like).

You will either have to undo your UNICODE define (not recommended) or convert the char[] and char* to wchar_t based types

rubenvb
  • 74,642
  • 33
  • 187
  • 332