【问题标题】:gn+ninja link failed under ubuntu but workable on Arch Linuxgn+ninja 链接在 ubuntu 下失败,但在 Arch Linux 上可用
【发布时间】:2018-12-04 03:49:01
【问题描述】:

首先官方的gn例子在ubuntu(gcc 7.3.0)下是可以运行的。

git clone --depth=1 https://gn.googlesource.com/gn
cp -a gn/tools/gn/example .
cd example
gn gen out
ninja -C out

我已将hello.cc修改为

#include <stdio.h>

#include "hello_shared.h"
#include "hello_static.h"

#include <readline/readline.h>  // add this line
#include <readline/history.h>   // add this line

int main(int argc, char* argv[]) {
        // begin of test code
        int i;

        for (i=0; i<3; i++) {
                char *p=readline("try:");
                printf("%d: %s\n", i, p);
        }
        // end of test code
  printf("%s, %s\n", GetStaticText(), GetSharedText());
  return 0;
}

现在ninja -C out 显示以下错误消息我

ninja: Entering directory `out'
[1/1] LINK hello
FAILED: hello
g++ -Wl,-rpath=\$ORIGIN/ -Wl,-rpath-link= -o hello -Wl,--start-group @hello.rsp  -Wl,--end-group
obj/hello.hello.o: In function `main':
hello.cc:(.text+0x25): undefined reference to `readline'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

g++ hello.cc hello_static.cc hello_shared.cc -o hello -l readline 是可行的,这意味着readline 库存在并且可行。

在 Arch Linux (gcc 8.2.1) 下可以使用相同的测试过程

ubuntu环境是一个docker容器,就像Dockerfile一样

FROM ubuntu
RUN apt update -y
RUN apt upgrade -y
RUN apt install -y build-essential g++ unzip ninja-build pkg-config \
 libreadline-dev

【问题讨论】:

    标签: ninja gn


    【解决方案1】:

    对于较新的 gcc/g++ 8.x 或 gcc/g++ 4.x(CentOS6),ldflags 选项可以放在目标文件之前或之后。但是 gcc/g++ 7.x 只能放在目标文件之后。

    更改 example/build/toolchain/BUILD.gn
    tool("link") {
      ...
      command = "g++ {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end- group {{libs}}"
      ...
    }
    

    tool("link") {
      ...
      command = "g++ -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end- group {{libs}} {{ldflags}}"
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-29
      • 2012-01-17
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      相关资源
      最近更新 更多