Calling flex from a makefile

旧时模样 提交于 2019-12-02 12:50:15
all: a.out

lex.yy.c: scanner.l
    lex scanner.l

a.out: lex.yy.c main.c hash.c
    gcc -o a.out main.c hash.c -I.  

Try this:

lex.yy.c: scanner.l
    lex scanner.l

comp: main.c hash.c
    gcc -o a.out main.c hash.c -I.

main.c: lex.yy.c

The first rule set tells make that lex.yy.c needs to be rebuilt any time scanner.l changes and provides the command to recreate lex.yy.c. The second rule set tells make that the fake target comp depends on main.c and hash.c. If either file changes, then invoking make comp will cause a recompile. The last line is a stand-alone dependency that tells make to consider main.c as dirty any time that lex.yy.c changes. It will also force an invocation of make comp to create lex.yy.c if it does not exist.

Remove the backslash, or add a semicolon (;) before it.
As it is now, the two commands are added together on one line, and executed as one long command.

IIRC the usual Makefle pattern is

a.out: lex.yy.c main.c hash,c
<tab> gcc -o a.out main.c hash.c lex.yy.c -I. -ll

lex.yy.c: scanner.l
<tab> lex scanner.l

This is wrong, because in the original code, main.c includes lex.yy.c

This assumes the original code is changed so that main.c does not include lex.yy.c

Without that change, this will fail because there will be two definitions of yylex(), one from its #include in main.c, and one because it is supplied as a source code compilation unit. I encourage folks to not include a .c file into another .c file.

In general, the convention is to use a different file extension (.i) for included source files which generate unique symbols and code.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!