I am trying to read a key hit and then stop a code. In C.
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool starting()
{
char c;
if (kbhit())
{
c=getch();
if (c=="S"||c=="s")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
int main()
{
while(!starting)
{
printf("line 1");
delay(100);
}
return 0;
}
Without stdbool.h, it says errors like
syntax error: identifier 'starting',
syntax error: ";"
syntax error: ")"
'starting': undeclared identifier
With stdbool.h, it says file not found. My compiler is the one that comes with Visual Studio 2010.
Any suggestion how to remove this? How can I still use a function that returns a boolean value?
ADDED
sorry! for the short comment added. resolved mostly. Thanks all
Added
More Errors:
After Compiling: it reads:
filename.obj unresolved external symbol _delay referenced in function _main.
What should I do?
stdbool.h is introduced in C99, and Visual Studio doesn't support C99. You can define the types yourself. One possible way is:
typedef int bool;
#define true 1
#define false 0
Three problems at once:
C doesn't know bool as a type of its own, but you can define it (e.g. through stdbool.h or just by using a typedef to any other integral type (usually unsigned char or int; this might be a question of memory usage vs. performance based).
MSVC is known for not having all the std**.h headers, especially older versions. So you most likely just don't have stdbool.h in VS 2010 (the reason for the file not found error).
You're missing brackets in this expression: while(!starting).
Related
I am getting a really weird issue with my code.
I tried using clang and gcc, both tell me the same thing
init_twiddle.c:12:10: warning: implicitly declaring library function 'cosf' with type 'float (float)' [-Wimplicit-function-declaration]
.re = cosf(primitive_root*i) ,
^
init_twiddle.c:12:10: note: include the header <math.h> or explicitly provide a declaration for 'cosf'
init_twiddle.c:13:10: warning: implicitly declaring library function 'sinf' with type 'float (float)' [-Wimplicit-function-declaration]
.im = sinf(primitive_root*i)
^
init_twiddle.c:13:10: note: include the header <math.h> or explicitly provide a declaration for 'sinf'
2 warnings generated.
The code:
// file: init_twiddle.c
#include "complex.h"
#include <math.h>
void init_twiddle1024(Complex__complex* twiddle) {
int i,span ;
// Init the twiddles
for(span=1;span<=512;span<<=1) {
float primitive_root = -Complex__pi/span ;
for(i=0;i<span;i++) {
Complex__complex t =
{
.re = cosf(primitive_root*i) ,
.im = sinf(primitive_root*i)
} ;
twiddle[span+i] = t ;
}
}
}
// file: complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include "stdbool.h"
#include "assert.h"
//#include "pervasives.h"
typedef struct Complex__complex {
float re;
float im;
} Complex__complex;
static const float Complex__pi = 3.141593;
#endif // COMPLEX_H
The command I use to compile:
gcc -I. -I$(heptc -where)/c/ -std=c99 -c init_twiddle.c
I am working on a project with some strange programming language which explain all the included directories.
Does someone have any idea of why I am getting those errors?
PS: note that it's not a linker issue but an issue at compile time.
It also does not seem to appear when I manually write the content of complex.h into the file
It turned out Barmar was right. I was including a directory where a math.h already exists, thus leading to not including the libc one.
The faulty one was -I$(heptc -where)/c/ for those who would have the same issue with the Heptagon langage.
Thanks for your help.
As commented by #Barmar, the issue is that in $(heptc -where)/c/ there is already a math.h header defined which don't implement the function you want to use in this exemple.
Considering it's to compile with Heptagon I would advise to copy the only file that is really useful in this case from $(heptc -where)/c/ which is pervasives.h where you have your init_twiddle.c and since you are compiling with -I. it will then compile perfectly fine.
I am working on STM32F1 on IAR, I write a weak function using
__attribute__((weak))
main.c
#include "tmp.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int testfunc1(int a)
{
return true;
}
int main(void)
{
while (1)
{
}
}
tmp.h
#include <stdio.h>
#include <stdlib.h>
int testfunc1(int a);
tmp.c
#include "tmp.h"
__attribute__((weak)) int testfunc1(int a)
{
}
It compiles with errors:
Error[Pe079]: expected a type specifier
Warning[Pe606]: this pragma must immediately precede a declaration
Error[Pe260]: explicit type is missing ("int" assumed)
Error[Pe141]: unnamed prototyped parameters not allowed when body is present
Error[Pe130]: expected a "{"
Error while running C/C++ Compiler
However, if I use __weak instead of attribute((weak)), it works normally as expected.
tmp.c
#include "tmp.h"
__weak int testfunc1(int a)
{
}
.
Warning[Pe940]: missing return statement at end of non-void function "testfunc1"
Done. 0 error(s), 1 warning(s)
So, why is attribute((weak)) not working?
IAR compiler has its own extensions to archive it:
#pragma
__weak
I strongly suggest to put some effort and read the compiler documentation before posting questions here.
why __attribute__((weak)) in IAR can't compile?
why is attribute((weak)) not working?
Because it's not supported by the version of IAR compiler you are using.
I believe most "why" questions are bad question. An answer to "why" something happens is either too broad (requires to explain everything) or too vague (because this is how it is). In this case your compiler just doesn't support that specific syntax. To further investigate "why" exactly the IAR Systems company decided not to implement support for that particular syntax for that IAR compiler version, ask that company.
Some lines of my flex file:
%{
#include <stdlib.h>
#include <string.h>
#include "types.h"
#define NO_YY_UNPUT
/* #define YY_NEVER_INTERACTIVE */
extern char *strdup(const char *);
short unsigned int yylineno = 1;
%}
{ID} {
yylval.txt = strdup(yytext);
return ID;
};
\n { ++yylineno; }
My code looks good but I have problem when i want to compile on Ubuntu. In windows everything is okay but on linux I have errors like:
lex.l:10:14: error: expected identifier or ‘(’ before ‘__extension__’
lex.l:12:20: error: conflicting types for ‘yylineno’
lex.c:355:5: note: previous definition of ‘yylineno’ was here
Line 10: extern char *strdup(const char *);
Line 12: short unsigned int yylineno = 1;
strdup is declared in string.h, but it is a Posix interface and you should define an appropriate feature test macro before including any system header:
%top {
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include "types.h"
}
(Note: Using %top forces the enclosed code to be inserted at the top of the generated C file, in order to provide the guarantee that the feature test macro is defined before any system header.)
I don't know if that works on Windows (and it certainly depends on your compiler and toolchain), so you might need to declare strdup on that platform. If so, make sure you surround the declaration with a preprocessor test for the build environment.)
The error at line 10 is probably the result of strdup being defined as a macro. I'm not sure under what conditions that will happen -- it will be some GNU extension mode -- but defining the Posix feature test macro should prevent it.
As for the error with the type of yylineno, there is a simple solution: don't declare yylineno. It is declared in the code flex generates (and it may be declared as a macro if you ask for a re-entrant -- "pure" -- lexer).
So I'm creating a simple program, and I usually use the GNU compiler.
However, this time I chose to use Visual C++ for developing in C.
I've set up my project correctly, changing the settings to make it compile in C. The code is very simple:
#include <stdlib.h>
#include <stdio.h>
int main(){
printf("Hey!");
int x = 9;
printf("%d",x);
return 0;
}
If I compiled this using Code::Blocks IDE and the GNU compiler, it would work, but for some reason it doesn't work in Visual C++. I keep getting these errors:
error C2143: syntax error : missing ';' before 'type'
error C2065: 'x' : undeclared identifier
How can I fix this?
VC++ 2010 only implements C89/C90, not the newer C standards that allow variable declarations after other statements inside of a function body. To fix it, move the declaration of x to the beginning of main:
#include <stdlib.h>
#include <stdio.h>
int main() {
int x = 9;
printf("Hey!");
printf("%d",x);
return 0;
}
Change the file extension to .cpp
I was doing code porting from Linux to Windows. I am using Visual Studio environment. I am stuck with one problem.
There is a function call with 2 parameters for Acquire and Release a semaphore in Windows.
The Linux code has one parameter
Windows:
KeInitializeSpinLock(spinlock,oldIRQL);
Linux
spin_lock_init(spinlock);
I have generic call like which I have to use :
Get_Lock(spinlock);
How do I do this for windows without changing the prototype of Get_Lock ?
I tried the following :
#define Get_Lock(lock) \
KIRQL oldIrql;\
KeAcquireSpinLock(&(lock),&oldIrql);
#define Release_Lock(sync) KeReleaseSpinLock(&(sync),oldIrql)
But the compiler is giving errors .. Basically I want to retain the value of oldIrql because that value is needed for KeReleaseSpinLock
Error
error C2275: 'KIRQL' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'oldIrql'
error C2065: 'oldIrql' : undeclared identifier
error C2065: 'oldIrql' : undeclared identifier
error C2065: 'oldIrql' : undeclared identifier
KIRQL is defined as
typedef UCHAR KIRQL
What wrong am I doing here ? Or Is there any other method which is there which can be used without changing the prototype of the Get_Lock and Release_Lock?
The problem is due to Microsoft compiler's only supporting C89 standard, which does not allow the intermingling of code and declarations. Get_Lock() is being called after a line of code (I suspect), which introduces the declaration of oldIrql.
If it is the case that the lock is obtained and released in the same scope always a possible fix (hack) would be to declare KIRQL oldIrql; at the top of the scope where Get_Lock() and Release_Lock() is called, and remove the declaration from Get_Lock().
A tidier solution would be to eliminate the macros and introduce a new struct that defines a lock. For example:
typedef struct _lock
{
#ifdef WIN32
UCHAR oldIrql;
PKSPIN_LOCK sem;
#else
#endif
} lock;
lock* lock_new()
{
lock* result = malloc(sizeof(lock));
/* Perform OS dependent initialisation. */
#ifdef WIN32
#else
#endif
return result;
}
void lock_delete(lock* aLock)
{
/* Perform OS dependent tidy tasks. */
#ifdef WIN32
#else
#endif
free(aLock);
}
void lock_obtain(lock* aLock)
{
/* OS dependent acquire. */
#ifdef WIN32
KeAcquireSpinLock(&aLock->sem, &aLock->oldIrql);
#else
#endif
}
void lock_release(lock* aLock)
{
/* OS dependent release. */
#ifdef WIN32
KeReleaseSpinLock(&aLock->sem, aLock->oldIrql);
#else
#endif
}
I also suspect the reason posted by hmjd. But, in my opinion, the solution can be as follows:
#define Get_Lock(lock) { KIRQL oldIrql; KeAcquireSpinLock(&(lock),&oldIrql);
#define Release_Lock(sync) KeReleaseSpinLock(&(sync),oldIrql) }
But, you need to ensure that Get_Lock & Release_Lock is in same scope which anyway you would be ensuring as per your comments.
The idea is mostly same as that of pthread_cleanup_push & pthread_cleanup_pop. You can refer to the same also.