【问题标题】:Why can't I compile with OpenBSD?为什么我不能用 OpenBSD 编译?
【发布时间】:2016-05-14 10:28:03
【问题描述】:

在我添加柠檬解析器之前,我的程序使用 OpenBSD 编译得很好。现在它在 Linux 上编译,但在 OpenBSD 上我得到一个我不明白的错误。

$ cmake ..
-- The C compiler identification is GNU 4.2.1
-- The CXX compiler identification is GNU 4.2.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dac/test/openshell/build
$ make
Scanning dependScanning dependencies of target shell
[ 28%] Building C object CMakeFiles/shell.dir/main.c.o
/home/dac/test/openshell/main.c: In function 'main':
/home/dac/test/openshell/main.c:788: warning: implicit declaration of function 'add_history'
/home/dac/test/openshell/main.c: In function 'command':
/home/dac/test/openshell/main.c:573: warning: passing argument 1 of 'expandVariable' discards qualifiers from pointer target type
cc: -ledit: linker input file unused because linking not done
cc: -lncurses: linker input file unused because linking not done
cc: -lcurses: linker input file unused because linking not done
cc: -ltermcap: linker input file unused because linking not done
[ 42%] Building C object CMakeFiles/shell.dir/shellparser.c.o
/home/dac/test/openshell/shellparser.c:280: error: expected expression before '%' token
/home/dac/test/openshell/shellparser.c:288: error: expected expression before '%' token
/home/dac/test/openshell/shellparser.c:288: error: initializer element is not constant
/home/dac/test/openshell/shellparser.c:288: error: (near initialization for 'yyRuleName[0]')
/home/dac/test/openshell/shellparser.c: In function 'yy_destructor':
/home/dac/test/openshell/shellparser.c:370: error: expected expression before '%' token
/home/dac/test/openshell/shellparser.c:370: warning: statement with no effect
/home/dac/test/openshell/shellparser.c: In function 'yyStackOverflow':
/home/dac/test/openshell/shellparser.c:551: error: expected expression before '%' token
/home/dac/test/openshell/shellparser.c:551: warning: statement with no effect
/home/dac/test/openshell/shellparser.c: At top level:
/home/dac/test/openshell/shellparser.c:609: error: expected expression before '%' token
/home/dac/test/openshell/shellparser.c:609: warning: missing braces around initializer
/home/dac/test/openshell/shellparser.c:609: warning: (near initialization for 'yyRuleInfo[0]')
/home/dac/test/openshell/shellparser.c:609: error: initializer element is not constant
/home/dac/test/openshell/shellparser.c:609: error: (near initialization for 'yyRuleInfo[0].lhs')
/home/dac/test/openshell/shellparser.c: In function 'yy_reduce':
/home/dac/test/openshell/shellparser.c:664: error: expected expression before '%' token
/home/dac/test/openshell/shellparser.c:664: warning: statement with no effect
/home/dac/test/openshell/shellparser.c: In function 'yy_parse_failed':
/home/dac/test/openshell/shellparser.c:710: error: expected expression before '%' token
/home/dac/test/openshell/shellparser.c:710: warning: statement with no effect
/home/dac/test/openshell/shellparser.c: In function 'yy_syntax_error':
/home/dac/test/openshell/shellparser.c:726: error: expected expression before '%' token
/home/dac/test/openshell/shellparser.c:726: warning: statement with no effect
/home/dac/test/openshell/shellparser.c: In function 'yy_accept':
/home/dac/test/openshell/shellparser.c:745: error: expected expression before '%' token
/home/dac/test/openshell/shellparser.c:745: warning: statement with no effect
/home/dac/test/openshell/shellparser.c: At top level:
/home/dac/test/openshell/shellparser.c:918: error: expected identifier or '(' before string constant
/home/dac/test/openshell/shellparser.c:929: error: expected identifier or '(' before ',' token

为什么会这样?我该怎么办?我忘了包括一个图书馆吗?我的 cmake 文件是

cmake_minimum_required(VERSION 3.0)
project(shell.test)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -L/usr/local/include/ -L/usr/include -std=c99 -pedantic -O3 -g -Wall -pedantic -ledit -lncurses -lcurses -ltermcap")
include_directories(/usr/local/include/ /usr/include)
link_directories(/usr/lib)
link_directories(/usr/local/lib)
add_executable(shell main.c shellparser.c errors.c util.c)
target_link_libraries(shell edit readline)
add_custom_target(shellparser DEPENDS ${CMAKE_SOURCE_DIR}/shellparser.c)
add_custom_command(OUTPUT ${CMAKE_SOURCE_DIR}/shellparser.c COMMAND lemon -s ${CMAKE_SOURCE_DIR}/shellparser.y DEPENDS ${CMAKE_SOURCE_DIR}/shellparser.y)
add_dependencies(shell shellparser)
set_property(TARGET shell PROPERTY C_STANDARD 99)$ 

在 Ubuntu 上我可以成功构建它:

