Skip to content

Commit e62ea58

Browse files
committed
Fix linking of classes under test in objc_foundation and objc_id
1 parent cb663f2 commit e62ea58

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

objc_foundation/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ use objc::runtime::Class;
2020

2121
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
2222
#[link(name = "gnustep-base", kind = "dylib")]
23-
extern { static _OBJC_CLASS_NSObject : Class; }
23+
extern "C" {}
24+
25+
// Split up to illustrate that the linking doesn't have to be annotated on the
26+
// correct `extern` block.
27+
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
28+
extern "C" {
29+
static _OBJC_CLASS_NSObject: Class;
30+
}
2431

2532
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
2633
#[allow(dead_code)]

objc_foundation/src/string.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ impl fmt::Display for NSString {
8484
mod tests {
8585
use super::{INSCopying, INSString, NSString};
8686

87+
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
88+
#[test]
89+
fn ensure_linkage() {
90+
unsafe { crate::get_class_to_force_linkage() };
91+
}
92+
8793
#[test]
8894
fn test_utf8() {
8995
let expected = "ประเทศไทย中华Việt Nam";

objc_id/src/id.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ mod tests {
206206
use objc::runtime::Object;
207207
use objc::{class, msg_send};
208208

209+
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
210+
#[test]
211+
fn ensure_linkage() {
212+
unsafe { crate::get_class_to_force_linkage() };
213+
}
214+
209215
fn retain_count(obj: &Object) -> usize {
210216
unsafe { msg_send![obj, retainCount] }
211217
}

objc_id/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ which can be cloned to allow multiple references.
1111
1212
Weak references may be created using the [`WeakId`](struct.WeakId.html) struct.
1313
14-
```
14+
```no_run
1515
# use objc::msg_send;
1616
use objc::runtime::{Class, Object};
1717
use objc_id::{Id, WeakId};
@@ -41,3 +41,23 @@ assert!(weak.load().is_none());
4141
pub use id::{Id, Owned, Ownership, ShareId, Shared, WeakId};
4242

4343
mod id;
44+
45+
// TODO: Remove the need for this hack
46+
47+
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
48+
use objc::runtime::Class;
49+
50+
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
51+
#[link(name = "gnustep-base", kind = "dylib")]
52+
extern "C" {}
53+
54+
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
55+
extern "C" {
56+
static _OBJC_CLASS_NSObject: Class;
57+
}
58+
59+
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
60+
#[allow(dead_code)]
61+
unsafe fn get_class_to_force_linkage() -> &'static Class {
62+
&_OBJC_CLASS_NSObject
63+
}

0 commit comments

Comments
 (0)