Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat preprocessor directive or other method should I use to discern 32- vs 64-bit environment?
    primarykey
    data
    text
    <p>I would like to compile the following <strong>C</strong> program for 32- and 64-bit systems.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;stddef.h&gt; int main(int argc, char** argv) { size_t size = atoi(argv[1]); int *array; array = malloc(size * sizeof(int)); if (array == NULL) { fprintf(stderr, "could not allocate memory\n"); exit(1); } fprintf(stdout, "memory allocated on heap: %u bytes\n", sizeof(int)*size); fprintf(stdout, "press Return to quit\n"); getchar(); fprintf(stdout, "freeing memory\n"); free(array); exit(0); } </code></pre> <p>What I have been doing with my <code>Makefile</code> is pass in <code>-m32</code> and <code>-64</code> to make bit-specific binaries:</p> <pre><code>CFLAGS=-ansi -pedantic -Wall -O3 32BIT_ARCH=-m32 64BIT_ARCH=-m64 32_CFLAGS=${32BIT_ARCH} ${CFLAGS} 64_CFLAGS=${64BIT_ARCH} ${CFLAGS} CC=gcc ARRAY_32BIT_BINARY_NAME=arrayTest32 ARRAY_64BIT_BINARY_NAME=arrayTest64 all: ${ARRAY_32BIT_BINARY_NAME} ${ARRAY_64BIT_BINARY_NAME} arrayTest32: main32_array.o ${CC} ${32_CFLAGS} main32_array.o -o ${ARRAY_32BIT_BINARY_NAME} arrayTest64: main64_array.o ${CC} ${64_CFLAGS} main64_array.o -o ${ARRAY_64BIT_BINARY_NAME} main32_array.o: main.c ${CC} ${32_CFLAGS} -c main.c -o main32_array.o main64_array.o: main.c ${CC} ${64_CFLAGS} -c main.c -o main64_array.o clean: -rm *.o *~ ${ARRAY_32BIT_BINARY_NAME} ${ARRAY_64BIT_BINARY_NAME} install: cp ${ARRAY_32BIT_BINARY_NAME} ${ARRAY_64BIT_BINARY_NAME} ../bin </code></pre> <p>This works great, but I run into a warning when compiling:</p> <pre><code>$ make gcc -m32 -ansi -pedantic -Wall -O3 -c main.c -o main32_array.o gcc -m32 -ansi -pedantic -Wall -O3 main32_array.o -o arrayTest32 gcc -m64 -ansi -pedantic -Wall -O3 -c main.c -o main64_array.o main.c: In function ‘main’: main.c:14: warning: format ‘%u’ expects type ‘unsigned int’, but argument 3 has type ‘long unsigned int’ gcc -m64 -ansi -pedantic -Wall -O3 main64_array.o -o arrayTest64 </code></pre> <p>What I would like to do is fix this warning without having two main.c files for two "bit" targets.</p> <p>Is there an <code>#ifndef</code> or other preprocessor conditional I can add to line 14 of <code>main.c</code> to handle this difference?</p> <p>Or is there a different and better way to handle this? </p> <p><strong>EDIT</strong>: I used the following solution:</p> <pre><code>#if defined(__LP64__) fprintf(stdout, "memory allocated on heap: %lu bytes\n", sizeof(int)*size); #else fprintf(stdout, "memory allocated on heap: %u bytes\n", sizeof(int)*size); #endif </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload