nonocast / me

记录和分享技术的博客
http://nonocast.cn
MIT License
20 stars 0 forks source link

Swift & C (Part 3: mix compile) #287

Open nonocast opened 2 years ago

nonocast commented 2 years ago

通过Xcode可以很方便的在swift项目中加入bridge header和c file,通过命令行也可以实现, 下面介绍的方式是我个人尝试,未必是标准答案,辩证理解:

src/main.swift

import Foundation

func main() {
  let version = RTMP_LibVersion()
  print(String(format: "0x%08x", version))
  hello()
}

main()

src/bridge.h

#include "librtmp/amf.h"
#include "librtmp/log.h"
#include "librtmp/rtmp.h"

void hello(void);

Makefile

SRC=src
BUILD=build
TARGET=$(BUILD)/app

all: $(BUILD) $(TARGET)

$(TARGET): $(SRC)/main.swift $(BUILD)/foo.o
    swiftc -import-objc-header $(SRC)/bridge.h -Iinclude -lrtmp -Llib -o $@ $^

$(BUILD):
    @mkdir -p $@

$(BUILD)/foo.o : $(SRC)/foo.c
    clang -c -o $@ $<

run: $(TARGET)
    @$(TARGET)

clean:
    @rm -rf $(BUILD)

.PHONY: clean run all

code: streamr.zip