Header included but declarations still missing? - c

Here's a simple example:
#include <stdlib.h>
int main(void) {
_set_error_mode(_OUT_TO_STDERR);
return EXIT_SUCCESS;
}
When compiling this program, I get the following problems:
main.c: In function 'main':
main.c:4: error: implicit declaration of function '_set_error_mode'
main.c:4: error: '_OUT_TO_STDERR' undeclared (first use in this function)
main.c:4: error: (Each undeclared identifier is reported only once
main.c:4: error: for each function it appears in.)
The header does contain the function declaration and the macro:
_CRTIMP int __cdecl __MINGW_NOTHROW _set_error_mode (int);
# define _OUT_TO_STDERR 1
How come I get the errors? Notice that I also used the EXIT_SUCCESS macro which is also defined in the same stdlib.h header but for some reason GCC doesn't complain about it. Odd.
I'm using MinGW + GCC on a Vista machine.

Your code snippet works fine for me with MinGW 3.4.5
Are you sure you have your include file path set correctly? Maybe the wrong stdlib.h is being processed. Alternatively, maybe MingGW isn't defining __MSVCRT__ which is necessary to get that function prototype (MinGW seems to define that automatically for me - I'm not sure how one would turn it off).
edit:
tyranid's comment seems to have the answer - if I specify the -ansi option, I get the exact same set of errors as in your example.
Setting to CW and will delete if tyranid posts an answer.

Related

off64_t in Cygwin

I have compiled program in minGW which I want to run in my computer which has Cygwin installed in my system. I have the following compilation error:
error: 'off64_t' undeclared (first use in this function)
I added the following to the CFLAGS in Makefile, but still having the same error.
-D"_LARGEFILE64_SOURCE" -D"_FILE_OFFSET_BITS=64"
Can someone suggest what am I missing here and what I need to add?
Cygwin does not define the off64_t type. Instead, if defines _off64_t. If you want to compile your existing code with minimal changes, add the following at the top of your sources:
#include <sys/types.h>
typedef _off64_t off64_t;

Compilation Error: Fprintf throws parse error

