question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing the header of an objc_library from another objc_library

See original GitHub issue

Hello:

Description of the problem:

I am trying to consume an objc_library from a objc_library as follows:

load("@rules_cc//cc:defs.bzl", "objc_library")
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_unit_test")

objc_library(
    name = "ObjcModule",
    srcs = ["Sources/ObjcModule.m"],
    hdrs = ["Sources/ObjcModule.h"],
    module_name = "ObjcModule",
)

objc_library(
    name = "ObjcModuleTestsLib",
    srcs = ["ObjcTests/SomeCodeTests.m"],
    deps = [":ObjcModule"],
)

ios_unit_test(
    name = "ObjcModuleTests",
    minimum_os_version = "8.0",
    deps = [":ObjcModuleTestsLib"],
)

The content of ObjcTests/SomeCodeTests.m is:

#import <XCTest/XCTest.h>
#import "ObjcModule/ObjcModule.h"

@interface ObjcModuleTests: XCTestCase
@end

@implementation ObjcModuleTests
- (void)test {
  XCTAssertEqual(text, @"Hello, World!"); // The text constant is declared inside ObjcModule.h
}
@end

When running bazel build //Libraries/ObjcModule:ObjcModuleTestsLib, in line #import "ObjcModule/ObjcModule.h" I am getting the error Libraries/ObjcModule/ObjcTests/SomeCodeTests.m:2:9: fatal error: 'ObjcModule/ObjcModule.h' file not found. The only way I can make it work is by using the full path from the WORKSPACE: #import "Libraries/ObjcModule/Sources/ObjcModule.h".

I expected #import "ObjcModule/ObjcModule.h" to work when doing module_name = "ObjcModule". Any idea why it doesn’t? Any workaround? Is this expected behaviour?

I am using bazel 0.29.1.

Thanks!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sergiocampamacommented, Oct 1, 2019

I don’t think that’s how objc modules are supposed to work, though I’m no expert in that subject. I believe with modules you are supposed to import objc modules using the @import ObjcModule statement, but I’m unclear on what the status is of that feature.

The expected way to import it is using the full path relative to the workspace, as you’ve already found. If what you really want is to have a shorter import path, you could in theory add an includes = ["."], attribute to the ObjcModule target, and then the directory where that target lives will be added as an include path, so you could just do #import "ObjcModule.h".

Keep in mind that sprinkling include attributes might slow down your compilation a bit as the compiler will have to stat more locations to find the imported headers.

1reaction
bigbangvncommented, Sep 1, 2021

Cool, thanks @vikage I used the trick from @acecilia https://github.com/bazelbuild/bazel/issues/9461#issuecomment-537270226 It works also but no need to depends on rules_ios

Read more comments on GitHub >

github_iconTop Results From Across the Web

Importing header in objective c - Stack Overflow
In Objective-c when we using object of one class into another class by convention we should forward declare the class in . h...
Read more >
bazelbuild/rules_swift - Aspect's Bazel Documentation
Any headers referenced by a module map that is imported into Swift must have only C features visible, often by using preprocessor conditions...
Read more >
Objective-C Rules - Bazel
This rule encapsulates an already-compiled static library in the form of an .a file. It also allows exporting headers and resources using the...
Read more >
Swift Package Manager with a Mixed Swift and Objective-C ...
This example assumes you have your internal headers in a directory named “Internal”. This is the name that will be used to import...
Read more >
[ROS] Include a Cpp header from another package
Learn how to include a header file in another package with ROS. First we'll create a library package, then install the library, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found