본문 바로가기

프로그래밍 언어/C

[C/Error] 'for' loop initial declarations are only allowed in C99 or C11 mode[Note] use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code : for loop 에러 문제 해결

 

[Error] 'for' loop initial declarations are only allowed in C99 or C11 mode[Note] use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code


 

mitosis-project/mitosis-workload-btree

The BTree workload used for evaluation. Contribute to mitosis-project/mitosis-workload-btree development by creating an account on GitHub.

github.com

 

 

mitosis-project/mitosis-workload-hashjoin

The HashJoin workload used for evaluation. Contribute to mitosis-project/mitosis-workload-hashjoin development by creating an account on GitHub.

github.com

위 벤치마크들을 make 하고 실행시키려고 하니 다음과 같은 에러가 떴다.

두 벤치마크 모두 같은 오류가 발생했고, 해결 방법도 같았다. 

더보기
gcc  -O3 -march=native src/main.c src/btree1.c -o bin/bench_btree_st  -lrt -ldl -lnuma -lpthread -lm
src/main.c: In function ‘main’:
src/main.c:81:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for (int i = 0; i < argc; i++) {
     ^
src/main.c:81:5: note: use option -std=c99 or -std=gnu99 to compile your code
src/main.c:128:14: error: redefinition of ‘i’
     for (int i = 0; i < argc; i++) {
              ^
src/main.c:81:14: note: previous definition of ‘i’ was here
     for (int i = 0; i < argc; i++) {
              ^
src/main.c:128:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for (int i = 0; i < argc; i++) {
     ^
src/btree1.c: In function ‘alloc_node’:
src/btree1.c:679:9: error: ‘for’ loop initial declarations are only allowed in C99 mode
         for (size_t i = 0; i < NODE_SLAB_GROW / sizeof(struct node); i++) {
         ^
src/btree1.c:679:9: note: use option -std=c99 or -std=gnu99 to compile your code
src/btree1.c: In function ‘alloc_record’:
src/btree1.c:711:9: error: ‘for’ loop initial declarations are only allowed in C99 mode
         for (size_t i = 0; i < RECORD_SLAB_GROW / sizeof(struct record); i++) {
         ^
src/btree1.c: In function ‘real_main’:
src/btree1.c:1636:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for (size_t i = 0; i < nelements; i++) {
     ^
src/btree1.c:1643:17: error: redefinition of ‘i’
     for (size_t i = nelements; i > 1; i--) {
                 ^
src/btree1.c:1636:17: note: previous definition of ‘i’ was here
     for (size_t i = 0; i < nelements; i++) {
                 ^
src/btree1.c:1643:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for (size_t i = nelements; i > 1; i--) {
     ^
src/btree1.c:1650:17: error: redefinition of ‘i’
     for (size_t i = 0; i < nelements; i++) {
                 ^
src/btree1.c:1643:17: note: previous definition of ‘i’ was here
     for (size_t i = nelements; i > 1; i--) {
                 ^
src/btree1.c:1650:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for (size_t i = 0; i < nelements; i++) {
     ^
src/btree1.c:1676:17: error: redefinition of ‘i’
     for (size_t i = 0; i < nlookup; i++) {
                 ^
src/btree1.c:1650:17: note: previous definition of ‘i’ was here
     for (size_t i = 0; i < nelements; i++) {
                 ^
src/btree1.c:1676:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for (size_t i = 0; i < nlookup; i++) {
     ^
make: *** [bin/bench_btree_st] Error  

C99를 적용시켜서 나지 않은 에러라 하니 Makefile을 수정해서 c99를 적용하는 옵션을 주기로 했다. 

 

결론은 컴파일시에 -std=gnu99 옵션을 줘야 한다는 것!

 

처음엔 std-c99 옵션을 줬더니 다음과 같은 에러가 떴다.

더보기
gcc  -O3 -march=native src/main.c src/btree1.c -o bin/bench_btree_st  -lrt -ldl -lnuma -lpthread -lm -std=c99 -std=c99 -D _BSD_SOURCE

src/main.c: In function ‘main’:
src/main.c:94:5: warning: implicit declaration of function ‘getopt’ [-Wimplicit-function-declaration]
     while ((c = getopt(argc, argv, "o:h")) != -1) {
     ^
src/main.c:102:34: error: ‘optarg’ undeclared (first use in this function)
             opt_file_out = fopen(optarg, "a");
                                  ^
src/main.c:102:34: note: each undeclared identifier is reported only once for each function it appears in
src/main.c:109:21: error: ‘optopt’ undeclared (first use in this function)
             switch (optopt) {
                     ^
src/main.c:126:5: error: ‘optind’ undeclared (first use in this function)
     optind = 1;
     ^
src/main.c:152:22: error: storage size of ‘sigact’ isn’t known
     struct sigaction sigact;
                      ^
src/main.c:155:5: warning: implicit declaration of function ‘sigfillset’ [-Wimplicit-function-declaration]
     sigfillset(&block_set);
     ^
src/main.c:156:5: warning: implicit declaration of function ‘sigdelset’ [-Wimplicit-function-declaration]
     sigdelset(&block_set, SIGUSR1);
     ^
src/main.c:158:5: warning: implicit declaration of function ‘sigemptyset’ [-Wimplicit-function-declaration]
     sigemptyset(&sigact.sa_mask);
     ^
src/main.c:161:5: warning: implicit declaration of function ‘sigaction’ [-Wimplicit-function-declaration]
     sigaction(SIGUSR1, &sigact, NULL);
     ^
src/btree1.c: In function ‘allocate’:
src/btree1.c:120:5: warning: implicit declaration of function ‘posix_memalign’ [-Wimplicit-function-declaration]
     if (posix_memalign(&memptr, alignment, size)) {
     ^
src/btree1.c: In function ‘real_main’:
src/btree1.c:1604:5: warning: implicit declaration of function ‘getopt’ [-Wimplicit-function-declaration]
     while ((c = getopt(argc, argv, "n:l:")) != -1) {
     ^
src/btree1.c:1607:32: error: ‘optarg’ undeclared (first use in this function)
             nelements = strtol(optarg, NULL, 10);
                                ^
src/btree1.c:1607:32: note: each undeclared identifier is reported only once for each function it appears in
make: *** [bin/bench_btree_st] Error 1

 

오류 해결한 수정 Makefile

###############################################################################
# Makefile to build the HashJoin workload
#
# Paper: Mitosis - Mitosis: Transparently Self-Replicating Page-Tables
#                  for Large-Memory Machines
# Authors: Reto Achermann, Jayneel Gandhi, Timothy Roscoe,
#          Abhishek Bhattacharjee, and Ashish Panwar
###############################################################################

CC = gcc
CXX = g++

# cflags used
CFLAGS=-O3 -march=native
CXXFLAGS=-O3 -march=native

# common libraries used
COMMON_LIBS=-lrt -ldl -lnuma -lpthread -lm

# custom options
OPTIONS=-std=gnu99

# common main functions for multithreaded, single threaded and page-table dump
SRC_M=src/main.c src/hashjoin.c src/murmurhash.c
SRC_S=src/main.c src/hashjoin.c src/murmurhash.c
DEPS=src/config.h src/murmur3.h

# the targets
all : bin/bench_hashjoin_st bin/bench_hashjoin_mt

###############################################################################
# HashJoin
###############################################################################

bin/bench_hashjoin_st: $(SRC_S) $(DEPS)
	$(CC) $(INCLUDES) $(CFLAGS) $(SRC_S) $(OPTIONS) -o $@  $(COMMON_LIBS)

bin/bench_hashjoin_mt:  $(SRC_M) $(DEPS)
	$(CC) $(INCLUDES) $(CFLAGS) $(OPTIONS) -fopenmp $(SRC_M) -o $@  $(COMMON_LIBS)

###############################################################################
# Clean
###############################################################################

clean:
	rm -rf bin/*

 

 

OPTIONS=-std=gnu99

변수를 만들고,

$(OPTIONS)를 추가해준다. 또는 그냥 직접 -std=gnu99를 라인에 추가해줘도 되는데 나는 다른 옵션들이 더 필요할까봐 그냥 변수로 추가했다. 

 

Makefile을 수정하고 다시 $ make를 해보니 다음과 같이 에러 없이 실행된다. 

gcc  -O3 -march=native src/main.c src/hashjoin.c src/murmurhash.c -std=gnu99 -o bin/bench_hashjoin_st  -lrt -ldl -lnuma -lpthread -lm
gcc  -O3 -march=native -std=gnu99 -fopenmp src/main.c src/hashjoin.c src/murmurhash.c -o bin/bench_hashjoin_mt  -lrt -ldl -lnuma -lpthread -lm

 

각 벤치마크 실행 방법은 위 github의 README 참고!

 

 


References