I'd like to use libclang in my ios app for syntax highlighting.
I've cross-compiled clang for ios (armv7) and i'm able to use it for tokenize, diagnostics, etc. But when i try to tokenize/get diagnostics for .cpp file which have #include <stdio.h> or iostream or cstdio.h libclang outputs fatal diagnostics that header file is not found.
Do i have to cross-compile libc++, upload cross compiled headers and static lib to device and add according -I to libclang command-line arguments?
libc++ already exists on iOS. You don't need to do anything special to access it. Just link against /usr/lib/libc++.dylib in the iOS SDK.
Related
Full disclosure: total iOS development amateur here. My background is embedded firmware development.
TLDR: I'm trying to include a shared library, written in C, into a swift project, but getting "undefined symbols" errors
For a project, I'm trying to include a shared library written in C, that I manually cross compiled for ios-arm64 architecture (using xcrun -sdk iphoneos clang -arch arm64 on a macbook ), and I'm trying to include this into a swift based iOS application: as the most simple example let's take the default swift application you get when starting a new iOS application in xcode.
If I understand correctly, the way I should approach this, is adding a target framework to the project (Editor > Add Target > Framework ) and then dragging my shared library (let's name it libcustom.dylib) and my header (custom.h) into the target framework. And then adding this custom header into the framework header file. I also set the framework settings to embed & sign the shared library.
Next, I try to call a function in this shared library in the ViewController.swift file. So what I do is import the framework header and then call the function, let's call it perform_custom_action() (xcode editor also autocompletes it), but when trying to build I get the following error:
Undefined symbol: _perform_custom_action
So there is a linking issue, probably it's not linking the library, but I dont know how I can link it in this xcode environment. I see in the compile output that it includes the framework, but I dont know if this framework is correctly including the library...
Does anyone know how I can fix this?
PS: some sanity checks:
libcustom.dylib is cross-compiled for arm64, output of file libcustom.dylib is:
libcustom.dylib: Mach-O 64-bit dynamically linked shared library arm64
_perform_custom_action is part of the symbols inside libcustom.dylib, when executing nm libcustom.dylib I get
...
00000000005a544 T _perform_custom_action
...
EDIT: extra info:
When creating the target framework, a bridging header is automatically created, after adding my own header to it, it looks like this:
//
// custom_framework.h
// custom_framework
//
// Created by <user> on 13.01.22.
//
#import <Foundation/Foundation.h>
//! Project version number for custom_framework.
FOUNDATION_EXPORT double custom_frameworkVersionNumber;
//! Project version string for custom_framework.
FOUNDATION_EXPORT const unsigned char custom_frameworkVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <custom_framework/PublicHeader.h>
#import "custom_framework/custom.h"
EDIT2: when manually setting linker flags, namely:
Library Search Paths : set to $(PROJECT_DIR)/custom_framework
Other Linker Flags : set to -lcustom
Then it builds succesfully, so to me it looks like something is wrong in the framework
I have a Qt project that was developed on MacOS. I've been given the job to make it compile on Windows.
My current problem is that compiler (Microsofts LLVM clang-cl) seems to be ignoring objective-c++ files, and the QtCreator is freaking out on #import statement (#import of type library is an unspported Microsoft feature). It also freaks out about a lot of stuff after that, but I'm guessing it's due to the import not being handled properly.
I was led to believe that win32-clang-mvsc was the only QtCreator compiler that supports compilation of objective-c++ files on windows, however, it doesn't seem to support the #import statement.
I've looked around and it seems that I would need GNUstep to be able to compile that on windows, and I'm not entirely sure it would work.
Is it possible to use the whichever compiler GNUstep provides with QtCreator? Or is there some way to use the compiler I'm already using to compile the files with #import statement?
The error I'm getting on the build is that ***.obj file doesn't exists, where *** is the name of the file with the #import statement. I'm guessing the compiler skips it and doesn't generate the .obj file, so something can't find it. I'd guess it was the linker, but I'd expect linker errors then, not a generic file not found error.
You can use the GNUstep Windows MSVC Toolchain to build Objective-C code using Clang on Windows. We’re also using this in a Qt project. You’ll just need to tell your build system to use the necessary flags for Objective-C files (e.g. using QMAKE_EXTRA_COMPILERS if you’re using QMake). This Qt example project for using Objective-C in a Qt project for Android should get you started.
The error you’re seeing about #import is likely because your project is using some clang-cl flags like /TC//TP that force the compiler to treat the file as C/C++ irrespective of its extension. Removing these flags should make this error disappear.
Please open issues in the GitHub project linked above if you need further help with the setup.
I have a dylib file on my macbook, and I want to use it in an iPhone application I create. I try using dlopen as usual:
#include <dlfcn.h>
char* dylibPath = "/Users/benams/lib.dylib";
void* libHandle = dlopen(dylibPath, RTLD_NOW);
libHandle is null and I guess the reason is that the dylib file doesn't exist on my iPhone. How can I solve it and use this dylib when I debug my app?
In general you can open dynamic libraries on iOS, too. But you can't use a dynamic librray for macOS on iOS, because they have different processor architecturs (x86_64 vs. arm?). You have to compile your library for arm32 and arm64 processors. If you want deploy your app (in the app store), you must also code sign they library.
But it is much easier to create and include a static library for iOS, or use use a shared framework. Note: In general dynamic libraries or shared frameworks will only make sense when reused for multiple executables (e. g. app and its extensions).
So I have to import some YML files into my latest swift project, and I found this lovely library (https://github.com/behrang/yaml.swift) which seems to work beautifully. The problem is, it's an iOS project and by default yaml.swift compiles for OSX.
Not too big of a deal, I changed this line in the makefile
sdk = $$(xcrun --show-sdk-path --sdk macosx)
to this
sdk = $$(xcrun --show-sdk-path --sdk iphoneos)
As soon as I make this switch, I get all sorts of errors when I compile.
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk/usr/include/mach/ndr.h:37:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk/usr/include/mach/ndr.h:37:
#include <libkern/OSByteOrder.h>
^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk/usr/include/libkern/OSByteOrder.h:45:10: error: 'libkern/machine/OSByteOrder.h' file not found
#include <libkern/machine/OSByteOrder.h>
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/shims/shims.h:13:10: error: could not build module 'Darwin'
#include <stddef.h> // for size_t
^
<unknown>:0: error: could not build Objective-C module 'SwiftShims'
make: *** [build/libyaml.dylib] Error 1
That's the last few lines, but this goes back for a few thousand lines. Nothing that I can find appears to be related to the library I'm trying to compile. It almost seems as if it's a problem with the SDK. I've tried digging and seeing whether or not the library is dependent on OSX frameworks, but the only thing it imports is Foundation.
Any ideas what the error could be related to?
What I've come up with so far is, SwiftShims is a swift module (NOT Objective-C as the error says). At the top of the SwiftShims.swift file, it appears to be importing some Darwin/C modules. Perhaps this is the issue? But SwiftShims isn't being imported in any of the swift files for this module. I'm really lost here, I'd appreciate any help I could get!
You also need to tell the swift compiler to build for the x86 cpu from which your simulator is running. In the make file, you need to also add:
-target x86_64-apple-ios8.0
as one of the swiftc options under the libyml.dylib section.
I have a project which I need to compile with different preprocessor macros. To reduce the compile time, I would like to compile only the files which re changed due to the macros and then somehow put them in the app binary. Is it possible to do using Xcode or command line tools for iOS?
If the macros are defined in a header file, it might only recompile files that #import or #include that header. Make sure to import the header only in source files where the macros are used to limit the recompiles.
If the macros are defined in the project file, imported or defined in a prefix file or added from the command-line, then it will likely recompile the whole project as it can't tell who depends on them. (Unless some newer version of Xcode can analyze and adapt for that.)