c - 我应该使用什么预处理器指令或其他方法来辨别 32 位和 64 位环境?

标签 c makefile c-preprocessor compiler-flags

我想为 32 位和 64 位系统编译以下 C 程序。

#include <stdio.h>                                                                                                                                                                  
#include <stdlib.h>                                                                                                                                                                 
#include <stddef.h>                                                                                                                                                                 

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);                                                                                                                                                                          
}         

我对 Makefile 所做的是传入 -m32-64 以生成位特定的二进制文件:

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

这很好用,但我在编译时遇到警告:

$ 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  

我想做的是在没有两个“位”目标的两个 main.c 文件的情况下修复此警告。

是否有 #ifndef 或其他预处理器条件我可以添加到 main.c 的第 14 行来处理这种差异?

或者是否有其他更好的方法来处理这个问题?

编辑:我使用了以下解决方案:

#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      

最佳答案

C99 提供了用于打印 size_t 值的 'z' 修饰符;在格式字符串中使用它。

关于c - 我应该使用什么预处理器指令或其他方法来辨别 32 位和 64 位环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/870363/

相关文章:

python - 使用多段三次贝塞尔曲线和距离以及曲率约束逼近数据

c - 我的二维字段不打印其内容

c - 使用循环查找除数

makefile - 将参数从 makefile 传递到 cocotb 测试平台

c++ - MinGW 在 Windows 上制作

C宏获取大于给定数的最小二乘方

c++ - 通过变量名遍历结构

android - 树莓派 : Create a RPi server which can be accessed from another device

c - 在生成器宏中删除参数

makefile - 如何使用模块为 Fortran 编写 makefile?