android / android/ndk

investigate using `#pragma comment(lib, ...)`

Open
#1,931 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
No language data
Stars
2.3k
Forks
310
PR merge metrics
No merged PRs in 30d

Description

> We just found a pretty big landmine with this feature. In the example we just encountered this on:
>
> ```c++
> if (__builtin_available(android 29, *)) {
> ATrace_beginAsyncSection("ndk::asyncBeginEndSection", 0);
> }
> ```
>
> The code is right. It includes a check for `__builtin_available` to make sure the app is running on a new enough version of the OS before making the maybe-available API call.
>
> The part that was wrong isn't visible here (and therefore isn't visible to the compiler, so it can't diagnose this): the build script forgot to link libandroid. The loader saw the weak symbol, couldn't find a definition for it, and set `ATrace_beginAsyncSection` to `nullptr`. `__builtin_available` returned true though, because the device we were running on was API 32, so the call segfaulted.

Could you add a `#pragma comment(lib, "android")` in the relevant headers so that including the header also ensures that you link against the library? That started out as a Windows thing, but Clang and LLD have supported it for ELF for a while now, and I believe e.g. Fuchsia uses it pretty extensively. I guess it'd still need to be guarded by an `__ANDROID_API__` check for libraries which were introduced in later API levels, so it's not foolproof, but it's still something.

_Originally posted by @smeenai in https://github.com/android/ndk/issues/837#issuecomment-1718722941_

Contributor guide

Open the contributing guide

Research direction

Start by identifying the relevant Android NDK headers that declare APIs such as ATrace_beginAsyncSection, then check how Clang and LLD handle #pragma comment(lib, "android") for ELF. Review the proposed __ANDROID_API__ guard requirements and verify that including each header supplies the intended library dependency without affecting unsupported API levels.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, cpp
Domain
build-system, mobile-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.