How to include external header files in cc_binary build

1,050 views
Skip to first unread message

Netanel Hadad

unread,
Jan 9, 2023, 2:29:05 PM1/9/23
to Android Building
Hi,

I'm trying to include cpp and header files from the external folder in soong build.

I saw here that for cpp files I can use filegroup and it indeed works, but I didn't find a way to include header files.

In the example below, I want to include common files in both Foo and Bar Android.bp.

- Root
- - Android.bp
-
- - Common(folder)
- - - Common.cpp
- - - Common.h
- - - Defs.h
-
- - Foo(folder)
- - - Android.bp
- - - Foo.cpp
- - - Foo.h
-
- - Bar(folder)
- - - Android.bp
- - - Bar.cpp
- - - Bar.h

Regards,
Netanel

Dan Willemsen

unread,
Jan 9, 2023, 11:39:35 PM1/9/23
to android-...@googlegroups.com
A common way for this to work would be to use a static library:

Common/Android.bp:

  cc_library_static {
      name: "libCommon",
      srcs: ["Common.cpp"],
      export_include_dirs: ["."],
  }

Foo/Android.bp:

  cc_library_shared {
      name: "libFoo",
      static_libs: ["libCommon"],
      srcs: ["Foo.cpp"],
  }

That works best if the common code is actually common, and doesn't need to try to include Foo.h or Bar.h at build time. It also works in the inverse case, where Foo and Bar can be independently built static libraries included by Common. It has the advantage that there's only one link that provides both the sources and headers, so you can't accidentally forget one, or change one but not the other. It'll also only build Common.cpp once, even if it's used by both libFoo and libBar.

If that's not the case, I'd suggest some refactoring :-) But it is possible to use a header-only library instead, along with a filegroup for the source:

Common/Android.bp:

  filegroup {
      name: "CommonSrcs",
      srcs: ["Common.cpp"],
  }
  cc_library_headers {
      name: "libCommonHdrs",
      export_include_dirs: ["."],
  }

Foo/Android.bp:

  cc_library_shared {
      name: "libFoo",
      header_libs: ["libCommonHdrs"],
      srcs: ["Foo.cpp", ":CommonSrcs"],
  }

cc_library_headers is really meant to be used only for header-only libraries -- cases where there are no source files, and everything you need is in the header files. So this is outside the expected use cases, which may cause problems as the build system evolves in the future (though misuses of this are fairly common).

- Dan

--
--
You received this message because you are subscribed to the "Android Building" mailing list.
To post to this group, send email to android-...@googlegroups.com
To unsubscribe from this group, send email to
android-buildi...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-building?hl=en

---
You received this message because you are subscribed to the Google Groups "Android Building" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-buildi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-building/b2e5a425-90a8-4358-920b-86b7e2def8d6n%40googlegroups.com.

Netanel Hadad

unread,
Jan 10, 2023, 1:47:11 PM1/10/23
to Android Building
Hi Dan,

Thank you very much for the in-depth and detailed explanation!
I really appreciate the time and seriousness you put into the answer.
It definitely sorted me out.

Netanel

ב-יום שלישי, 10 בינואר 2023 בשעה 06:39:35 UTC+2, dwill...@google.com כתב/ה:
Reply all
Reply to author
Forward
0 new messages