I am writing a program in C that uses winsock and I am using the command fcntl to make the receive call nonblocking and I am getting the following errors.
warning C4013: 'fcntl' undefined; assuming extern returning int
error C2065: 'F_SETFL' : undeclared identifier
error C2065: 'F_GETFL' : undeclared identifier
error C2065: 'F_SETFL' : undeclared identifier
error C2065: 'O_NDELAY' : undeclared identifier
error C2065: 'EWOULDBLOCK' : undeclared identifierenter code here
I am including the winsock2.h header file in my code as follows
#pragma comment(lib,"ws2_32.lib")
#include <winsock2.h>
Please help me out.
Thanks in advance.
I think on Windows you need to use ioctlsocket rather than fcntl().
To make non-blocking:
unsigned long on = 1;
if (0 != ioctlsocket(socket_fd, FIONBIO, &on))
{
/* Handle failure. */
}
To make blocking:
unsigned long off = 0;
if (0 != ioctlsocket(socket_fd, FIONBIO, &off))
{
/* Handle failure. */
}
Instead of EWOULDBLOCK use WSAEWOULDBLOCK.
Related
I'm trying to compile some old C code from a decade ago and am getting errors for the sigset_t type. I've researched the issue on the net and here on SO without success. I also tried adding #define _POSIX_C_SOURCE too, but all without success. Maybe something has been deprecated since the code was working and has a new replacement now.
I'm wondering if someone can tell me what to do to fix the issue? Thank you
Here's is what it looks like on my end:
I set my command line environment for compiling 64bit by running the usual batch file (vcvars64.bat)
"c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
I am using this cl.exe compiler:
c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\HostX64\x64\cl.exe
C:\ufo-dev\code>make all
cl /c /Od /nologo /MT /I. "-I..\s" "-Ic:\ufo-dev\include" ..\s\spool.c
spool.c
..\s\spool.c(85): error C2065: 'sigset_t': undeclared identifier
..\s\spool.c(85): error C2146: syntax error: missing ';' before identifier 'set'
..\s\spool.c(85): error C2065: 'set': undeclared identifier
..\s\spool.c(86): error C2065: 'sigset_t': undeclared identifier
..\s\spool.c(86): error C2146: syntax error: missing ';' before identifier 'old'
..\s\spool.c(86): error C2065: 'old': undeclared identifier
..\s\spool.c(298): error C2065: 'set': undeclared identifier
..\s\spool.c(299): error C2065: 'set': undeclared identifier
..\s\spool.c(300): error C2065: 'set': undeclared identifier
..\s\spool.c(300): error C2065: 'SIGHUP': undeclared identifier
..\s\spool.c(301): error C2065: 'set': undeclared identifier
..\s\spool.c(301): error C2065: 'SIGQUIT': undeclared identifier
..\s\spool.c(302): error C2065: 'set': undeclared identifier
..\s\spool.c(303): error C2065: 'SIG_BLOCK': undeclared identifier
..\s\spool.c(303): error C2065: 'set': undeclared identifier
..\s\spool.c(303): error C2065: 'old': undeclared identifier
..\s\spool.c(338): error C2065: 'SIG_UNBLOCK': undeclared identifier
..\s\spool.c(338): error C2065: 'set': undeclared identifier
..\s\spool.c(338): error C2065: 'old': undeclared identifier
make: *** [spool.obj] Error 2
/* typedefs in an include file that is included */
mytypedefs.h:
#define dcl_sigsett(x) sigset_t x
spool.c:
#define _POSIX_SOURCE /* enables POSIX compiler options */
#define _POSIX_C_SOURCE /* enables POSIX compiler options */
#include<mytypedefs.h>
#include <signal.h> /* signals */
spool.c(85):
dcl_sigsett (set); /* set of signals to block */
dcl_sigsett (old); /* set of signals to block */
spool.c(298):
/* : - block signals while rewriting the mailbox! */
sigemptyset (&set);
sigaddset (&set, SIGINT);
sigaddset (&set, SIGHUP);
sigaddset (&set, SIGQUIT);
sigaddset (&set, SIGTERM);
sigprocmask (SIG_BLOCK, &set, &old);
This is a silly compilation error, but for the life of me I can't find out what's wrong. I have spent hours on it, but I have made no progress at all. And I certainly don't understand enough about OpenSSL to understand what an X509_NAME is.
I'm compiling a small C file whose function it is to print out an error message if anyone calls one of the 30 or so functions in the file, to indicate that the 30 SSL functions are not supported by the C program. Each function is about 3 lines long (see below). The calling interface is copied from the OpenSSL openssl/X509.h file for win32. (I'm running Windows10 x64, but compiling with VStudio 2017 command line set up for 32bits).
Here is the original interface from the openssl/x509.h include file:
int X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len);
Since the original x509.h include file says inside that "the definition for X509_NAME on Win32 is located in wincrypt.h," I copied the definition for the X509_NAME macro directly into my source file as shown below.
The code for my simple error message function follows. I just copied the interface definition from the openssl/x509.h file and made it into a function:
#define X509_NAME ((LPCSTR) 7) /* copied from wincrypt.h */
int X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len) {
print_error (E_NO_SSL_SUPPORT);
return (-1);
}
When I try to compile the code with the VS 2017 command line compiler for 32bits, I get these error messages. Line 236 contains the troublesome code line int X509_NAME_....
cl /c /Od /nologo /MT /I. "-I..\s" ..\s\stubssl.c
stubssl.c
..\s\stubssl.c(236): error C2143: syntax error: missing ')' before '('
..\s\stubssl.c(236): error C2091: function returns function
..\s\stubssl.c(236): error C2059: syntax error: ')'
..\s\stubssl.c(236): error C2143: syntax error: missing ')' before 'constant'
..\s\stubssl.c(236): error C2143: syntax error: missing '{' before 'constant'
..\s\stubssl.c(236): error C2059: syntax error: 'constant'
..\s\stubssl.c(242): error C2059: syntax error: '<parameter-list>'
c:\stubssl\winxp\make.exe: *** [stubssl.obj] Error 2
I get the same kind of error with another function that is a bit simpler (but that also uses the X509_NAME macro). It starts on line 242 (see the error messages above).
X509_NAME *X509_get_subject_name(X509 *a) {
hio_oerror (E_NO_SSL_SUPPORT);
return (NULL);
}
Does anyone have any idea how I might try to resolve this problem? It seems so simple, yet it has been very elusive for me to solve. I can't imagine how I can be missing ')' before '(' in those lines.
EDIT: My include files in stubssl.c:
#define WIN32_LEAN_AND_MEAN
#include "openssl/ssl.h"
#include "openssl/x509.h"
#include "openssl/bio.h"
#include "openssl/pem.h"
In openSSL, X509_NAME is a type, but <wincrypt.h> defines it as a value (((LPCSTR) 7)). Don't define it yourself, but use the openSSL header, don't include <wincrypt.h> and #define WIN32_LEAN_AND_MEAN to avoid it being included through other windows headers.
nb: This question has been reduced many times, due to comments.
Below now is presented minimum amount of code which generated the error. inttypes.h file was downloaded from here: ffMPEG "inttypes.h not found" error), which was thought to be the issue in the beginning.
//tlvlist.c
static int32_t test(somestruct *a);
/* Private method, adds tlv object to the list which contains raw binary data. */
int32_t int32_t test(somestruct *a)
{
/* Some checks */
if(a == NULL || bytes == NULL)
return -1;
/* Check if list is full */
if(a->used == MAX_LIST_SIZE)
return -1;
/* Index to first free element in the list */
int iIndex = a->used;
// ...
return 0;
}
errors:
tlvlist.c
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(21): error C2143: syntax error : missing ';' before 'type'
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(23): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(24): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(28): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(29): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(32): error C2065: 'iIndex' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Evidently, when compiling a C file in MSVS you need to have all variable declarations at the start of the function before any statement. For example:
int32_t Tlvlist_AddRawt(Tlvlist *a, uint8_t type, uint16_t size, const void *bytes)
{
/* Index to first free element in the list */
int iIndex;
/* Some checks */
if(a == NULL || bytes == NULL)
return -1;
iIndex = a->used;
...
}
I believe this is old C89 format and most C compilers now use C99 (or greater) which would permit variable declarations anywhere in the function. Renaming the file as CPP is another option for MSVS without moving the variable declarations to the top of a function, though it may raise other issues in the code.
I made a number guessing game in C that compiles fine with GCC 4.7.1. But it gives a lot of error with VC2010. Mostly syntax error issues. I don't know why it gives these errors as I've compiled many other C sources with VC2010 without these errors.
I compile it with VC (Command Line) with the command: cl guessgame.c
VC gives the following errors:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for
Copyright (C) Microsoft Corporation. All rights reserved.
guessgame.c
guessgame.c(8) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(9) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(10) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(11) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(12) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(13) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(14) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(20) : error C2065: 'chances' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeX' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeY' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'chances' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(25) : error C2065: 'chances' : undeclared identifier
guessgame.c(25) : error C2065: 'i' : undeclared identifier
guessgame.c(26) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'randomNumber' : undeclared identifier
guessgame.c(30) : error C2065: 'chances' : undeclared identifier
guessgame.c(30) : error C2065: 'i' : undeclared identifier
guessgame.c(36) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(39) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(41) : error C2065: 'randomNumber' : undeclared identifier
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int randomNumber = (rand() % 245)+5;
int userAns;
int randRangeX = randomNumber-((rand() % 3)+5);
int randRangeY = randomNumber+((rand() % 3)+5);
int chances = 3;
int wrong_guesses = 0;
int i;
printf("Number guessing game v1.1.\n");
printf("Copyright(c) 2013 - Ahnaf Tahmid.\n");
printf("-----------------------------------\n\n");
printf("Rules: 1) Guess the number.\n");
printf(" 2) You have %d chances.\n\n", chances);
printf("The number is between %d and %d.", randRangeX, randRangeY);
for(i = chances; i >= 1; i--)
{
printf("\nGuess %d: ", (chances-i)+1);
scanf("%d", &userAns);
while(getchar() != '\n');
if(userAns == randomNumber)
{
printf("\nGood guess! You guessed it in %d turn(s).\n", (chances-i)+1);
break;
}
else
{
printf("Aww, bad guess.\n");
wrong_guesses += 1;
}
}
if(wrong_guesses == 3)
{
printf("\nGame Over!\n\nThe answer was %d.\n", randomNumber);
}
getch();
return 0;
}
VC2010 supports C89 which requires that variables be declared at the start of their scope block. You can build with both compilers if you move the srand call after your variable declarations and delay initialising the variables which depend on rand().
int main()
{
int randomNumber;
int userAns;
int randRangeX;
int randRangeY;
int chances = 3;
int wrong_guesses = 0;
int i;
srand(time(NULL));
randomNumber = (rand() % 245)+5;
randRangeX = randomNumber-((rand() % 3)+5);
randRangeY = randomNumber+((rand() % 3)+5);
In C89, all variables have to be declared in the beginning of a block. This has been removed in C99.
But VC2010 doesn't support C99. So you have to put
srand(time(NULL));
after all the declaration of variables.
Reference:
C89 §3.6.2 Compound statement, or block
Syntax
compound-statement:
{ declaration-list opt statement-list opt }
declaration-list:
declaration
declaration-list declaration
statement-list:
statement
statement-list statement
Note that in C89, declaration-list has to be before statement -list in a block.
C99 §6.8.2 Compound statement
Syntax
compound-statement:
{ block-item-list_opt }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
In C99, declaration and statement can be mixed.
An alternative approach to solve the issue pointed out by the other answers would be to open a new scope:
int main()
{
srand(time(NULL));
{
int randomNumber = (rand() % 245)+5;
int userAns;
int randRangeX = randomNumber-((rand() % 3)+5);
int randRangeY = randomNumber+((rand() % 3)+5);
int chances = 3;
int wrong_guesses = 0;
int i;
...
Hello i have the following code..
#define VB_CHAR signed char
#define VB_UCHAR unsigned char
#if defined(VBISAM_NO_CISAM_CONFLICT)
#include "vbisam_rename.h"
#endif
#include "vbdecimal.h"
vbdecimal doesnt include any other header but fails to see it
Error 1 error C2065: 'VB_CHAR' : undeclared identifier c:\users\parhs\documents\visual studio 2010\projects\testdll4\vbdecimal.h
I have to define VB_CHAR in vbdecimal in order to work..
Any work-around for this?
Well. It could be that vbisam_rename.h '#undefs' VB_CHAR.