9

I have a C application I am trying to compile for Mac OS X 10.6.4:

$ uname -v
Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386

My gcc is as follows:

$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)

My Makefile is as follows:

CC=gcc
CFLAGS=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -O3 -Wformat -Wall -pedantic -std=gnu99

all: myApp
    rm -rf *~

myApp: myApp.o
    ${CC} ${CFLAGS} myApp.o -lbz2 -o myApp
    rm -rf *~

clean:
    rm -rf *.o myApp

The issue is that my application makes calls to fseeko64 and fopen64, and uses the off64_t type for offsets. When I compile my application I get the following warnings and errors:

$ make myApp
gcc -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -O3 -Wformat -Wall -pedantic -std=gnu99   -c -o myApp.o myApp.c
myApp.c: In function ‘extractData’:
myApp.c:119: warning: implicit declaration of function ‘fseeko64’
myApp.c:119: error: ‘off64_t’ undeclared (first use in this function)
myApp.c:119: error: (Each undeclared identifier is reported only once
myApp.c:119: error: for each function it appears in.)
myApp.c: In function ‘extractMetadata’:
myApp.c:305: warning: implicit declaration of function ‘fopen64’
myApp.c:305: warning: assignment makes pointer from integer without a cast

My code builds without errors under Linux. What changes can I make to the source code to add large file support when building under Darwin?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345

3 Answers3

12

On Darwin file I/O is 64-bit by default (10.5 at least), just found this by grepping in /usr/include:

sys/_types.h:typedef __int64_t  __darwin_off_t;

unistd.h:typedef __darwin_off_t     off_t;

So all you need to do is something like

#ifdef __APPLE__
#  define off64_t off_t
#  define fopen64 fopen
...
#endif
mojuba
  • 11,842
  • 9
  • 51
  • 72
7

While this question has an up-voted accepted answer which works I think the solution is a bit misleading. Instead of fixing something it's always better to avoid having to fix it later in the first place.

For example for the fopen64 function the GNU C Library docs say:

If the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits machine this function is available under the name fopen and so transparently replaces the old interface.

You can just use the same function fopen on systems that support 64-bit I/O by default and you can set the _FILE_OFFSET_BITS=64 flag on 32-bit without the need write redefines at all. The same goes for types like off64_t vs. off_t.

Save the redefines for the case when you have to deal with 3rd party sources and use standard functions in your own code.

aergistal
  • 29,947
  • 5
  • 70
  • 92
  • This should be the accepted answer. This is the right way to fix this. – Sam Jul 07 '16 at 01:08
  • 1
    I looked into this more and although this should be the accepted answer for non-Darwin platforms, on Darwin they have made the file functions 64-bit, and `_FILE_OFFSET_BITS` and `_LARGEFILE64_SOURCE` don't exist. So the accepted answer is probably the best way to fix such a portability issue. – Sam Jul 07 '16 at 01:35
1

The fseeko and similar commands work with large file support so no need for the fseeko64 etc Apple man page

mmmmmm
  • 32,227
  • 27
  • 88
  • 117