【问题标题】:build error using CMake使用 CMake 构建错误
【发布时间】:2016-04-17 21:32:02
【问题描述】:

我在尝试使用 CMake 构建 flex and lemon 项目时遇到构建错误。你能帮我找出问题所在吗?

$ make
/usr/bin/cmake -H/home/dac/ClionProjects/openshell -B/home/dac/ClionProjects/openshell --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/dac/ClionProjects/openshell/CMakeFiles /home/dac/ClionProjects/openshell/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/dac/ClionProjects/openshell'
make -f CMakeFiles/lemon.dir/build.make CMakeFiles/lemon.dir/depend
make[2]: Entering directory '/home/dac/ClionProjects/openshell'
cd /home/dac/ClionProjects/openshell && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/dac/ClionProjects/openshell /home/dac/ClionProjects/openshell /home/dac/ClionProjects/openshell /home/dac/ClionProjects/openshell /home/dac/ClionProjects/openshell/CMakeFiles/lemon.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/dac/ClionProjects/openshell'
make -f CMakeFiles/lemon.dir/build.make CMakeFiles/lemon.dir/build
make[2]: Entering directory '/home/dac/ClionProjects/openshell'
make[2]: Nothing to be done for 'CMakeFiles/lemon.dir/build'.
make[2]: Leaving directory '/home/dac/ClionProjects/openshell'
/usr/bin/cmake -E cmake_progress_report /home/dac/ClionProjects/openshell/CMakeFiles  1
[ 16%] Built target lemon
make -f CMakeFiles/openshell.dir/build.make CMakeFiles/openshell.dir/depend
make[2]: Entering directory '/home/dac/ClionProjects/openshell'
cd /home/dac/ClionProjects/openshell && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/dac/ClionProjects/openshell /home/dac/ClionProjects/openshell /home/dac/ClionProjects/openshell /home/dac/ClionProjects/openshell /home/dac/ClionProjects/openshell/CMakeFiles/openshell.dir/DependInfo.cmake --color=
Scanning dependencies of target openshell
make[2]: Leaving directory '/home/dac/ClionProjects/openshell'
make -f CMakeFiles/openshell.dir/build.make CMakeFiles/openshell.dir/build
make[2]: Entering directory '/home/dac/ClionProjects/openshell'
/usr/bin/cmake -E cmake_progress_report /home/dac/ClionProjects/openshell/CMakeFiles 2
[ 33%] Building C object CMakeFiles/openshell.dir/main.c.o
/usr/bin/cc   -Wall -Werror -O3 -std=c99 -I/usr/include/readline    -o CMakeFiles/openshell.dir/main.c.o   -c /home/dac/ClionProjects/openshell/main.c
Linking C executable openshell
/usr/bin/cmake -E cmake_link_script CMakeFiles/openshell.dir/link.txt --verbose=1
/usr/bin/cc   -Wall -Werror -O3 -std=c99   CMakeFiles/openshell.dir/main.c.o CMakeFiles/openshell.dir/errors.c.o CMakeFiles/openshell.dir/util.c.o CMakeFiles/openshell.dir/stack.c.o CMakeFiles/openshell.dir/flex/shellparser.c.o  -o openshell -rdynamic -lreadline 
CMakeFiles/openshell.dir/main.c.o: In function `main':
main.c:(.text.startup+0xef): undefined reference to `yylex_init'
main.c:(.text.startup+0x100): undefined reference to `yyset_in'
main.c:(.text.startup+0x12e): undefined reference to `yylex'
main.c:(.text.startup+0x13a): undefined reference to `yyget_text'
main.c:(.text.startup+0x178): undefined reference to `yylex_destroy'
collect2: error: ld returned 1 exit status
CMakeFiles/openshell.dir/build.make:188: recipe for target 'openshell' failed
make[2]: *** [openshell] Error 1
make[2]: Leaving directory '/home/dac/ClionProjects/openshell'
CMakeFiles/Makefile2:98: recipe for target 'CMakeFiles/openshell.dir/all' failed
make[1]: *** [CMakeFiles/openshell.dir/all] Error 2
make[1]: Leaving directory '/home/dac/ClionProjects/openshell'
Makefile:78: recipe for target 'all' failed
make: *** [all] Error 2

我的 CMake 构建文件是

cmake_minimum_required (VERSION 2.6)
project (openshell)
set(CMAKE_VERBOSE_MAKEFILE on)
include_directories(/usr/include/readline)

#### Lemon bootstrap ####
ADD_EXECUTABLE(lemon lemon.c)


file(GLOB SOURCES "./*.c")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -O3 -std=c99")
add_executable(openshell main.c openshell.h errors.c errors.h util.c util.h stack.c stack.h flex/shellparser.c flex/shellscanner.l flex/shellscanner.h)
target_link_libraries(openshell readline)

【问题讨论】:

    标签: c cmake flex-lexer lemon


    【解决方案1】:

    您没有链接 Flex 库,因此您使用的任何 Flex 函数都会出现链接器错误,例如 yyset_in

    您可以使用find_package(FLEX),在使用FLEX_TARGET 定义您的Flex 目标之后,您可以使用类似以下的内容:

    # Note: I use "ShellScanner" below as in FLEX_TARGET(ShellScanner flex/shellscanner.l ...)
    add_executable(openshell
        ...
        flex/shellparser.c
        ${FLEX_ShellScanner_OUTPUTS}
    )
    target_link_libraries(openshell ${READLINE_LIBRARY} ${FLEX_LIBRARIES})
    

    更多信息请参见FindFLEX

    但是,我没有看到 FindReadline.cmake 模块与 CMake 默认提供的其他模块,因此您可能必须自己创建一个或使用现有的一个,例如我刚刚链接的那个。还有 Editline 库,它提供了类似的功能,您也可以使用现有的 FindEditline.cmake 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多