GNU c -ansi option

 makefile and -ansi option

SRCDIR     = .
OBJDIR     = .
LIBDIR     = .
TARGETDIR  = ./lib
TARGETLIB  = $(LIBDIR)/testmake
TARGETOBJ  = $(OBJDIR)/testmake.o
TARGETSRC  = $(SRCDIR)/testmake.c
TARGETHEADER = $(SRCDIR)/testmake.h
INC = .
CC = g++
CCOPTIONS = -c -O -fPIC -Wno-deprecated -m64 -mtune=generic -mcmodel=small
LIBCC = $(CC)
LDOPTIONS = -m64
.SUFFIXES: .c .o
# rules
all: $(TARGETLIB)
$(TARGETLIB): $(TARGETOBJ)
$(LIBCC) $(LDOPTIONS) $(TARGETOBJ) -o $(TARGETLIB)
$(TARGETOBJ): $(TARGETHEADER) $(TARGETSRC)
$(CC) $(CCOPTIONS) -I$(INC) $(TARGETSRC) -o $(TARGETOBJ)
clean:
@rm -f $(TARGETOBJ) $(TARGETLIB)
install:
@cp -p $(TARGETLIB) $(TARGETDIR)

testmake.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <testmake.h>

int main()
{
printf("num=%s len=%d\n",num,strlen(num));
printf("chars=%s len=%d\n",chars,strlen(chars));
printf("chars2=%s len=%d\n",chars2,strlen(chars2));
return 0;
}

testmake.h

char num[] = {

    0x31, 0x32, 0x33, 0x34, 0x00,

    0x00, 0xFF, 0xFF, 0xFF, 0x00,

};

char chars[] = {0x41,0x42,0x43,0x00};

char chars2[128] = {'\0'};


make result:


# make -f testmake.mk

g++ -c -O -fPIC -Wno-deprecated -m64 -mtune=generic -mcmodel=small -I. ./testmake.c -o ./testmake.o

In file included from ./testmake.c:4:

./testmake.h:4:1: エラー: narrowing conversion of ‘255’ from ‘int’ to ‘cha ’ inside { } [-Wnarrowing]

 };

 ^

./testmake.h:4:1: エラー: narrowing conversion of ‘255’ from ‘int’ to ‘cha ’ inside { } [-Wnarrowing]

./testmake.h:4:1: エラー: narrowing conversion of ‘255’ from ‘int’ to ‘cha ’ inside { } [-Wnarrowing]

make: *** [testmake.mk:32: testmake.o] エラー 1


add -ansi to the compile option to compile the code without errors:

CCOPTIONS = -c -O -fPIC -Wno-deprecated -m64 -mtune=generic -mcmodel=small -ansi

# make -f testmake.mk

g++ -c -O -fPIC -Wno-deprecated -m64 -mtune=generic -mcmodel=small -ansi -I. ./testmake.c -o ./testmake.o

g++ -m64 ./testmake.o -o ./testmake


result of execution testmake:

# ./testmake

num=1234 len=4

chars=ABC len=3

chars2= len=0

#

コメント

人気の投稿