compiling c code on mac

Discussion in 'Mac Programming' started by wrathkeg, Feb 1, 2010.

  1. wrathkeg macrumors newbie

    Joined:
    Feb 1, 2010
    #1
    Hi all,

    I was given some C code (below) some years ago. The program converts WAV audio files to PDF objects, which I then embed into PDFs created with LaTeX. I have binary versions of the program for Windows and Linux. I now have a Mac and am trying to compile the code to work on there too, but am having problems. When I run

    Code:
    gcc wav2obj.c -o wav2obj
    I get this message:

    Code:
    wav2obj.c: In function ‘main’:
    wav2obj.c:122: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘DWORD’
    wav2obj.c:142: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘DWORD’
    wav2obj.c:146: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘DWORD’
    When I run the resulting binary on a file, I get this:

    Code:
    wav2obj: Error - ding.wav unknown file format
    My linux binary processes this file just fine. So, my guess is that something is going wrong with the creation of the binary. Does anyone have any suggestions? I'd be really grateful for some pointers. I have no knowledge of C (and no time to learn about it!) so a solution which "just works" (and without me understanding it) would be ideal, or a (simple!) explanation of the problem.

    Thanks in advance,

    wrathkeg

    Code:
    /*
     *  wav2obj - Wave to PDF-object converter
     *  Copyright (C) 2000-2003 Wolfram Manthey
     *
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 2 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program; if not, write to the Free Software
     *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     *
     *  Wolfram Manthey
     *  wolfram.manthey@gmx.de
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef unsigned short int WORD;
    typedef unsigned long int DWORD;
    
    typedef struct {
    	char ID[3];
    	DWORD length;
    } WAVEID;
    
    typedef struct { 
        WORD  wFormatTag; 
        WORD  nChannels; 
        DWORD nSamplesPerSec; 
        DWORD nAvgBytesPerSec; 
        WORD  nBlockAlign; 
        WORD  wBitsPerSample; 
    } WAVEFORMAT; 
    
    typedef struct { 
    	char m;
    } mono8; 
    
    typedef struct { 
    	char ml;
    	char mh;
    } mono16; 
    
    typedef struct { 
    	char l;
    	char r;
    } stereo8; 
    
    typedef struct { 
    	char ll;
    	char lh;
    	char rl;
    	char rh;
    } stereo16; 
    
    
    
    int main(int argc, char **argv) {
    	FILE *infile, *outfile;
    	char ID[4];
    	WAVEID chunk;
    	WAVEFORMAT Format;
    	DWORD Pos;
    	mono8 SampleM8;
    	mono16 SampleM16;
    	stereo8 SampleS8;
    	stereo16 SampleS16;
    	char Temp;
    
    	if (argc < 3) {
    		printf("wav2obj - converts a WAV file into a PDF sound-object\n\n");
    		printf(" Syntax: wav2obj <wavefile.wav> <objfile.obj>\n");
    		return 1;
    	}
    
    	if (( infile=fopen( argv[1], "rb" )) == NULL ) {
    		printf( "wav2obj: Error - Could not open input file %s\n", argv[1] );
    		return 2;
    	}
    
    	if (( outfile=fopen( argv[2], "wb" )) == NULL ) {
    		printf( "wav2obj: Error - Could not open output file %s\n", argv[2] );
    		return 2;
    	}
    
    	fread (&chunk, sizeof(chunk), 1, infile);
    	if (memcmp(chunk.ID, "RIFF", 4)) {
    		printf( "wav2obj: Error - %s unknown file format\n", argv[1]);
    		return 3;
    	}
    
    	fread (&ID, sizeof(ID), 1, infile);
    	if (memcmp(ID, "WAVE", 4)) {
    		printf( "wav2obj: Error - %s unknown file format\n", argv[1]);
    		return 3;
    	}
    
    	fread (&chunk, sizeof(chunk), 1, infile);
    	if (memcmp(chunk.ID, "fmt ", 4)) {
    		printf( "wav2obj: Error - %s unknown file format\n", argv[1]);
    		return 3;
    	}
    
    	fread (&Format, sizeof(Format), 1, infile);
    	fread (&chunk, sizeof(chunk), 1, infile);
    	if (memcmp(chunk.ID, "data", 4)) {
    		printf( "wav2obj: Error - %s unknown file format\n", argv[1]);
    		return 3;
    	}
    
    	printf("\n%s Format:\n", argv[1]);
    	printf(" Channels: %d\n", Format.nChannels);
    	printf(" SampleRate: %d Hz\n", Format.nSamplesPerSec);
    	printf(" BitsPerSample: %d\n", Format.wBitsPerSample);
    
    	if (Format.wFormatTag != 1) {
    		printf("\nSorry - not a PCM coded wav-file!\n");
    		return 4;
    	}
    
    	if ((Format.nChannels > 2) || (Format.nChannels < 1)) {
    		printf("\nSorry - only 1 or 2 channels supported by PDF!\n");
    		return 4;
    	}
    
    	if ((Format.wBitsPerSample != 8) && (Format.wBitsPerSample != 16)) {
    		printf("\nSorry - only 8 or 16 bits per sample supported by PDF!\n");
    		return 4;
    	}
    
    	fprintf(outfile, "<<\n");
    	fprintf(outfile, "/Type /Sound ");
    	fprintf(outfile, "/R %d ", Format.nSamplesPerSec);
    	fprintf(outfile, "/C %d ", Format.nChannels);
    	fprintf(outfile, "/B %d ", Format.wBitsPerSample);
    	fprintf(outfile, "/E /Signed\n");
    	fprintf(outfile, "/Length %d\n", chunk.length);
    	fprintf(outfile, ">>\n");
    	fprintf(outfile, "stream\n");
    
    	Pos=1;
    	while (Pos < chunk.length) {
    		switch (Format.nChannels + Format.wBitsPerSample) {
    		case  9 :
    			fread (&SampleM8, sizeof(SampleM8), 1, infile);
    			fwrite (&SampleM8, sizeof(SampleM8), 1, outfile);
    			break;
    		case 10 : 
    			fread (&SampleS8, sizeof(SampleS8), 1, infile);
    			fwrite (&SampleS8, sizeof(SampleS8), 1, outfile);
    			break;
    		case 17 : 
    			fread (&SampleM16, sizeof(SampleM16), 1, infile);
    			Temp=SampleM16.mh;
    			SampleM16.mh=SampleM16.ml;
    			SampleM16.ml=Temp;
    			fwrite (&SampleM16, sizeof(SampleM16), 1, outfile);
    			break;
    		case 18 : 
    			fread (&SampleS16, sizeof(SampleS16), 1, infile);
    			Temp=SampleS16.lh;
    			SampleS16.lh=SampleS16.ll;
    			SampleS16.ll=Temp;
    			Temp=SampleS16.rh;
    			SampleS16.rh=SampleS16.rl;
    			SampleS16.rl=Temp;
    			fwrite (&SampleS16, sizeof(SampleS16), 1, outfile);
    			break;
    		}
    		Pos+=Format.nBlockAlign;
    	}
    
    	fprintf(outfile, "\nendstream\n");
    
    	fclose(outfile);
    	fclose(infile);
    	return 0;
    }
     
  2. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    DWORD in the code is defined as a long int.

    %d is an integer print format that's why you get the warnings at compile time.

    %ld is the appropriate format for long int.

    You can change the %d to %ld for the three lines that reported warnings and it should compile just fine.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    Change the WORD and DWORD to this:
    Code:
    typedef uint16_t WORD;
    typedef uint32_t DWORD;
    and add
    Code:
    #include <stdint.h>
    Worked for me.
     
  4. wrathkeg thread starter macrumors newbie

    Joined:
    Feb 1, 2010
    #4
    Thanks, both, for your replies.

    PilotError: I followed your suggestion, and while the code compiled without warnings, when I tried to run the binary I got a 'unary operator expected' message. Whatever that means :eek:

    Kainjow: your suggestions worked perfectly. Thanks a million. Now I don't have to learn C :D

    Thanks again,

    wrathkeg
     

Share This Page