I have an Xcode project with an obj-c .c file and a .h file aswell as a .swift file where I am calling functions from those obj-c files with a bridging header but when I build my project I get a duplicate symbols error and Xcode doesn't show where.
here is .h header file:
#define smc_h
#include <stdint.h>
#include <mach/mach.h>
#include <IOKit/IOKitLib.h>
typedef struct {
uint32_t datasize;
uint32_t datatype;
uint8_t data[8];
} SMCVal_t;
io_connect_t conn;
kern_return_t openSMC(void);
kern_return_t closeSMC(void);
kern_return_t readSMC(char *key, SMCVal_t *val);
double convertToFahrenheit(SMCVal_t *val);
#endif /* smc_h */
my .c implementation file:
#include "smc.h"
kern_return_t openSMC(void) {
kern_return_t result;
kern_return_t service;
io_iterator_t iterator;
service = IOServiceGetMatchingServices(kIOMainPortDefault, IOServiceMatching("AppleSMC"), &iterator);
if(service == 0) {
printf("error: could not match dictionary");
return 0;
}
result = IOServiceOpen(service, mach_task_self(), 0, &conn);
IOObjectRelease(service);
return 0;
}
kern_return_t closeSMC(void) {
return IOServiceClose(conn);
}
kern_return_t readSMC(char *key, SMCVal_t *val) {
kern_return_t result;
uint32_t keyCode = *(uint32_t *)key;
SMCVal_t inputStruct;
SMCVal_t outputStruct;
inputStruct.datasize = sizeof(SMCVal_t);
inputStruct.datatype = 'I' << 24; //a left shift operation. turning the I into an int by shifting the ASCII value 24 bits to the left
inputStruct.data[0] = keyCode;
result = IOConnectCallStructMethod(conn, 5, &inputStruct, sizeof(SMCVal_t), &outputStruct, (size_t*)&inputStruct.datasize);
if (result == kIOReturnSuccess) {
if (val -> datasize > 0) {
if (val -> datatype == ('f' << 24 | 'l' << 16 | 't' << 8 )) { //bit shifting to from 32bit operation associated with the ASCII charecters'f', 'l', and 't', sets datatype field.
double temp = *(double *)val -> data;
return temp;
}
}
}
return 0.0;
}
double convertToFahrenheit(SMCVal_t *val) {
if(val -> datatype == ('f' << 24 | 'l' << 16 | 't' << 8 )) { //checking if val->datatype is equal to the result of the bit-shifting operation.
double temp = *(double *)val -> data;
return (temp * 9.0 / 5.0) + 32.0;
}
return 0.0;
}
And my .swift file where my objc functions are called:
import IOKit
public class CPUTempCaller {
public struct SMCVal_t {
var datasize: UInt32
var datatype: UInt32
var data: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)
}
@_silgen_name("openSMC")
func openSMC() -> Int32
@_silgen_name("closeSMC")
func closeSMC() -> Int32
@_silgen_name("readSMC")
func readSMC(key: UnsafePointer<CChar>?,val: UnsafeMutablePointer<SMCVal_t>) -> kern_return_t
@_silgen_name("convertToFahrenheit")
func convertToFahrenheit(val: UnsafePointer<SMCVal_t>) -> Double {
let openSM = openSMC()
guard openSM == 0 else {
print("Failed to open SMC: \(openSM)")
return 0.0;
}
let closeSM = closeSMC()
guard closeSM == 0 else {
print("could not close SMC: \(closeSM)")
return 0.0;
}
func convertAndPrintTempValue(key:UnsafePointer<CChar>?,scale: Character, showTemp: Bool ) -> Double? {
var SMCValue = SMCVal_t(datasize: 0, datatype: 0, data:(0,0,0,0,0,0,0,0)) //initializing SMC value
if let Key = key { //check if nil. If not nil, proceed to code block execution
let key = "TC0P"
let keyCString = (key as NSString).utf8String //passing key as null terminated utf8 string
let readSMCResult = readSMC(key: keyCString, val: &SMCValue) //call readSMC obj-C function, store result in "readSMCResult"
if readSMCResult != KERN_SUCCESS {
print("Failed to read SMC: \(readSMCResult)")
}
}
if showTemp { //return nil if showTemp is false
let convertRawToFahrenheit = convertToFahrenheit(val: &SMCValue)
let scaleToStr = String(scale)
print(String(format: "Temperature: %0.1f °%c", convertRawToFahrenheit, scaleToStr))
return nil
} else {
print("could not convert temperature and format values in Fahrenheit")
return nil
}
}
return 0.0;
}
}