0

一开始官方的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
4

1 回答 1

0

对于较新的 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}}"
  ...
}
于 2018-12-04T08:24:32.400 回答