C - weird syntax error - c

I'm writing an application in ANSI C (Visual Studio 2010)
my library looks like this:
#include <stdio.h>
#include <stdlib.h>
#ifndef _MYLIB_
#define _MYLIB_
typedef enum {false, true} bool;
// some structures and function prototypes...
#endif
I include this library in every .c file (I've got like 4 .c files - 1 main.c with main() and the rest with functions).
I get an error:
Generating Code...
1> first.cpp
1>c:\users\A1\documents\visual studio 2010\projects\pr1\pr1\mylib.h(7): error C2059: syntax error : 'constant'
1>c:\users\A1\documents\visual studio 2010\projects\pr1\pr1\mylib.h(7): error C2143: syntax error : missing ';' before '}'
1>c:\users\A1\documents\visual studio 2010\projects\pr1\pr1\mylib.h(7): error C2059: syntax error : '}'
1> second.cpp
1>c:\users\A1\documents\visual studio 2010\projects\pr1\pr1\mylib.h(7): error C2059: syntax error : 'constant'
1>c:\users\A1\documents\visual studio 2010\projects\pr1\pr1\mylib.h(7): error C2143: syntax error : missing ';' before '}'
1>c:\users\A1\documents\visual studio 2010\projects\pr1\pr1\mylib.h(7): error C2059: syntax error : '}'
What's more - when I paste all functions and structures into main.c - it works properly...
I have no idea how to fix it...

I strongly believe that you are interfering with the C++ built-in false and true, so you are trying to redefine them in your enum.
Try to replace false and true with FALSE and TRUE and bool with BOOL.
#include <stdio.h>
#include <stdlib.h>
#ifndef _MYLIB_
#define _MYLIB_
typedef enum {FALSE, TRUE} BOOL;
// some structures and function prototypes...
#endif

Related

X509_NAME macro in C won't compile

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.

Passing a struct as an array and storing

My phase looks like this :
enum Gait_cycle_states {
HEEL_STRIKE,
FLAT_FOOT,
MID_STANCE,
HEEL_OFF,
TOE_OFF,
MID_SWING
};
My struct looks like this :
struct State {
Gait_cycle_states current_state;
uint8_t next_state; //index of the next state
uint8_t ay_num_criteria;
State_criterion *ay_criteria_for_state;
unsigned int gy_num_criteria;
State_criterion *gy_criteria_for_state;
};
and my function looks like this :
void initialize_FSM(State states_array[]){
//Heel strike----------------------------------------------------------
State heel_strike = {
.current_state = HEEL_STRIKE,
.next_state = 1, //the index of the next state
.ay_num_criteria = 1,
.ay_criteria_for_state = new State_criterion[1],
.gy_num_criteria = 2,
.gy_criteria_for_state = new State_criterion[2]
};
//The features to look for in accel_y and gyro_y, and how old they can be to count (in ms)
heel_strike.ay_criteria_for_state[0] = { NO_FEATURE, 150 };
heel_strike.gy_criteria_for_state[0] = { NEGATIVE_TROUGH, 150 };
heel_strike.gy_criteria_for_state[1] = { BREACHED_HIGH_THRESHOLD, 30 };
//putting the state we just configured into the state array
states_array[0] = heel_strike;
};
It seems Visual studio 2015 is not letting me assign the value to my types using "." or in the fashion, I am doing it.
It is throwing me a bunch or error and warnings like this :
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(123): error C2059: syntax error: '.'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(131): error C2143: syntax error: missing ';' before '}'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C3927: '->': trailing return type is not allowed after a non-function declarator
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C3484: syntax error: expected '->' before the return type
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C3613: missing return type after '->' ('int' assumed)
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C2146: syntax error: missing ';' before identifier 'ay_criteria_for_state'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C2143: syntax error: missing ';' before '{'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C3927: '->': trailing return type is not allowed after a non-function declarator
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C3484: syntax error: expected '->' before the return type
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C3613: missing return type after '->' ('int' assumed)
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C2086: 'int heel_strike': redefinition
1> c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): note: see declaration of 'heel_strike'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C2146: syntax error: missing ';' before identifier 'gy_criteria_for_state'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C2143: syntax error: missing ';' before '{'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C3927: '->': trailing return type is not allowed after a non-function declarator
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C3484: syntax error: expected '->' before the return type
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C3613: missing return type after '->' ('int' assumed)
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C2086: 'int heel_strike': redefinition
1> c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): note: see declaration of 'heel_strike'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C2146: syntax error: missing ';' before identifier 'gy_criteria_for_state'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C2143: syntax error: missing ';' before '{'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C2447: '{': missing function header (old-style formal list?)
could someone please suggest me what could be done?
State heel_strike = {
HEEL_STRIKE,
1, //the index of the next state
1,
new State_criterion[1],
2,
new State_criterion[2]
};
What you are trying to use is known as "designated initializers". They are a feature of C language (since C99), not part of standard C++. Some compilers support it for C++ as an extension, but MSVC does not.

Error C2061: syntax error: identifier BLOBHEADER though wincrypt.h is included

I have a struct in C, but I'm encountering a syntax error stating:
C:\sv62w8\pc\sales\src\main\custom.c(2179) : error C2061: syntax error : identifier 'BLOBHEADER'
Here's my code:
sample.c
#include <windows.h>
#include <wincrypt.h>
//...more codes
struct ENCRYPT_KEY_STRUCT_TYPE {
BLOBHEADER sample_variable;
} EncryptKeyStruct;
//...more codes
By the way, I'm using Microsoft Visual C++.
Thanks!

typedef definition not found

I can't figure why the following code gives compilation error as if the typedef definition not found. In fact, if I add the line 'typedef TCHAR my_tchar;' (which is already in win32def.h) in app.h the compilation goes well.
win32def.h
#ifndef win32def_h
#define win32def_h
#include <tchar.h>
typedef TCHAR my_tchar;
#endif
app.h
#include "win32def.h"
int my_function(const my_tchar *filename, ....)
compilation error at line of my_function:
error C2143: syntax error : missing ')' before '*'
what the compiler is used?
I'm not sure, but try to turn on the option "treat wchar_t as built-in type".

C visual studio undeclared identifier for define

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.

Resources