Undeclared Identifier C Programming in Visual C++ - c

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

Related

Issue with math.h

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.

Visual studio #include "file.c" needed in every file

I recently wanted to test my C project on visual studio (I used visual studio code) but for some reason my other file (file.c) do not recognize function from my main file (main.c). I need to manually add #include in both files. I can compile the code normally using cmd (gcc main.c). If you have any idea what cause this I would be happy to hear.
main.c:
#include <stdio.h>
#include <stdlib.h>
int randNumber = 10;
#include "file.c"
int main ()
{
int a = 10;
int b = 2;
printf ("%i", multiplication (a, b));
return 0;
}
file.c:
int multiplication (int a, int b)
{
return a * b * randNumber;
}
This runs normally in vs code but not in visual studio. (I can also compile the code with cmd outside of visual studio with the command (gcc main.c)) The thing that doesn't work is the randNumber, it gets an error saying (identifier "randNumber" is undefined).
You should not include source files (.c) in other source files. software_rendering.c uses types defined in win32_platform.c. For example, the Render_Buffer structure.
You need to put the common types and definitions in a header file (.h) and include that. Keeping the Render_Buffer example: move the Render_Buffer structure definition in a header file and include that header file in both source files.
See Creating your own header file in C for details.
As a side note: please do not post images of your code, instead paste it as text and properly format it as code.

why __attribute__((weak)) in IAR can't compile?

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.

Alternative to this; STDBOOL not found

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).

Strange problem with compiling c code in xcode when including glext.h

I have this simple code
#include <stdio.h>
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
int main (int argc, const char * argv[])
{
printf("Hello, World!\n");
return 0;
}
If I comment out the line with "glext.h" it compiles and runs fine in xcode 4 if I uncomment that line I get 345 errors most of them 'expected * before *' ...
What is going on?! both gl.h and glext.h are inside the OpenGL framework but no matter if I incluhe it or not I get the same error. I tried GCC 4.2 as well as LLVM GCC 4.2 and LLVM (in this case 21 semantic and parse errors).
I am sure my lack of experience with C is causing this but I am surprised gl.h has no problem but glext.h has.
Even if I try to compile in from the command line by gcc I get lots of
/System/Library/Frameworks/OpenGL.framework/Headers/glext.h:3137: error: expected ‘)’ before ‘const’
Any ideas?
It's a bug with glext.h. If you look at that file, you'll see that it has a bunch of definitions that use GLenum, but GLenum isn't defined anywhere in that file. So, before you include glext.h, you need to include a file that defines GLenum. The easiest thing to do is to include gl.h first instead of second:
#include <stdio.h>
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
Switch these two lines around:
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
And it should work.

Resources