understanding some source code - c

gcc 4.7.2
c89
Hello,
I am reviewing someones source code, and I have come across this.
I have this declaration and definition that I don't understand what it does. I know that the static means that it will not be exported out of the file.
static SERVICE_STATUS_HANDLE g_win_status_handle = NULL;
Because it is set to NULL it looks like a pointer. SERVICE_STATUS_HANDLE isnt' defined anywhere else. Only this file.
It is being used like this, is this comparing if g_win_status_handle is equal to NULL after SERVICE_STATUS_HANDLE is casted to 0 or NULL:
if(g_win_status_handle == (SERVICE_STATUS_HANDLE)0) {
/* do something */
}
And like this:
if(!SetServiceStatus(g_win_status_handle, &g_win_status)) {
/* do something */
}
Many thanks if someone can shed some light on this.

i have made small program
#include<stdio.h>
static SERVICE_STATUS_HANDLE g_win_status_handle = NULL;
int main()
{
if(g_win_status_handle == (SERVICE_STATUS_HANDLE)0) {
printf("ksdfbhdejkfb");
}
return 0;
}
compiled on gcc (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4)
with c89 flags, like
gcc -std=c89 temp.c
its giving error
temp.c:3:30: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘g_win_status_handle’
temp.c: In function ‘main’:
temp.c:6:4: error: ‘g_win_status_handle’ undeclared (first use in this function)
temp.c:6:4: note: each undeclared identifier is reported only once for each function it appears in
temp.c:6:28: error: ‘SERVICE_STATUS_HANDLE’ undeclared (first use in this function)
temp.c:6:50: error: expected ‘)’ before numeric constant
Here it is obvious that SERVICE_STATUS_HANDLE must be defined some where.. if not defined then how your code is going to even compile?
May be it will be defined in some header file..
Updated answer from comments
SERVICE_STATUS_HANDLE is defined in windows.h and its going to include.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx

Related

Why does Clang think these types in Cairo conflict?

