I have been trying to compile C source code in visual studio 2010. While compiling i am getting some errors like:
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\winnt.h(4277): error C2040: 'CONTEXT' : '_CONTEXT' differs in levels of indirection from 'binding *'
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\winuser.h(5341): error C2365: 'INPUT' : redefinition; previous definition was 'enumerator'
1>c:\documents and settings\xyz\desktop\abc\clause.h(72) : see declaration of 'INPUT'
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(16): error C2011: 'timeval' : 'struct' type redefinition
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\WinSock2.h(176) : see declaration of 'timeval'
I searched for those error and but some hints that it is due to the change in order of the header files. But could not get the exact answer. May I know , How can I overcome this problem?
Following is the order of header file inclusions.
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
Thanks in advance.
See here - if you include windows.h explicitly with winsock2.h you have to add a #define WIN32_LEAN_AND_MEAN to make this work.
For historical reasons, the Windows.h
header defaults to including the
Winsock.h header file for Windows
Sockets 1.1. The declarations in the
Winsock.h header file will conflict
with the declarations in the
Winsock2.h header file required by
Windows Sockets 2.0. The WIN32_LEAN_AND_MEAN macro prevents the Winsock.h from being included by the Windows.h header.
Related
I am new to programming and picked up the C language as my first computer language. I was doing a sample conversion program and when i compiled the code, i get the following error.
What does it mean and how do i fix it?
Thanks!
1>------ Build started: Project: ConversionProgram, Configuration: Debug Win32 ------
1>Build started 24-07-2012 20:58:37.
1>InitializeBuildStatus:
1> Touching "Debug\ExcerciseProgram1.unsuccessfulbuild".
1>ClCompile:
1> conversion.cpp
1>c:\my documents\visual studio 2010\projects\excerciseprogram1\excerciseprogram1\question2.cpp(12): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\my documents\visual studio 2010\projects\excerciseprogram1\excerciseprogram1\question2.cpp(18): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>c:\my documents\visual studio 2010\projects\excerciseprogram1\excerciseprogram1\question2.cpp(32): error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source file
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.1
I am using VB 2010 as my IDE
The reason why you are experiencing a compile error is mentioned here:
error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source
This means that the compiler is making the inclusion of StdAfx.h compulsory. You can remedy this by adding #include <StdAfx.h> to your source code.
It seems that your source file is not including the precompiled header file. All source files must, as first non-comment, include "stdafx.h" if you are using precompiled headers.
I've been trying to learn C, and I'm stuck on including libraries. I need to use strcpy(), but that method is included in the iostream library, but whenever I try to include the library, the program gives me errors. I've tried using "iostream", "iostream.h", , , but it either gives me a "can't find iostream.h" error, or the program exceeds 100 errors and just crashes. Even if my code is empty, I still get the same thing. Here's the code:
#include "iostream"
int main(void)
{
}
Yup, just that much makes it crash already. And here's a part of the errors I'm getting (could never paste them all here):
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(37): error C2061: syntax error : identifier 'abs'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(37): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2061: syntax error : identifier 'acos'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2061: syntax error : identifier 'asin'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'atan'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'atan2'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'ceil'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(41): error C2061: syntax error : identifier 'cos'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(41): fatal error C1003: error count exceeds 100; stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So yeah it even exceeds the 100 errors and the program just stops counting. I don't understand why, I'm just including a regular library. Is there an equivalent of strcpy()? I mainly wanted to use it like this (for practice):
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
#include "iostream"
int main(void)
{
struct person
{
int id;
char name[50];
int age;
};
struct person p1;
p1.id = 5595;
strcpy(p1.name, "Myname");
p1.age = 18;
printf("%d%s%d", p1.id, p1.name, p1.age);
}
<iostream> is a C++ header (it deals with input/ouput streams, as the name implies). If you want strcpy, you need <string.h>.
If your source file is ".c", all you have to do is to rename it ".cpp".
Then it'll compile as C++, you'll have the C++ headers, and you'll be able to use C++ streams.
I don't see any need for iostreams, however.
Strcpy and friends are in "<string.h>". Just include it, and "stdio.h" (like you're doing); delete the "iostreams" #include ... and life should be good.
iostreams are a C++-only feature. The iostreams header file is written in C++, not C. (Yes, they are different languages!) Presumably, you're invoking the compiler in C mode, so when the compiler looks at the header file it of course throws lots of errors since many of the constructs used in iostream makes sense only in C++ mode.
If you want to use iostreams, you have to compile in C++ mode (and code in proper, modern C++ accordingly), use a different library that's C-only or work around it by implementing your own code as necessary.
In this case, all you apparently want to do is to use strcpy(). That is declared in the string.h, not iostream. (string.h is a C header file.) Just #include <string.h> and it should compile.
I'm looking for a header file on windows that I can use to get the ntohl function in C, but Winsock2.h will not work for me. Does anybody know what other header files would provide this?
Here are some errors I get when I try to include Winsock2.h
C:\Program Files (x86)\PellesC\Include\Win\Winsock2.h(1045): error #2120: Redeclaration of 'getservbyport', previously declared at C:\Program Files (x86)\PellesC\Include\Win\winsock.h(468); expected 'PSERVENT __stdcall function(int, const char *)' but found 'struct servent * __stdcall function(int, const char *)'.
C:\Program Files (x86)\PellesC\Include\Win\Winsock2.h(135): error #2123: Redefinition of 'hostent', previously defined at C:\Program Files (x86)\PellesC\Include\Win\winsock.h(91).
C:\Program Files (x86)\PellesC\Include\Win\ws2def.h(39): error #1050: Redefinition of macro 'AF_IPX'.
C:\Program Files (x86)\PellesC\Include\Win\Winsock2.h(1310): error #2121: Redeclaration of 'LPSOCKADDR_IN'.
It's in winsock2.h as per the MSDN docs and, from a development system of mine (in C:\Program files (x86) Microsoft SDKs\Windows\v7.0A\Include\Winsock2.h):
WINSOCK_API_LINKAGE u_long WSAAPI ntohl (__in u_long netlong);
I'm not sure why that's "not working" for you. If you're getting an error message, you need to post it.
Based on your question edits on why you're having trouble with winsock2.h, it appears you're trying to include both winsock.h and winsock2.h - that's rarely a good idea :-) They're actually fundamentally incompatible, winsock2 is a later version with extra goodies.
Unfortunately, it's not always under your obvious control since windows.h automagically include winsock.h for you under most circumstances.
From memory, one way around this is to define WIN32_LEAN_AND_MEAN before including windows.h. This will prevent some of the lesser-used headers from being included.
Another is to define _WINSOCKAPI_ before including windows.h - this will prevent winsock.h from being included as well.
The third (and prbably preferable) way is simply to include winsock2.h before windows.h. winsock2.h defines _WINSOCKAPI_ so has the same effect as the previous paragraph.
I solved my own problem eventually. I had neglected to download the windows software development kit and add the include directories etc. in my project. I had assumed that my compiler came with all of the libraries and headers for sockets.
The error shown:
Error 11 error C2664: '_vswprintf_c_l' : cannot convert parameter 4 from 'void *' to '_locale_t' C:\Program Files\Microsoft Visual Studio 8\VC\include\swprintf.inl 41
It locates the file- C:\Program Files\Microsoft Visual Studio 8\VC\include\swprintf.inl which is a system file I guess. So, how to resolve?
Platform: Visual Studio 2005
Version 8.0.50727.762
I've also seen this issue in a code I was dealing with.
The problem was that stdlib.h was being included after a local header which probably was including some other c or c++ header.
wrong order:
#include "someheaderofmine.h"//includes several other headers
#include <stdlib.h>
just reversing the include order fixed my problem:
#include <stdlib.h>
#include "someheaderofmine.h"
seems like the same problem can occur if you are using string.h
In my case, it was using in C++ code a legacy C header containing #define NULL ((void *)0) in some legacy C header. My error message was "C2664 ...cannot convert argument 3 from void * to const_locale_t". The argument in question was NULL. Normally NULL is defined inside vcruntime.h (part of Visual C++). Using the custom NULL before any code that depends on vcruntime.h, like string.h, stdio.h, caused this error. Removing our custom definition or changing it to the following solved the problem.
#ifndef NULL
#ifdef __cplusplus
/*C++ NULL definition*/
#define NULL 0
#else
/*C NULL definition*/
#define NULL ((void *)0)
#endif
#endif
I get a ton of errors in cstdio when I add #include <cstdio>
to the C program.
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17) : error C2143: syntax error : missing '{' before ':'
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17) : error C2059: syntax error : ':'
Thanks
EDIT - I would like to use snprintf, which is why I am trying to include this.
You want #include <stdio.h>. cstdio is the C++ wrapper for the C header.
Edit: MSVC only supports the elements in C99 that form a subset of C++.
This site has a C implementation of snprintf() licensed under the GPL.
With Visual Studio, I believe you have to use sprintf_s or something similar. See this. There's also vsnprintf.
MSVC offers the _snprintf function in stdio.h.
If you prefer not to use the leading underscore, you can:
#include <stdio.h>
#define snprintf _snprintf
This is a C library function, not specifically related to C++ (although you can use it there too).