Ссылка gn+ninja не работает под Ubuntu, но работает в Arch Linux

Во-первых, официальный пример 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 показывает следующие сообщения об ошибках I

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 представляет собой контейнер докеров, например 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

person Daniel YC Lin    schedule 04.12.2018    source источник


Ответы (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}}"
  ...
}

to

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