您是否可以将针对c++ linux的应用程序链接到Objective-C静态库?

时间:2022-09-07 08:51:03

I've seen plenty of information on how to link an Objective-C application to C++ libraries, but can you do the other way around?

我已经看到了很多关于如何将Objective-C应用程序链接到c++库的信息,但是你可以反过来吗?

My partners and I have been working on an iPhone App using entirely Objective-C. Half way through the project, we decide to add a new feature to it. This feature will require Push Notifications, so we need to write the server-side application that will be running on a Linux box. This server app needs access to some (most, actually) of the code written in Objective-C. I figure we have two choices: re-write the whole thing in C/C++ or re-compile the Objective-C code on Linux. As rewriting would take too long, I'd like to take the second route.

我和我的搭档一直在开发一个完全使用Objective-C的iPhone应用。在项目进行到一半的时候,我们决定为它添加一个新特性。这个特性将需要推送通知,因此我们需要编写将在Linux机器上运行的服务器端应用程序。这个服务器应用程序需要访问Objective-C中编写的一些(实际上是大部分)代码。我认为我们有两个选择:用C/ c++重写整个代码,或者在Linux上重新编译Objective-C代码。由于重写需要很长时间,我想走第二条路。

I managed to use GCC and GNUstep to compile the Objective-C classes we'll need for the linux server app into a static library. I'd like to write the actual server app in C++, but I need to link that app to the Objective-C library. Can it be done? How?

我设法使用GCC和GNUstep将linux服务器应用程序需要的Objective-C类编译为静态库。我想用c++编写实际的服务器应用程序,但是我需要将该应用程序链接到Objective-C库。可以做到吗?如何?

If not, can anyone give me an idea of how to go about this?

如果没有,谁能告诉我该怎么做?

1 个解决方案

#1


2  

Gcc on your Linux box should be capable to compile Objective-C together with standard foundation classes like NSString or NSArray. You may need to apt-get the front-end to gcc. Or even better use LLVM.

Linux上的Gcc应该能够编译Objective-C和标准的基础类,如NSString或NSArray。您可能需要使用ap -获取gcc的前端。或者使用LLVM更好。

As long as you don't use Apple libraries in your shared code you should be fine to compile it on Linux.

只要在共享代码中不使用Apple库,就可以在Linux上编译它。

You will have to wrap your Objective-C functionality into C functions if you want to call them from your C or C++ application. I have provided some guidance on it in my answer to this SO question: is there any way to use Objective-C library in C?.

如果您想从您的C或c++应用程序调用Objective-C函数,那么您必须将您的Objective-C功能封装到C函数中。在我对这个问题的回答中我提供了一些指导:是否有办法在C中使用Objective-C库?

EDIT: (to answer comment's questions)

编辑:(回答评论的问题)

The wrapper function have to be .m files and be compiled with Objective-C compiler as it will still contain Objective-C code and just expose plain .C functions. It's just the same as C API wrapper for C++ code still needs to be a C++ code - just having extern "C" functions defined. In Objective-C, you need no extern "C" as Objective-C is still C - the same linkage, etc.

包装器函数必须是.m文件,并且使用Objective-C编译器编译,因为它仍然包含Objective-C代码,并且只公开普通的. c函数。它与c++代码的C API包装器一样,仍然需要一个c++代码——只需定义外部“C”函数。在Objective-C中,你不需要外部的"C"因为Objective-C仍然是C -相同的连杆,等等。

You can use the resultant library with simple -l flag in GCC-compliant compilers.

您可以在兼容gcc的编译器中使用带有简单-l标志的合成库。

By Apple libraries, I mean something more than NSFoundation.h - the specific iPhone libraries - like CocoaTouch.

在苹果的图书馆里,我指的不仅仅是NSFoundation。h -特定的iPhone库——像CocoaTouch。

NSFoundation.h is definitely provided by your Objective-C package / GNUStep.

NSFoundation。h是由你的Objective-C包/ GNUStep提供的。

#1


2  

Gcc on your Linux box should be capable to compile Objective-C together with standard foundation classes like NSString or NSArray. You may need to apt-get the front-end to gcc. Or even better use LLVM.

Linux上的Gcc应该能够编译Objective-C和标准的基础类,如NSString或NSArray。您可能需要使用ap -获取gcc的前端。或者使用LLVM更好。

As long as you don't use Apple libraries in your shared code you should be fine to compile it on Linux.

只要在共享代码中不使用Apple库,就可以在Linux上编译它。

You will have to wrap your Objective-C functionality into C functions if you want to call them from your C or C++ application. I have provided some guidance on it in my answer to this SO question: is there any way to use Objective-C library in C?.

如果您想从您的C或c++应用程序调用Objective-C函数,那么您必须将您的Objective-C功能封装到C函数中。在我对这个问题的回答中我提供了一些指导:是否有办法在C中使用Objective-C库?

EDIT: (to answer comment's questions)

编辑:(回答评论的问题)

The wrapper function have to be .m files and be compiled with Objective-C compiler as it will still contain Objective-C code and just expose plain .C functions. It's just the same as C API wrapper for C++ code still needs to be a C++ code - just having extern "C" functions defined. In Objective-C, you need no extern "C" as Objective-C is still C - the same linkage, etc.

包装器函数必须是.m文件,并且使用Objective-C编译器编译,因为它仍然包含Objective-C代码,并且只公开普通的. c函数。它与c++代码的C API包装器一样,仍然需要一个c++代码——只需定义外部“C”函数。在Objective-C中,你不需要外部的"C"因为Objective-C仍然是C -相同的连杆,等等。

You can use the resultant library with simple -l flag in GCC-compliant compilers.

您可以在兼容gcc的编译器中使用带有简单-l标志的合成库。

By Apple libraries, I mean something more than NSFoundation.h - the specific iPhone libraries - like CocoaTouch.

在苹果的图书馆里,我指的不仅仅是NSFoundation。h -特定的iPhone库——像CocoaTouch。

NSFoundation.h is definitely provided by your Objective-C package / GNUStep.

NSFoundation。h是由你的Objective-C包/ GNUStep提供的。