Compile objc and c

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

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, but I get this error

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…

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")

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.

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

Just for completion in case others run across this, it probably requires CMP0119 from CMake 3.20 to work. Before that, changing the extension to .m is likely required.

1 Like