$ git clone http://github.com/montao/openshell
Cloning into 'openshell'...
remote: Counting objects: 1439, done.
remote: Compressing objects: 100% (184/184), done.
remote: Total 1439 (delta 124), reused 0 (delta 0), pack-reused 1255
Receiving objects: 100% (1439/1439), 405.08 KiB | 117.00 KiB/s, done.
Resolving deltas: 100% (973/973), done.
Checking connectivity... done.
dac@dac-Latitude-E7450:~/montao/test$ cd openshell/
dac@dac-Latitude-E7450:~/montao/test/openshell$ mkdir build
dac@dac-Latitude-E7450:~/montao/test/openshell$ cd build/
dac@dac-Latitude-E7450:~/montao/test/openshell/build$ cmake ..
-- The C compiler identification is GNU 5.3.1
-- The CXX compiler identification is GNU 5.3.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dac/montao/test/openshell/build
dac@dac-Latitude-E7450:~/montao/test/openshell/build$ make
Scanning dependencies of target shellparser
[ 14%] Generating ../shellparser.c
Parser statistics:
  terminal symbols...................     9
  non-terminal symbols...............     3
  total symbols......................    12
  rules..............................     8
  states.............................    14
  conflicts..........................     0
  action table entries...............    32
  total table size (bytes)...........   100
[ 14%] Built target shellparser
Scanning dependencies of target shell
[ 28%] Building C object CMakeFiles/shell.dir/main.c.o
/home/dac/montao/test/openshell/main.c: In function ‘free_pipeline’:
/home/dac/montao/test/openshell/main.c:337:6: note: the ABI of passing struct with a flexible array member has changed in GCC 4.4
 void free_pipeline(struct pipeline pipe) {
      ^
[ 42%] Building C object CMakeFiles/shell.dir/shellparser.c.o
[ 57%] Building C object CMakeFiles/shell.dir/errors.c.o
[ 71%] Building C object CMakeFiles/shell.dir/util.c.o
/home/dac/montao/test/openshell/util.c: In function ‘make_args’:
/home/dac/montao/test/openshell/util.c:1100:52: warning: operation on ‘jc’ may be undefined [-Wsequence-point]
             char *str = concat((char *) *retArgv[jc++], (char *) *retArgv[jc]);
                                                    ^
/home/dac/montao/test/openshell/util.c:1100:19: warning: unused variable ‘str’ [-Wunused-variable]
             char *str = concat((char *) *retArgv[jc++], (char *) *retArgv[jc]);
                   ^
[ 85%] Linking C executable shell
[100%] Built target shell

无论我做什么,使用 OpenBSD 编译时都会出现错误。我什么都试过了。

【问题讨论】:

  • 显然,知道至少一两行你的编译器在 openBSD 上失败的地方实际上会有帮助
  • @MarcusMüller 如果我使用 cmake 中的柠檬然后制作,我终于可以编译它了。 Lemon 会根据我调用它的方式从shellparser.y 生成不同的shellparser.c。我不确定细节,但至少我现在可以构建项目:-)
  • 如果你不添加答案,你并没有真正帮助任何人,而是你自己(你可以自我回答问题,非常感谢 SO!)并解释你做了什么,并实际展示不同的文件!除此之外,您的问题对未来的读者没有任何价值:(
  • @MarcusMüller 仍在寻找 为什么 它可以在 Ubuntu 上生成代码,然后使用 OpenBSD 运行它。但它有效。实际上我确实立即更新了构建指令 (github.com/montao/openshell)

标签: c compiler-errors cmake openbsd


【解决方案1】:

我没有使用过 OpenBSD,但这可能是由 2007 年 7 月 18 日发布的“古老”GCC 4.2.1 引起的。在 Ubuntu 上,它是使用更新的 GCC 编译的。

事实上,如果你检查 project you want to build 的依赖关系

依赖:editline、ncurses、C99

gcc 中的 C99 支持

从 GCC 4.5 开始基本上完全支持 C99(使用 -std=c99 -pedantic-errors;在 GCC 5 之前还需要 -fextended-identifiers 来启用扩展标识符)

【讨论】:

  • 有趣的线索!我认为,考虑到 OP 遇到的错误,我认为可能是他的解析器生成器没有按预期工作。这就是我要求生成的 .c 文件的原因; .y 文件不足以说明。
  • 无论我做什么我都会得到错误。我什么都试过了。
  • @Montao 您应该将-pedantic 更改为-pedantic-errors 以修复错误。可能您必须在main.c 中将#include <readline/history.h> 添加到OpenBSD ifdef。
  • 当我尝试这个例子时也会发生错误:stackoverflow.com/questions/34918631/… 所以我们有一个最小的方法来重现它。
【解决方案2】:

我可以用 OpenBSD 5.9 像这样编译它

gcc -std=c99 shellparser.c main.c util.c errors.c -ledit -ltermcap

如果我首先用 Linux 生成 shellparser.c 并将文件复制到 OpenBSD,那么上面的代码可以为 OpenBSD 构建。但是我仍然无法从 OpenBSD 的语法中生成 C,出现编译错误。我可以在 Ubuntu 上重新创建编译错误,当我运行 lemon shellparser.y

时,我仍在寻找导致生成的 C 中差异的原因

【讨论】:

    猜你喜欢
    • 2012-02-23
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多