I am trying to detect an error so I included in my program some traces.
The problem is that after that, it doesn't compile, giving me next error:
../src/DR700_API.c:46: parse error before `*'
I just added an fprintf at the beginning of each function:
fprintf(stdout,"_name_of_function_");
Commenting all fprintf it compiles right, so there's the error. I can't dispense with them as i want to track other error in execution time.
Here's a little example:
#include <stdio.h>
#include <stdlib.h>
ImprFunc *DR700_new()
{
fprintf(stdout,"DR700_new");
ImprFunc *impr = (ImprFunc *)malloc(sizeof(DR700_ImprFunc));
if (impr == NULL)
return NULL;
...
../src/DR700_API.c:46: parse error before `*'
../src/DR700_API.c:47: `impr' undeclared (first use in this function)
../src/DR700_API.c:47: (Each undeclared identifier is reported only once
../src/DR700_API.c:47: for each function it appears in.)
make: *** [../obj/DR700_API.o] Error 1
Probably your setup doesn't allow mixed code and declarations (as per C89). If you wish to not affect project setup - try to keep declarations before any code. In your example it means
ImprFunc *impr = (ImprFunc *)malloc(sizeof(DR700_ImprFunc));
fprintf(stdout,"DR700_new");
instead of
fprintf(stdout,"DR700_new");
ImprFunc *impr = (ImprFunc *)malloc(sizeof(DR700_ImprFunc));
Or alternatively - add -std=c99 (as was mentioned in comments).
In early versions of C, variables had to be declared at the beginning of a block.
C99 allows to mix declarations and statements arbitrarily (e.g. see Variable declaration placement in C and Where can I legally declare a variable in C99?).
You could try compiling with --std=c99 / --std=c11 which will allow you to declare variables anywhere (if supported in your version of gcc. See Status of C99 features in GCC and C11Status).
C99 is, for the most part, backward compatible with C89.

Compiling som_pak (97) with present gcc

I'm trying to use the original som implementation by Kohonen but I'm getting a segmentation fault error using vcal.
It turns out that you can use an unofficial version which corrects this error found at
http://cis.legacy.ics.tkk.fi/hynde/lvq/
but its from 1997, i'm sure that there are a lot of changes in cc compiler so I'm getting this error
checo#canija:~/bin/som/som_pak-3.2$ make
gcc -O2 -c -o vcal.o vcal.c
In file included from datafile.h:28,
from vcal.c:26:
fileio.h:69: error: conflicting types for ‘getline’
/usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here
make: *** [vcal.o] Error 1
checo#canija:~/bin/som/som_pak-3.2$
The file datafile.h
1:#ifndef SOMPAK_DATAFILE_H
2:#define SOMPAK_DATAFILE_H
...
24:#include
25:#include
26:#include "lvq_pak.h"
27:#include "errors.h"
28:#include "fileio.h"
Is there anything I can do to recomple this code?
Converting a comment to an answer to allow the question to be resolved.
getline() is now a POSIX function; it wasn't in 1997. Your best bet may be to rename the function in fileio.h and where it is used, maybe as simply as adding before the appearance of getline and after the #include <stdio.h>.
#undef getline
#define getline(a, b, c) som_getline(a, b, c)
Use the right number of parameters, or
#define getline(...) som_getline(__VA_ARGS__)
If <stdio.h> is not yet included, then add it to ensure that getline() is declared normally, then mapped by your macro.

Problems using graphviz as a library

I'm trying to use graphviz as a library for a C++ project, following the libguide provided here. However I'm having problems even compiling the examples in the appendix. When I try to compile demo.c using gcc I get the following output:
$ gcc -I/usr/local/Cellar/graphviz/2.28.0/include/ demo.c -L/usr/local/Cellar/graphviz/2.28.0/lib/ -lgvc -lgraph -lcdt
demo.c: In function ‘main’:
demo.c:14: error: ‘Agdirected’ undeclared (first use in this function)
demo.c:14: error: (Each undeclared identifier is reported only once
demo.c:14: error: for each function it appears in.)
demo.c:15: error: too many arguments to function ‘agnode’
demo.c:16: error: too many arguments to function ‘agnode’
demo.c:17: error: too many arguments to function ‘agedge’
Agdirected is found in cgraph.h, but if I change the includes in demo.c to
#include <graphviz/gvc.h>
#include <graphviz/cgraph.h>
Then all hell breaks loose (mostly conflicting declarations between the two headers). How can I include the necessary headers without the headache of all these conflicts?
Mac OS X 10.8.3, Graphviz 2.28.0, GCC 4.2.1
It seems after some experimentation that adding the flag
#define WITH_CGRAPH
has the effect of including cgraph.h, which gets rid of the "'Agdirected' undeclared" error.
The other errors can be fixed by changing the command line option in gcc from -lgraph to -lcgraph
The libguide you are using is the cgraph version, which assumes Graphviz 2.30 or later. With that version, the #define WITH_CGRAPH is already provided.

fileno, F_LOCK and F_ULOCK become undeclared and unavailable when I add std=c99 flag to gcc

I have these headers in a c code
#include <stdio.h>
#include <unistd.h>
Everything compiled fine until I added -std=c99 flag to gcc command (to enable restrict). And this triggered the following errors.
warning: implicit declaration of
function fileno
error: F_LOCK undeclared (first use
in this function)
error: (Each undeclared identifier is
reported only once error:
for each function it appears in.)
error: F_ULOCK undeclared (first use
in this function
Any ideas to workaround these errors/warnings?
You need to tell the headers that you want the POSIX extensions. These days, I use:
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */
If I'm compiling with -std=c89, it gives the correct POSIX version; if you compile with -std=c89, it gives the correct POSIX version. I use this on Solaris 9 and 10, MacOS X (10.4.x, 10.5.x), HP-UX 11.x, Linux (RHEL4 and 5, SuSE 9 and 10) and AIX 5.x and 6.x - AFAICR, without problems so far.
This stanza should appear before any system headers are included (in your own header, or in each source file), or you need to achieve the same effect with -D_XOPEN_SOURCE=600 on the compiler command line, or some other similar mechanism.
Try
-std=gnu99
to enable all extensions and still use the C99 language enhancements.
You might try -D_BSD_SOURCE to enable BSD-isms or -D_SVID_SOURCE to enable System-V isms

Resources