Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-lpthread in BUILD breaks Android projects that depend on protobuf #1373

Closed
andrewharp opened this issue Apr 1, 2016 · 6 comments
Closed

Comments

@andrewharp
Copy link
Contributor

Hi,
Android supports pthreads, but does not provide a libpthread.so library to link against. The functionality is provided in other system libraries.

Since protobuf/BUILD specifies "-lpthread", I've had to add some hacks to get around this to get the Android Tensorflow demo working by creating a dummy pthread library, which is "linked" against and bundled in the APK: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/BUILD

Now that I'm trying to make a standalone Android executable it's becoming even more problematic as the binary expects to find the fake libpthread.so lib somewhere on its library path.

Is there a better way to build protobuf for Android? Or can the -lpthread flag be made selective on build platform, so that these workarounds are not necessary?

thanks!

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Apr 2, 2016

You should be able to use bazel selectors to selectively include "-lpthread" on non android platforms. See an example of the selector:
https://github.com/google/protobuf/blob/master/BUILD#L588

You probably need to contact the bazel guys about how to select on android though.

@andrewharp
Copy link
Contributor Author

The problem isn't how to include "-lpthread" for non-Android -- we don't need to do anything for this.

The question is how can we negate the "-lpthread" in protobuf/BUILD? It seems like this requires an edit to the protobuf source. I am not aware of any way to do this in the dependent project unfortunately.

Are there any examples of other Android projects that build with Bazel and use protobuf that you know of?

@andrewharp
Copy link
Contributor Author

The comment there says "Bazel should provide portable link_opts for pthread". It's not clear to me if this is a TODO, or a note to users about how to build for mobile. Googling "bazel portable link_opts" just takes me back to that file.

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Apr 2, 2016

What I am proposing is:

    linkopts = select({
        "//conditions:default": ["-pthread"],
        "<android>": [],
    }),

though I don't know how to select android in bazel.

@andrewharp
Copy link
Contributor Author

Ah, gotcha (I'd thought you were talking about modifying Tensorflow somehow).

We select for Android in Tensorflow like this. //tensorflow:android is defined here.

There may be a better way to do it, but if nothing else we could just copy that config_setting into protobuf/BUILD and select against it when defining LINK_OPTS. Happy to do this myself if it sounds good with you.

@andrewharp
Copy link
Contributor Author

Fixed by #1386

icota added a commit to icota/autoconf-archive that referenced this issue Dec 31, 2019
The android libc, bionic, provides built-in support for pthreads, so no additional linking is necessary.
Leaving -pthread breaks the build, see protocolbuffers/protobuf#1373
icota added a commit to icota/autoconf-archive that referenced this issue Dec 31, 2019
The android libc, bionic, provides built-in support for pthreads, so no additional linking is necessary.
Leaving -pthread breaks the build, see protocolbuffers/protobuf#1373
icota added a commit to icota/autoconf-archive that referenced this issue Dec 31, 2019
The android libc, bionic, provides built-in support for pthreads, so no additional linking is necessary.
Leaving -pthread breaks the build, see protocolbuffers/protobuf#1373
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants