2

Есть следующий Makefile:

.PHONY: clean

LEX = lex
YACC = yacc
CC = gcc
CFLAGS = -m32
CXXFLAGS = -m32

bcpp: lex.yy.o y.tab.o execute.o global.o load.o main.o storage.o util.o libtask.a
    $(CC) lex.yy.o y.tab.o main.o storage.o util.o -L. -lbc -ltask -o bcpp $(CXXFLAGS)
bc: lex.yy.o y.tab.o execute.o global.o load.o main.o storage.o util.o libtask.a
    $(CC) lex.yy.o y.tab.o main.o storage.o util.o -L. -lbc -ltask -o bc $(CFLAGS)
lex.yy.c: scan.l
    $(LEX) scan.l
y.tab.c y.tab.h: bc.y
    $(YACC) -d bc.y
lex.yy.o: lex.yy.c y.tab.h
    $(CC) -c lex.yy.c $(CFLAGS)
y.tab.o: y.tab.c y.tab.h
    $(CC) -c y.tab.c $(CFLAGS)
execute.o: execute.c bcdefs.h global.h proto.h
    $(CC) -c execute.c $(CFLAGS)
global.o: global.c bcdefs.h global.h libmath.h
    $(CC) -c global.c $(CFLAGS)
load.o: load.c bcdefs.h global.h proto.h
    $(CC) -c load.c $(CFLAGS)
libtask.a: execute.o global.o load.o
    ar -cvq libtask.a execute.o global.o load.o
main.o: main.c bcdefs.h global.h proto.h getopt.h
    $(CC) -c main.c $(CFLAGS)
storage.o: storage.c bcdefs.h global.h proto.h
    $(CC) -c storage.c $(CFLAGS)
util.o: util.c bcdefs.h global.h proto.h
    $(CC) -c util.c $(CFLAGS)
clean:
    rm -f *.o
    rm -f *.yy.*
    rm -f *.tab.*
    rm -f libtask.a
    rm -f bc
    rm -f bcpp

Для компиляции с помощью компилятора С использую следующую команду:

make bc

Всё компилируется без проблем.

А для компиляции с помощью компилятора С++ использую следующую команду:

make CC=g++ bcpp

И получаю такой набор ошибок:

lex scan.l
yacc -d bc.y
bc.y: warning: 3 shift/reduce conflicts [-Wconflicts-sr]
g++ -c lex.yy.c -m32
In file included from scan.l:33:0:
global.h:151:12: error: previous declaration of ‘int optind’ with ‘C++’ linkage
 extern int optind;
            ^
In file included from /usr/include/unistd.h:871:0,
                 from lex.yy.c:912:
/usr/include/getopt.h:71:12: error: conflicts with new declaration with ‘C’ linkage
 extern int optind;
            ^
In file included from bcdefs.h:58:0,
                 from scan.l:32:
scan.l:51:16: error: previous declaration of ‘int yywrap()’ with ‘C++’ linkage
 _PROTOTYPE(int yywrap, (void));
                ^
number.h:96:32: note: in definition of macro ‘_PROTOTYPE’
 #define _PROTOTYPE(func, args) func args
                                ^
lex.yy.c:956:29: error: conflicts with new declaration with ‘C’ linkage
 extern "C" int yywrap (void );
                             ^
scan.l: In function ‘int yylex()’:
scan.l:227:38: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
       yyerror ("illegal character: #");
                                      ^
scan.l:298:24: error: ‘input’ was not declared in this scope
      while ( ((c=input()) != '*') && (c != EOF)) 
                        ^
scan.l:303:20: error: ‘input’ was not declared in this scope
   while ( (c=input()) == '*') /* eat it*/;
                    ^
scan.l:324:59: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
        if (count != 2) yyerror ("NUL character in string.");
                                                           ^
scan.l:356:55: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
      yyerror ("illegal character: ^%c",yytext[0] + '@');
                                                       ^
scan.l:359:61: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
        yyerror ("illegal character: \\%03o", (int) yytext[0]);
                                                             ^
scan.l:361:47: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
        yyerror ("illegal character: %s",yytext);
                                               ^
make: *** [lex.yy.o] Error 1

Можно ли решить как-то данную проблему?

1
  • Чем оправдана потребность в компиляции то C, то C++ компилятором?
    – andrybak
    2 ноя 2014 в 17:04

1 ответ 1

3

Ошибки всязаны с попыткой скомпилировать c-код c++ компилятором. В общем случае это невозможно (языки разные). Иногда можно поправить код так чтобы он компилировался обоими компиляторами. Например в number.h (строка 96) написать:

#ifdef __cplusplus
#define extern "C" _PROTOTYPE(func, args) func args
#iflse //__cplusplus
#define  _PROTOTYPE(func, args) func args
#endif 

И далее подобные правки по остальным ошибкам компиляии... Простого решения не будет. Лучьше понять что привело к необходимости компилировать С программу С++ компилятором, и решить эту проблему.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.