Buck: Undefined reference when using pthread

Created on 28 Jul 2017  路  4Comments  路  Source: facebook/buck

Compiling a small pthread example results in an undefined reference. It seems that Buck is not handling the linker_flags correctly.

Repo for this issue: https://github.com/njlr/buck-pthread-example

Here is the BUCK file:

cxx_binary(
  name = 'main',
  srcs = [
    'main.cpp',
  ],
  linker_flags = [
    '-lpthread',
  ],
)

Here is the error:

$ buck build :main 
buck-out/gen/main#compile-main.cpp.oa5b6a1ba,default/main.cpp.o: In function `main':
main.cpp:(.text+0x98): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

BUILD FAILED: //:main#binary failed with exit code 1:
c++ link
stderr: buck-out/gen/main#compile-main.cpp.oa5b6a1ba,default/main.cpp.o: In function `main':
main.cpp:(.text+0x98): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

[-] PROCESSING BUCK FILES...FINISHED 0.1s [100%] 馃悓  New Buck instance
[-] DOWNLOADING... (0.00 B/S AVG, TOTAL: 0.00 B, 0 Artifacts)
[-] BUILDING...FINISHED 0.4s [100%] (2/3 JOBS, 1 UPDATED, 1 [33.3%] CACHE MISS)

Compiling manually works:

c++ main.cpp -o main -lpthread

My setup:

$ buck --version
buck version v2017.05.31.01
$ c++ --version
c++ (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
P3

Most helpful comment

Work-around:

prebuilt_cxx_library(
  name = 'pthread',
  header_only = True,
  exported_linker_flags = [
    '-lpthread',
  ],
)

cxx_binary(
  name = 'main2',
  srcs = [
    'main.cpp',
  ],
  deps = [
    ':pthread',
  ],
)

Which gives us this argfile:

buck-out/gen/main2
"buck-out/gen/main2#compile-main.cpp.oa5b6a1ba,default/main.cpp.o"
-lpthread

Note that -lpthread is now after the object file main.cpp.o.

All 4 comments

Work-around:

prebuilt_cxx_library(
  name = 'pthread',
  header_only = True,
  exported_linker_flags = [
    '-lpthread',
  ],
)

cxx_binary(
  name = 'main2',
  srcs = [
    'main.cpp',
  ],
  deps = [
    ':pthread',
  ],
)

Which gives us this argfile:

buck-out/gen/main2
"buck-out/gen/main2#compile-main.cpp.oa5b6a1ba,default/main.cpp.o"
-lpthread

Note that -lpthread is now after the object file main.cpp.o.

I just ran into this too, writing a buck build file for libuv. Building libuv itself works fine, as does building a library that depends on it, but when I build a binary that depends on it, either directly or indirectly, it fails to build.

@Qard You might find this interesting: https://github.com/buckaroo-pm/libuv/blob/v1.x/BUCK

Oh, nice. Any plans on tracking latest stable? It's a bit out-of-date at the moment.

Was this page helpful?
0 / 5 - 0 ratings