1

i am making a program that i have decided to split in two .c files. the first program.c file has all the include files and declarations and the other unit.c file has a dozen functions and some extern declarations of global variables defined in the the main program.c

everything was working fine (compilation/linking) so far the extern variables declared in unit.c and defined in program.c are of the build in type like int or float. when i tried to use a variable of a new type from a header file that is included in program.c but not in unit.c normally the compiler complained.

to be specific the situation is like this:

program.c

#include "SDL.h"
int a;
SDL_Surface* sprite;
SDL_Surface* ghost;

unit.c

extern int a; //works fine
extern SDL_Surface* sprite; //compiler error
extern SDL_Surface* ghost; //compiler error
void funct1 (void)
{
    if (sprite == ghost)
    ....
}

if i include SDL.h at the top of unit.c the problem is solved, but i don't want to include everything everywhere so i figured a round solution. i have declared sprite and ghost to be of type void pointer only in unit.c but the definition in program.c remains the same. like this:

unit.c

extern void* sprite;
extern void* ghost;

the compiler did not complain. i only need those extern pointers to test if they point to the same address. i did a very little bit of testing on the executable and it's working fine.

i am using the free command line Borland compiler ver 5.5.1

the question is, can this be considered legitimate in ANSI C or is it some quirk of the Borland compiler? is the only true (best practice) solution to my situation to include SDL.h in unit.c or can you please point me to some other legitimate alternative?

  • If you don't want to include SDL everywhere, you should encapsulate the `sprite` and `ghost` objects by, for example, making your own `Entity` struct, not by relying on this trick. – Colonel Thirty Two Oct 21 '15 at 12:26

2 Answers2

1

No, it's not legal. You can convert any data pointer to void*, but you cannot pretend that existing pointer is void*.

Workaround is to create header program.h, and include that into unit.c:

#ifndef PROGRAM_H
#define PROGRAM_H
#include "SDL.h"
extern int a;
extern SDL_Surface* sprite;
extern SDL_Surface* ghost;
#endif

Don't try to avoid including headers into .c files at all cost; it's a standard practice in C, and best way to avoid problems.

user694733
  • 15,208
  • 2
  • 42
  • 68
  • as described in this post: –  Oct 21 '15 at 22:12
  • as described in this post: [link](http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c) best practice is to declare the globals in one header file with the keyword `extern`, so they are only declared and not defined, and then you can include that file everywhere. you only need to define a global in one source file. good explanation of the `extern` keyword: [link](http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/) i don't even have to declare: `extern void* ghost` but `extern ghost` is enough –  Oct 21 '15 at 22:29
  • @b'stard I don't think that omitting type is a good idea. As far as I know, `extern ghost` is interpreted as `extern int ghost`. – user694733 Oct 26 '15 at 08:50
0

SDL_Surface is not defined in unit.c.

Add

#include "SDL.h"

to unit.c

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56