georgemp
georgemp

Reputation: 824

Build Objective-C Library with CMake with ARC enabled

I am trying to build an Objective-C ARC enabled library using CMake. When using the "Unix Makefiles" generator I run into a warning:

method possibly missing a [super dealloc] call

I don't run into this warning when using the XCode generator. Is there a flag I can pass to CMake to make sure that the command line build also recognizes this to be an ARC build and not have that warning generated?

Thanks

Upvotes: 4

Views: 7076

Answers (4)

Frederik
Frederik

Reputation: 727

Another option if you want all Objective-C(++) files to be built with ARC:

set(CMAKE_OBJC_FLAGS "-fobjc-arc")
set(CMAKE_OBJCXX_FLAGS "-fobjc-arc")

Upvotes: 1

Ad N
Ad N

Reputation: 8406

An alternative approach is to specify per-target compiler flags. This could be considered more inline with modern CMake:

target_compile_options(target_name PUBLIC "-fobjc-arc")


Note: using PUBLIC will transitively forward this compiler flag to other targets depending on this one. Replacing by PRIVATE will prevent this propagation.

Upvotes: 6

Yuchen
Yuchen

Reputation: 33126

You need to let CMake know that you want to build the project with ARC. Otherwise, it will show the warning.

Option 1

However, CTSetObjCArcEnabled is only available only if we have cmake-toolkit installed. If it is not installed, you can use the following:

set_property (TARGET target_name APPEND_STRING PROPERTY 
              COMPILE_FLAGS "-fobjc-arc")

Option 2 (deprecated since 3.0)

Use CTSetObjCARCEnabled. Reference is available here:

Enables or disables Objective-C Automatic Reference Counting on a per-directory, per-target or per-source basis.

CTSetObjCARCEnabled(<value>  
        <DIRECTORY | TARGETS targets... | SOURCES sources... >)

Useful Tip

Also, as recommended from this answer, it is helpful to use the following to make sure the project is compiled with ARC enabled:

#if ! __has_feature(objc_arc)
#error "ARC is off"
#endif

Upvotes: 11

frinkr
frinkr

Reputation: 664

XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC works for me. See https://github.com/forexample/testapp/blob/master/CMakeLists.txt

set_target_properties(
    ${APP_NAME}
    PROPERTIES
    MACOSX_BUNDLE YES
    XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES  
)

Upvotes: 3

Related Questions