This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does VS2010 give syntax errors when syntax is correct?
I'm trying to develop a kind of Windows 32 service in C language, using Visual Studio 2010.
I created a new project, and inserted .c files :
main.c
service.c
misc.c
I also have two header files :
myerrors.h
my.h
Here's the code I have (be aware that it's just a draft).
main.c :
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "stdafx.h"
#include "my.h"
#include "myerrors.h"
static int parse_args(int ac, char **av)
{
int i = 0;
while (++i < ac)
if (strcmp(av[i], "-i") && !InstallMyService())
return false;
else if (strcmp(av[i], "-d") && !UninstallMyService())
return false;
else if (strcmp(av[i], "-p"))
if (!av[i + 1])
return false;
else
{
if (!InsertPathInRegistry(av[i + 1]))
return false;
i++;
}
else
return false;
return true;
}
int main(int ac, char **av)
{
HANDLE hLogFile;
if ((hLogFile = CreateFile(LOG_FILE_PATH, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
aff_error(CANT_CREATE_FILE);
if (ac > 1)
{
if (!parse_args(ac, av))
{
aff_error(BAD_ARGUMENTS);
return EXIT_FAILURE;
}
}
else
{
SERVICE_TABLE_ENTRY DispatchTable[] = {{DC_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
getchar();
if (!CloseHandle(hLogFile))
aff_error(CLOSE_FILE_FAILED);
return EXIT_SUCCESS;
}
misc.c :
#include <Windows.h>
#include <stdio.h>
#include "my.h"
#include "myerrors.h"
void aff_error(char *error_str)
{
fprintf(stderr, "ERROR: %s\n", error_str);
}
bool InsertPathInRegistry(char *path)
{
printf("LOG: Inserting %s as ", path);
}
void WriteInLogFile(HANDLE hLogFile, char *log_string)
{
printf("WriteInLogFile function");
}
service.c :
#include <Windows.h>
#include "my.h"
bool InstallMyService()
{
return true;
}
bool UninstallMyService()
{
return true;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
}
void WINAPI ServiceMain(DWORD ac, LPTSTR *av)
{
}
My headers are just some function declarations and macros such as :
# define DC_SERVICE_NAME "MyService"
/* MISC functions */
void aff_error(char *error_str);
my.h
#ifndef _MY_H_
# define _MY_H_
#include <Windows.h>
#include <strsafe.h>
/* Macros */
# define LOG_FILE_PATH "c:\\my_log_file.txt"
# define DC_SERVICE_NAME "MyService"
/* MISC functions */
void aff_error(char *error_str);
/* SERVICE functions */
void WINAPI ServiceMain(DWORD ac, LPTSTR *av);
bool InstallMyService();
bool UninstallMyService();
bool InsertPathInRegistry(char *path);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
#endif /*!MY_H_ */
While trying to compile the project, i got some weird errors :
my.h(19): error C2061: syntax error : identifier 'InstallMyService'
my.h(19): error C2059: syntax error : ';'
my.h(19): error C2059: syntax error : ')'
Or :
my.h(21): error C2061: syntax error : identifier 'InsertPathInRegistry'
my.h(21): error C2059: syntax error : ';'
my.h(21): error C2059: syntax error : 'type'
I checked on some forums that says those errors are commonly errors with includes badly placed, but I don't really know in this case, I don't think I made mistakes with includes...
Can anyone illuminate me ?
Thanks.
bool is not a data type in ANSI C. It is a data type in the C99 version of the language, only if <stdbool.h> is included, but Visual Studio does not support C99, only C89 (C99 also adds the _Bool data type, which can be used without including any headers).
I suggest you replace bool with another type such as int, or use a typedef to alias it with int or unsigned char or something.
Related
I have this code for deleting directories. I have header1.h, header1.c, main.c.
I get some errors, but the one is more difficult to me to understand is the errors:
(1) storage size of ffblk isn't known.
Also, i have the doubt of how to define the attributes of ffblk, which are ff_name and ff_attrib
This example is from various code examples from the internet(even other from here), all of them do the same code, just in my case it does not work.
Am i missing the definition of the struct? or maybe i have added code where it should not be any code?
Could you please help me? I dont usually program in C. and i am using Dev-Cpp.
header1.h:
#ifndef HEADER1_H_INCLUDED
#define HEADER1_H_INCLUDED
typedef struct ffblk ffblk;
#endif
header.c:
#include <stdio.h>
#include <stdlib.h>
#include "header1.h"
struct ffblk
{
char ff_attrib[20]; //this line i added just so dont show error of unknown
char ff_name[20]; //this line i added just so dont show error of unknown
};
main.c:
#ifndef FA_RDONLY
#define FA_RDONLY _A_RDONLY
#endif
#ifndef FA_HIDDEN
#define FA_HIDDEN _A_HIDDEN
#endif
#ifndef FA_SYSTEM
#define FA_SYSTEM _A_SYSTEM
#endif
#ifndef FA_DIREC
#define FA_DIREC _A_SUBDIR
#endif
#ifndef FA_ARCH
#define FA_ARCH _A_ARCH
#endif
# include <stdio.h>
# include <dos.h>
# include <dir.h>
# include <io.h>
# include <conio.h>
#include "header1.h"
typedef struct ffblk ffblk;
int BorrarArchivo(char *nombarch)
{
printf("Borrando Archivo %s \n",nombarch);
remove(nombarch);
return 0;
}
int EliminarAtributo(char *nombarch,int atributo)
{
printf("Elimina Atributo %s %d\n",nombarch,atributo);
chmod(nombarch,atributo);
return 0;
}
int BorrarArbol(void)
{
struct ffblk ffblk;
int done,err;
err=0;
done=findfirst("*.*",&ffblk,FA_RDONLY|FA_HIDDEN|FA_DIREC|FA_ARCH|FA_SYSTEM);
while (!done)
{
if (FA_HIDDEN & ffblk.ff_attrib)
EliminarAtributo(ffblk.ff_name,FA_HIDDEN);
if (FA_SYSTEM & ffblk.ff_attrib)
EliminarAtributo(ffblk.ff_name,FA_SYSTEM);
if (FA_RDONLY & ffblk.ff_attrib)
EliminarAtributo(ffblk.ff_name,FA_RDONLY);
if (FA_ARCH & ffblk.ff_attrib)
err=BorrarArchivo(ffblk.ff_name);
else if (FA_DIREC & ffblk.ff_attrib)
{
if (ffblk.ff_name[0]!='.')
{
chdir(ffblk.ff_name);
err=BorrarArbol();
chdir("..");
if (!err)
printf("Removiendo %s\n",ffblk.ff_name);
rmdir(ffblk.ff_name);
}
}
else
err=BorrarArchivo(ffblk.ff_name);
if (err)
{
printf("Error en el borrado ... !"); return err;
}
done=findnext(&ffblk);
}
return 0;
}
int main (void)
{ int err=0;
char c;
printf("Esta seguro [ Si -> S , No ->otra tecla ] =>");
c=getchar();
if (!(c=='S' || c=='s')) return 0;
err=BorrarArbol();
if (err) printf("Error en el borrado ... !");
return err;
}
EDIT:
I found a definition of struct and paste it in header.h
typedef struct ffblk {
char lfn_magic[6]; /* LFN: the magic "LFN32" signature */
short lfn_handle; /* LFN: the handle used by findfirst/findnext */
unsigned short lfn_ctime; /* LFN: file creation time */
unsigned short lfn_cdate; /* LFN: file creation date */
unsigned short lfn_atime; /* LFN: file last access time (usually 0) */
unsigned short lfn_adate; /* LFN: file last access date */
char ff_reserved[5]; /* used to hold the state of the search */
unsigned char ff_attrib; /* actual attributes of the file found */
unsigned short ff_ftime; /* hours:5, minutes:6, (seconds/2):5 */
unsigned short ff_fdate; /* (year-1980):7, month:4, day:5 */
unsigned long ff_fsize; /* size of file */
char ff_name[260]; /* name of file as ASCIIZ string */
}ffblk;
now it shows error:
C:\Users\1\AppData\Local\Temp\cc89P309.o:Untitled3.c:(.text+0x8e): undefined reference to `findfirst'
C:\Users\1\AppData\Local\Temp\cc89P309.o:Untitled3.c:(.text+0x1d8): undefined reference to `findnext'
At the end, the problem was always the functions findfirst(a,b,c) and findnext(a,b,c). I had to change the functions and use their version with 2 parameters and not 3. I put all the code in one file with the includes, typedef struct and the code below.
I am working on making a boolean expression evaluator in lex and c, however I am having problem finding the error in my code.
When I run the code the parser.c file throws the error("expected end-of-file"). This means that the program is not reading the end of file character, and I cannot find where this is going wrong.
I have attached the problematic code below. If, to solve this issue you need to see some more of the code please let me know, and I will be happy to post them also. I have been stuck on this problem for several weeks not, any help would be greatly appreciated.
Lexer.h
#ifndef ____lexer__
#define ____lexer__
#include <stdio.h>
#define AND 258
#define OR 259
#define NOT 260
#define TRUE 261
#define FALSE 262
#define DONE 300
#define NONE (-1)
int lexan();
extern int value;
extern int lineNo;
extern char lexbuf[];
extern FILE *fileSource;
#endif /* defined(____lexer__) */
lexer.lex
%{
#include <ctype.h>
#include <unistd.h>
#include "lexer.h"
#include "error.h"
int value;
int lineNo;
%}
%option noyywrap
%%
['''\t']* {}
['\n']* { lineNo++; }
<<EOF>> {
return DONE;
}
"True" {return (TRUE);}
"False" {return (FALSE);}
"or" {return (OR);}
"and" {return (AND);}
"not" {return (NOT);}
.|\n {
value = NONE;
int temp = (int)(yytext[0]);
return (temp);
}
%%
int lexan()
{
yyin = fileSource;
int result = yylex();
return result;
}
parser.c
#include <stdlib.h>
#include "parser.h"
#include "lexer.h"
#include "error.h"
#include "interpreter.h"
static int lookahead;
static void stmts();
static void stmt();
static void assign();
static void expr();
static void match();
void parse()
{
lookahead = lexan();
stmts();
lookahead = lexan();
if(lookahead != DONE)
error("expected end-of-file");
}
static void stmts()
{
while (lookahead != DONE)
{
if(lookahead == AND || lookahead == OR || lookahead == NOT || lookahead == TRUE || lookahead == FALSE)
{
stmt();
}
else
break;
}
}
static void stmt()
{
switch (lookahead)
{
case AND:
emit(AND);
match(AND);
break;
case OR:
emit(OR);
match(OR);
break;
case NOT:
emit(NOT);
match(NOT);
break;
default:
assign();
}
}
static void assign()
{
switch (lookahead)
{
case TRUE:
emit(TRUE);
match(TRUE);
break;
case FALSE:
emit(FALSE);
match(FALSE);
default:
error("syntax error");
}
}
void match(int t)
{
if (lookahead == t)
{
lookahead = lexan();
}
else
error("syntax error");
}
Your immediate problem is that lookahead is already DONE if stmts() returns successfully. Calling yylex again after it hits EOF is undefined behaviour, but you really don't want to do the call because the logic is incorrect; lookahead hasn't yet been matched when stmts() returns so the caller should attempt to match it, not overwrite it with a new token.
Fixing that is the least of your problems, though. The logic of stmts and stmt are also wrong; you need to reread whatever guide/text you are using to write a recursive descent parser.
By the way, if you don't have an <<EOF>> rule, yylex will return 0 when it hits the end of input. You should consider making use of this behaviour rather than producing a custom return code.
In Command Windows, there is an error!
please see below!
In file included from lwIP/test/unit/lwip_unittests.c:1:0:
lwIP/test/unit/lwip_check.h:7:19: fatal error: check.h: No such file or directory
compilation terminated.
make: *** [obj/lwIP/test/unit/lwip_unittests.o] Error 1
I am using an Sourcery_2011_09_ARM_EABI.
But in this files there isn't header file names check.h
In the file lwip_unittests.c:
#ifndef __LWIP_CHECK_H__
#define __LWIP_CHECK_H__
/* Common header file for lwIP unit tests using the check framework */
#include <sys/config.h>
#include <check.h>
#include <stdlib.h>
#define FAIL_RET() do { fail(); return; } while(0)
#define EXPECT(x) fail_unless(x)
#define EXPECT_RET(x) do { fail_unless(x); if(!(x)) { return; }} while(0)
#define EXPECT_RETX(x, y) do { fail_unless(x); if(!(x)) { return y; }} while(0)
#define EXPECT_RETNULL(x) EXPECT_RETX(x, NULL)
/** typedef for a function returning a test suite */
typedef Suite* (suite_getter_fn)(void);
/** Create a test suite */
static Suite* create_suite(const char* name, TFun *tests, size_t num_tests, SFun setup, SFun teardown)
{
size_t i;
Suite *s = suite_create(name);
for(i = 0; i < num_tests; i++) {
/* Core test case */
TCase *tc_core = tcase_create("Core");
if ((setup != NULL) || (teardown != NULL)) {
tcase_add_checked_fixture(tc_core, setup, teardown);
}
tcase_add_test(tc_core, tests[i]);
suite_add_tcase(s, tc_core);
}
return s;
}
#endif /* __LWIP_CHECK_H__ */
I need a file check.h, where can i get this?
And if check.h will be implemented in the file Sourcery_2011_09_ARM_EABI, will it work?
Or do i need to change check.h to tree-check.h or something other named, that is implement in the Sourcery_2011_09_ARM_EABI?
You'll probably need to install the check unit testing framework: http://check.sourceforge.net/
I'm having a really hard time adjusting function to my needs. First of all look at those three files and notice how I have to call f_texture function in main function in order to make it work:
externs.h
#ifndef EXTERNS_H_
#define EXTERNS_H_
extern char t_about[100];
extern int friction;
extern int f_texture(char* ,char*);
#endif
functionA.c
#include <stdio.h>
#include "externs.h"
int main()
{
f_texture("rough","friction");
printf("Friction: %d\n", friction);
f_texture("rough","t_about");
return 0;
}
functionB.c
#include "externs.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
char t_about[100];
int friction;
int f_texture(char* texture,char* what_is_needed)
{
/*Checking if both values are present*/
assert(what_is_needed);
assert(texture);
/*Static array in order to prevent it's disappearance*/
memset(t_about, '\0', sizeof(t_about));
/*Configuring variables for desired texture*/
if (strcmp(texture, "smooth") == 0)
{
strcpy(t_about, "The surface is perfectly smooth, without any "
"protuberances.\n");
friction = 0;
}
else if (strcmp(texture, "rough") == 0)
{
strcpy(t_about, "Rough bumps can be feeled under my fingertips.\n");
friction = 4;
}
/*In case of absent keyword of desired texture it will crash the program*/
else
{
assert(!what_is_needed);
}
/*Returning desired value*/
if (strcmp(what_is_needed, "t_about") == 0)
{
int i=0;
while (t_about[i] != '\0')
{
printf("%c", t_about[i]);
i++;
}
}
else if (strcmp(what_is_needed, "friction") == 0)
{
return friction;
}
/*In case of absent keyword of desired value it will crash the program*/
else
{
assert(!what_is_needed);
}
return 0;
}
And now here is my question: How to rewrite this code to make it possible to call f_texture function without using quotation marks inside? I mean instead of f_texture("abcd","efgh") just to type f_texture(abcd,efgh). I've noticed that this way it's required just after I've wrote this code.
Thanks in advance.
If you don't want to assign string constants to variables or preprocessor object macros, another option is to use preprocessor function macros, using the stringification feature:
#define call_f_texture(a,b) f_texture(#a,#b)
....
call_f_texture(rough,friction);
The C preprocessor will turn this into
f_texture("rough","friction");
You can also use some macros:
#define ROUGH "rough"
#define FRICTION "friction"
#define T_ABOUT "t_about"
int main()
{
f_texture(ROUGH, FRICTION);
printf("Friction: %d\n", friction);
f_texture(ROUGH, T_ABOUT);
return 0;
}
You can do like this,
char rough[]="rough";
char friction[]= "friction";
and call
f_texture(rough, friction);
char a[MAX] = "rouch";
char b[MAX} = "friction";
int main()
{
f_texture();
...
}
int f_texture()
{
/*Checking if both values are present*/
assert(b);
assert(a);
}
or
int f_texture(char* a,char* b)
{
/*Checking if both values are present*/
assert(b);
assert(a);
...
}
int main()
{
char a[MAX] = "rouch";
char b[MAX} = "friction";
f_texture(a,b);
...
}
1>cb.c(51): error C2061: syntax error : identifier 'SaveConfiguration'
1>cb.c(51): error C2059: syntax error : ';'
1>cb.c(51): error C2059: syntax error : 'type'
1>cb.c(52): error C2061: syntax error : identifier 'LoadConfiguration'
1>cb.c(52): error C2059: syntax error : ';'
1>cb.c(52): error C2059: syntax error : 'type'
1>cb.c(122): error C2061: syntax error : identifier 'SaveConfiguration'
1>cb.c(122): error C2059: syntax error : ';'
1>cb.c(122): error C2059: syntax error : 'type'
1>cb.c(127): error C2061: syntax error : identifier 'LoadConfiguration'
1>cb.c(127): error C2059: syntax error : ';'
1>cb.c(127): error C2059: syntax error : 'type'
1>
1>Build FAILED.
It's just a single .c file in the project. Here's the code:
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <tchar.h>
typedef struct _Configuration
{
int KeyActivate;
int BlockWidth;
int BlockHeight;
double HueStart;
double HueEnd;
double SaturationStart;
double SaturationEnd;
double ValueStart;
double ValueEnd;
} Configuration;
typedef struct _DIBSection
{
HDC ScreenDC;
HDC WindowDC;
HDC MemoryDC;
HBITMAP ScreenBMPHandle;
BITMAP ScreenBMP;
} DIBSection;
typedef struct _Thread
{
HANDLE Handle;
unsigned Id;
} Thread;
typedef struct _Window
{
HANDLE Handle;
HDC DC;
int Width;
int Height;
int Top;
int Left;
} Window;
__declspec ( dllexport ) int Initialize ( void );
unsigned __stdcall Start ( void * Arguments );
void LoadDefaultConfiguration ( Configuration * Config );
bool SaveConfiguration ( Configuration * Config, LPTSTR FilePath );
bool LoadConfiguration ( Configuration * Config, LPTSTR FilePath );
Thread MainThread;
Window Screen;
Configuration Settings;
BOOL WINAPI DllMain ( HINSTANCE Instance, DWORD Reason, LPVOID Reserved )
{
switch ( Reason )
{
case DLL_PROCESS_ATTACH:
// TODO: Load settings from file
LoadDefaultConfiguration ( & Settings );
// Create main thread
MainThread.Handle = (HANDLE) _beginthreadex (
NULL,
0,
Start,
NULL,
0,
& MainThread.Id
);
if ( MainThread.Handle )
{
SetThreadPriority ( MainThread.Handle, THREAD_PRIORITY_BELOW_NORMAL );
}
else
{
MessageBox ( NULL, L"Unable to create main thread; exiting", L"Error", MB_OK );
ExitProcess ( 0 );
}
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
__declspec ( dllexport ) int Initialize ( void )
{
return 1;
}
unsigned __stdcall Start ( void * Arguments )
{
return 1;
}
void LoadDefaultConfiguration ( Configuration * Config )
{
Config->BlockHeight = 50;
Config->BlockWidth = 100;
Config->HueEnd = 0.00;
Config->HueStart = 0.00;
Config->KeyActivate = VK_SHIFT;
Config->SaturationEnd = 0.00;
Config->SaturationStart = 0.00;
Config->ValueEnd = 0.00;
Config->ValueStart = 0.00;
}
bool SaveConfiguration ( Configuration * Config, LPTSTR FilePath )
{
return true;
}
bool LoadConfiguration ( Configuration * Config, LPTSTR FilePath )
{
return true;
}
Line 51 is
bool SaveConfiguration ( Configuration * Config, LPTSTR FilePath );
bool is not a C type.
I do suspect BOOL is defined somewhere.
Same goes for the usage of true and false.
Actually, bool is a valid type (well, a macro actually) in the C99 standard, assuming you are using a recent compiler. You need to add:
#include <stdbool.h>
Note that bool is not valid in the older ANSI, C89, C90 etc variants of the C standards.
As highlighted by JeremyP in the comments, Microsoft's C compiler still lacks proper support for C99 features.
Which leaves three alternatives:
Treat it as C++, not C; because C++ has bool as a built-in type
Create your own bool implementation
Re-write the code to avoid using bool
For option 2 something like this would work, but it's an ugly work-around:
typedef short bool;
#define true 1
#define false 0