I am building cairo from source using Clang. I get the following error:
src/cairo-quartz-font.c:368:1: error: conflicting types for 'cairo_quartz_font_face_create_for_cgfont'
cairo_quartz_font_face_create_for_cgfont (CGFontRef font)
^
src/cairo-quartz-font.c:247:18: note: previous implicit declaration is here
*font_face = cairo_quartz_font_face_create_for_cgfont (cgFont);
However, looking at the source, I find these definitions:
247:
CGFontRef cgFont = NULL;
// ...
*font_face = cairo_quartz_font_face_create_for_cgfont (cgFont);
CGFontRelease (cgFont);
368:
cairo_font_face_t *
cairo_quartz_font_face_create_for_cgfont (CGFontRef font)
{
cairo_quartz_font_face_t *font_face;
// ...
The full source is mirrored here.
What is the type conflict here?
When you use the function cairo_quartz_font_face_create_for_cgfont at line 247, it is undeclared (you should get a warning about that unless you fail to use -Wall). So the compiler fills in an assumed return type of int.
When you finally declare the function, its return type is not int. So that's a type conflict.
Normally this sort of problem would be avoided by #includeing a header with the function prototypes.

Clang: Do not optimize a specific function

For a long time i used gcc to compile C code. Sometimes i had to use the optimize("O0") attribute to disable optimizations for a specific function. Now i like to do this with clang.
Assume the following code:
#include <stdio.h>
void __attribute__((optimize("O0"))) blabla(void) {
}
int main(void) {
blabla();
return 0;
}
If i compile it with clang i get this error:
test2.c:3:21: warning: unknown attribute 'optimize' ignored [-Wattributes]
void __attribute__((optimize("O0"))) blabla(void) {
^
1 warning generated.
Then i used google (and also) stackoverflow to find out what attribute is required for clang, because many of them are not in the standard (as soon as i know).
I found this thread:
In clang, how do you use per-function optimization attributes?
If i try the attribute optimize("0") i get this error:
test2.c:3:21: warning: unknown attribute 'optimize' ignored [-Wattributes]
void __attribute__((optimize("0"))) blabla(void) {
^
1 warning generated.
And if i try the attribute optnone i get this error:
test2.c:3:21: warning: unknown attribute 'optnone' ignored [-Wattributes]
void __attribute__((optnone)) blabla(void) {
^
1 warning generated.
I also tried to move the attribute after the function name, but it doesn't work (for some reason there is a warning about GCC?!):
test2.c:3:34: warning: GCC does not allow optnone attribute in this position on a function definition [-Wgcc-compat]
void blabla(void) __attribute__((optnone)) {
^
test2.c:3:34: warning: unknown attribute 'optnone' ignored [-Wattributes]
2 warnings generated.
Another test with the following code:
#include <stdio.h>
[[clang::optnone]]
void blabla(void) {
}
int main(void) {
blabla();
return 0;
}
It produces:
user#ubuntu:/tmp/optxx$ clang test2.c
test2.c:3:1: error: expected identifier or '('
[[clang::optnone]]
^
test2.c:3:2: error: expected expression
[[clang::optnone]]
^
test2.c:8:5: warning: implicit declaration of function 'blabla' is invalid in C99 [-Wimplicit-function-declaration]
blabla();
^
1 warning and 2 errors generated.
Probably i do something wrong, but i cannot see what.
-edit-
clang version:
user#ubuntu:/tmp/optxx$ clang -v
Ubuntu clang version 3.3-16ubuntu1 (branches/release_33) (based on LLVM 3.3)
Target: x86_64-pc-linux-gnu
Thread model: posix
Try the following, clang-style attribute specification:
[[clang::optnone]]
void blabla(void);
EDIT: Clang 3.3 is pretty outdated. Use a more recent version, and your original ((optnone)) code will work.

GCC compiler errors with primitive type definitons

So upon upgrading to Linux Mint(and using GCC v4.7.3) some headers have been generating some very odd compiler errors. When I try to compile I get the following error messages:
gcc s.c
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.7/include/setjmp.h:26:0,
from /usr/lib/gcc/x86_64-linux-gnu/4.7/include/bits/pthreadtypes.h:14,
from /usr/lib/gcc/x86_64-linux-gnu/4.7/include/pthread.h:14,
from s.c:2:
/usr/lib/gcc/x86_64-linux-gnu/4.7/include/bits/setjmp.h:30:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘typedef’
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.7/include/bits/pthreadtypes.h:14:0,
from /usr/lib/gcc/x86_64-linux-gnu/4.7/include/pthread.h:14,
from s.c:2:
/usr/lib/gcc/x86_64-linux-gnu/4.7/include/setjmp.h:46:5: error: unknown type name ‘__jmp_buf’
Compilation exited abnormally with code 1 at Mon Nov 25 09:03:37
So this is the header which is triggering the compiler error(s):
/* Define the machine-dependent type `jmp_buf'. x86-64 version. */
#ifndef _BITS_SETJMP_H
#define _BITS_SETJMP_H 1
#include <bits/wordsize.h>
# if __WORDSIZE == 64
typedef long int __jmp_buf[8];
# else
typedef int __jmp_buf[6];
# endif
#endif /* bits/setjmp.h */
And here is the source code(s.c):
#include <stdio.h>
#include <pthread.h>
int main()
{
return 0;
}
What's going on here?
So I googled "linux pthreadtypes" and found another version of at code dot woboq dot org that is Linux specific. It compiles nicely so I scrubbed the existing version of the header that gave me the errors that I posted about earlier. Not sure where I got that version from but it was somewhere on the Internet and may very well have been the FreeBSD version.Gotta make sure your POSIX thread headers/libraries are compatible with your OS.

Linking a library in g++ doesn't work

I am trying to compile a .cpp-file which uses a matrix-library. The library-files libnewmat.a and libnewmat.so are in the path /usr/lib64 . The include-files are in path /usr/include/newmat , so I tried (several ways) to compile i.e. with:
g++ -I/usr/include -L/usr/lib64 -lnewmat new.cpp -o new3
but the compiler doesn't find the library. The content of the .cpp is:
#include <iostream>
#include <newmat/newmat.h>
#include <newmat/newmatio.h>
using namespace std;
int main()
{
Matrix A(2,2);
Real b[] = {1,2,3,4};
A << b;
cout << A << endl;
return 0;
}
The compiler says:
test.cpp: In function ‘int main()’:
test.cpp:9: error: ‘Matrix’ was not declared in this scope
test.cpp:9: error: expected ‘;’ before ‘A’
test.cpp:10: error: ‘Real’ was not declared in this scope
test.cpp:10: error: expected ‘;’ before ‘b’
test.cpp:11: error: ‘A’ was not declared in this scope
test.cpp:11: error: ‘b’ was not declared in
this scope
Could You provide me with the correct c++ code, or the correct command line instruction?
Thanks, Kepler
If you recently installed this library yourself you probably need to run sudo ldconfig to load the so into the linker cache.
EDIT: As Kevin said not a linking error that you're getting.
Perhaps it's a name space issue?
using namespace NEWMAT;
according to this: http://www.robertnz.net/nm10.htm#namesp
This isn't a library problem - it's a compiler problem - it can't find any definition for Matrix (probably in your include files, but we can't determine that with the information given)
[edit]
Ascertain if your classes in the include files are being referenced correctly
[/edit]

Eclipse goto label not working in C

I am using the Eclipse CDT and I have a goto label and a FILE definition after it and when I compile the project it gives me the error: Expression expected before FILE.
Thanks in advance,
Mr. Man
EDIT:
Ok, so this is what I get from the command line:
iOS.c: In function ‘main’:
iOS.c:45: error: expected expression before ‘FILE’
iOS.c:49: error: ‘preFile’ undeclared (first use in this function)
iOS.c:49: error: (Each undeclared identifier is reported only once
iOS.c:49: error: for each function it appears in.)`
And this is what code throws the error:
fileExists:
FILE *preFile = fopen("prefix.txt","r");
As you're coding in C, you need to declare the variable at the beginning of the function:
void foo()
{
FILE* preFile;
// some code
fileExists:
preFile = fopen("prefix.txt","r");
}

Resources