# Compile objc and c

**URL:** https://discourse.cmake.org/t/compile-objc-and-c/4094
**Category:** Usage
**Tags:** os:macos, lang:objc
**Created:** [September 15, 2021, 9:40am UTC](https://discourse.cmake.org/t/compile-objc-and-c/4094 "2021-09-15T09:40:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Adonis](https://discourse.cmake.org/user_avatar/discourse.cmake.org/adonis/32/1536_2.png) [@Adonis](https://discourse.cmake.org/u/Adonis)
#### Post date: [September 15, 2021, 9:40am UTC](https://discourse.cmake.org/t/compile-objc-and-c/4094/1 "2021-09-15T09:40:35Z")

</div>

Hi, I want to build a dylib. but when I build it , I get error as follow:

```auto
In file included from /Users/apple/Documents/TEST/bluepulse-plugin/window-capture/library.c:3:
In file included from /Users/apple/Documents/TEST/bluepulse-plugin/window-capture/window-utils.h:2:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Cocoa.framework/Headers/Cocoa.h:12:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:492:1: error: expected identifier or '('
@class NSString, Protocol;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:494:9: error: unknown type name 'NSString'
typedef NSString * NSExceptionName NS_EXTENSIBLE_STRING_ENUM;
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:495:9: error: unknown type name 'NSString'
typedef NSString * NSRunLoopMode NS_EXTENSIBLE_STRING_ENUM;
        ^

```

Then I find solution in stackoverflow . [link](https://stackoverflow.com/questions/20962869/cmakelists-txt-for-an-objective-c-project), but I get this error

```auto
CMakeFiles/window-capture.dir/window-utils.m.o:3:478: warning: null character ignored [-Wnull-character]
CMakeFiles/window-capture.dir/window-utils.m.o:3:479: error: source file is not valid UTF-8

```

I cannot find solution , Could somebody help me ?  
This is my CMakeLists. And if I change the type of library to ‘STATIC’, it will be worked. I dont know why…

```auto
cmake_minimum_required(VERSION 3.20)
project(window-capture)

#set(CMAKE_CXX_STANDARD 17)

find_library(COREAUDIO CoreAudio)
find_library(AUDIOUNIT AudioUnit)
find_library(COREFOUNDATION CoreFoundation)
find_library(IOSURF IOSurface)
find_library(COCOA Cocoa)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -x objective-c")

include_directories(${COREAUDIO}
        ${AUDIOUNIT}
        ${COREFOUNDATION}
        ${IOSURF}
        ${COCOA})

set(window-capture_SOURCES_M
        window-utils.m
)
set_source_files_properties(${window-capture_SOURCES_M}
        PROPERTIES
        COMPILE_FLAGS "-fobjc-arc")

set(window-capture_SOURCES_C
        library.c
)
set(window-capture_SOURCES
        ${window-capture_SOURCES_C}
        ${window-capture_SOURCES_M}
        )

set(window-capture_HEADERS
        library.h
        window-utils.h
)

add_library(window-capture MODULE
        ${window-capture_SOURCES}
        ${window-capture_HEADERS})

target_link_libraries(window-capture
        ${COREAUDIO}
        ${AUDIOUNIT}
        ${COREFOUNDATION}
        ${IOSURF}
        ${COCOA})

install(TARGETS window-capture DESTINATION "${CMAKE_SOURCE_DIR}/build")

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 18, 2021, 3:05pm UTC](https://discourse.cmake.org/t/compile-objc-and-c/4094/2 "2021-09-18T15:05:52Z")

</div>

> [@Adonis](#):
>
> ```auto
> set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -x objective-c")
> 
> ```

Don’t do this. You’re confusing _every_ C compilation with this setup. Instead, you should set the `LANGUAGE` property of the source files which have a `.c` extension that you want to be compiled as Objective-C.

> [@Adonis](#):
>
> `project(window-capture)`

This should probably be `project(window-capture LANGUAGES C ObjC)`.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 23, 2021, 1:19pm UTC](https://discourse.cmake.org/t/compile-objc-and-c/4094/3 "2021-09-23T13:19:07Z")

</div>

> [@ben.boeckel](#):
>
> Instead, you should set the `LANGUAGE` property of the source files which have a `.c` extension that you want to be compiled as Objective-C.

Just for completion in case others run across this, it probably requires [`CMP0119`](https://cmake.org/cmake/help/git-master/policy/CMP0119.html) from CMake 3.20 to work. Before that, changing the extension to `.m` is likely required.
