160

I use Apple Reachability class from Apple Sample code Reachability

in Xcode 4.2 and new Apple 3.0 compiler I get warning in this class that

+ (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;

declaration of 'struct sockaddr_in' will not be visible outside of this function*

I am not good at classic C %) so I dont understand how I can fix this warning or may be I can ignore it at all.

Thx

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
Sergnsk
  • 3,305
  • 3
  • 23
  • 28

1 Answers1

352

Add #import <netinet/in.h> in Reachability.h to get away with this

Saurabh
  • 7,894
  • 2
  • 23
  • 31
  • 7
    I forgot to format it as code and stackoverflow removed for me :( – Saurabh Oct 14 '11 at 09:57
  • 3
    And take care: the import already exists in Reachability.m - but it's also needed in Reachability.h – brainray Feb 24 '12 at 10:05
  • 8
    `#include`, not `#import`. Due to subtle differences between C and Objective-C headers, you should only use `#import` for Objective-C. Pure C should continue to use `#include`. – Jonathan Grynspan Mar 10 '12 at 20:47
  • what's the difference between #include and #import? – user4951 Apr 19 '12 at 05:54
  • Anyway I got rid of #import #import out of the .m file and add that to .h file – user4951 Apr 19 '12 at 05:56
  • @Jonathan-Grynspan what differences? Is there any reason not to use `import` with C headers inside ObjC module? – Cfr Jul 19 '12 at 09:38
  • 7
    `#import` will prevent a header from being brought into a compilation unit twice. `#include` will allow it (and then guard macros are used to prevent duplicate symbols.) There are some headers that *expect* to be brought in twice. – Jonathan Grynspan Jul 19 '12 at 12:35
  • 6
    Instead of the #import, you can also do a forward declaration of the missing struct. Add a single "struct sockaddr_in;" line to the .h file – fishinear Nov 06 '12 at 17:33
  • 1
    @JonathanGrynspan - I have to disagree; `#import` is always the preferable option. It removes the need to have a bunch of `#ifndef SOMEFILE_H` nonsense in every single header file. If someone set up their header so that it *needs* to be brought in twice (or more) then they have done something wrong. In any other case the only argument in favor of using `#include` is a pedantic one about maintaining portability to compilers that do not support `#import`. Which is silly, in my opinion. – aroth Jan 24 '13 at 00:18
  • @aroth: Your opinion, while entitled to you, is wrong, for the reasons previously stated. – Jonathan Grynspan Jan 24 '13 at 03:18
  • 2
    @aroth - "If someone set up their header so that it needs to be brought in twice (or more) then they have done something wrong." - not really, you can make templates using macros, e.g. for hashtables, etc. which you *need* to include multiple times with different defs above so that different structures are defined. Example: https://klib.svn.sourceforge.net/svnroot/klib/trunk/klib/khash.h – Charlie Monroe Jul 11 '13 at 14:43
  • 1
    This was one interesting discussion I have read on stackoverflow. Thanks @JonathanGrynspan – ShayanK Oct 28 '13 at 19:24