Objective-C and C interoperability - c

We're going to have to write a C library and this library has to be accessible from Objective-C. Now I don't know anything about Objective-C so I wonder how simple Objective-C <-> C interoperability is. Is that as simple as including the C header in the Objective-C code and linking to the library? Is there anything special I'm supposed to do (like the extern "C" bit with C++)?

Objective-C is a strict superset of C. You don't need to use extern "C" or anything like that.

Objective-C is a strict superset of GNU C (note, this is not the same as ISO C90).

Related

Procedure to include own functions to ANSI C Standard library

How one can include his/her own programming functions to standard C (ANSI C) library? And any one who is learning or working on C language able to use those functions anywhere anytime, no need of development in general.
Example : someone developed function named "FunFun()" and assume it does fantastic work for most programmers. so how anyone can access this "FunFun" function without developing and just including standard library?
The sane way to approach it would be to develop a 3rd party library and make it available over the internet through open source, Github etc.
The GNU C dialect is one such example, which is a collection of non-standard compiler extensions used by the GCC compiler. One could join the GCC open source group and try to get the new function added there. It would still not be standard library C, but the GCC extensions are often an inspiration to the C standard and several of them (designated initializers, flexible array members, anonymous struct/union etc) have been adopted into the language itself with the C99 and C11 standards. One of the purposes for GNU C is actually to serve as an experimental playground where new languages features can be tried out live.
If you truly wish to add a new function to the actual C standard library, you would have to join the ISO working group and convince them that the function should be added to the language. Or alternatively find a member of the committee and convince them to speak in favour of the new function.
All this of course assuming you are a C programming veteran, or otherwise nobody will likely take you seriously.
Your question can't be answered because it's based on several wrong assumptions.
Things like stdlib.h are not libraries. They are header files intended to be included in your program. Including means the contents are literally pasted into your program at the point of inclusion before the actual compilation happens. They are typically used for declaring functions, types, global variables etc a library provides. The actual library is then linked against after compilation.
There's no such thing as the C library as well as there's no such thing as the C compiler. c is a language that is specified in an open standard (if you're interested, here's the last draft of the latest standard version C11). There are many actual implementations and a complete implementation consists of at least a compiler and a standard library. You can of course implement your own standard library. It's a lot of work to have it really conform to the standard (you would have to implement printf() and scanf() correctly, for example). With your own standard library, you can also include your own extensions, but this would only mean people using your standard library (instead of e.g. glibc on a GNU system) could directly use them.
For having a function available on any implementation of C, it would be necessary to have the C Standard specify it. You won't get a new function in the standard without some very good reasoning.
So if you want to make your own function available to others, do what everyone does and just implement it in your own library. Users can download it, include its headers and link against it.

Using a C++ class in C code

I was under the impression that it should be possible to mix c and c++ code, but looks like I was wrong.
I'm having a bunch of existing C code and have written a class in C++ that I would like to use in the existing C code.
Is that even possible?
C++ is not a superset of C and thus not all C code is valid C++ code. This is even more true for C++ code in a C compiler. All language additions C++ has are not valid C (classes, generic programming, namespaces). What you could do is compile the result code with a C++ compiler and fix cases where code that was valid for a C compiler isn't for a C++ compiler.
You can't use classes from C code, because classes don't exist in C.
However, you can define a bunch of global functions that access your class, and then you can access those functions from C.
You can mix the two and then compile the result as C++.
If you have a C++ class that you want to use in C then you could remove all the member functions and rewrite then with an extra parameter being a ptr to a structure.
More advance C++ features wouldn't be so easy to incorporate in C.

Looking for a way to access c++ objects in C as if I was in c++

I have a few ideas:
char* test = "testInteger(5).workOnReturn("doIt")[10]"
int ret = execute(test);
What if I use the 'extern' keyword?
Suppose I have a whole bunch of C++ implementations and classes.
Couldn't I just define the same things in C with 'extern' and provide a dummy implementation and on runtime, it would access the C++ library with the actual implementation?
Perhaps you could customize your GCC compiler (e.g. with a MELT extension) to produce somehow a C interface to your library (perhaps by annotating relevant C++ functions with your own #pragma-s ....)
But your question is too vague to get a more precise answer.
What is your C++ library, how should it be used in C++?
Look at how other C++ libraries interface to C; e.g. look inside the source code of PPL
If you want to use C++ from C, it can be done only in an extremely limited way, and as far as the C++ code is explicitly built for it (that means basically throwing away most for what makes C++ C++). Won't happen in your case.
Your ways out are to just use C++ or get a library for C. Anything else will hurt too much to make any sense.

Can c++ libraries run in c?

I'm sorry if this is a basic question(I'm new to c/c++, but I'm a little confused at how to get the answer. stxxl is a c++ library but some of my code is in c. I know c++ can use c code(my c code is embedded in c++), but does it work the other way around so c can run c++ code?
Their site only mentions c++ but I'm wondering if there's something special that can be done to run c++ libraries within c?
Sorry the books I have read talk about using c code in c++ and the c book I read was written before c++ came out. Right now my c function is sending data to my c++ code which is using the c++ library and then sending results back so I'm thinking I want to test performance if I cut the middle man(c++).
You can link to a C++ library from C only when the C++ library has been designed to be used from C. Specifically, the functions the library provides need to be exported with extern "C" {} block to avoid name mangling, and the interface should be designed in a way to be usable from plain C (i.e. no classes or member functions, only functionless structs and plain functions).
It is worth mentioning that you can compile your C code with a C++ compiler, and it will for the most part be OK. This lets you pretend that your C code is a C++ code, and freely mix in functionality provided through C++ - specific interfaces.
Here's a links that may help you:
How to mix C and C++

Is the term "libc" equivalent to "C standard library"?

I sometimes hear people using the terms "libc" and "C standard library" interchangeably. I understand that "libc" is the name (or part of the names) of many popular C standard library implementations. My guess is that because of the widespread use of these implementations, people started referring to the C standard library in general as "libc" although it is not an official name.
Is it correct to refer to the C standard library as "libc"?
I always thought that "libc" just happens to be the name (or part of the names) of many popular C standard library implementations.
This is correct. "libc" is the name of some implementations of the C Standard Library.
As an example of a C Standard Library implementation that is not named "libc," the Microsoft C Standard Library implementation is a part of the "C Run-Time Libraries," usually referred to as the "CRT."
Is it correct to refer to the C standard library as "libc"?
The C Standard Library is not named "libc," so using that term to refer to it generically (and not to a particular implementation) would be incorrect. That said, in most contexts, if you did use the term "libc" to refer to the C Standard Library, you are still likely to be understood.
"libc" is indeed the name of the implementation. It often includes functions that are not part of the C standard, and might not include functions that are part of the C standard. (A common case of the latter is the Standard C math functions being split into a separate "libm".)
'libc' refers to the C standard library. However, the libc has several implementations :
glibc : an implementation of libc written for the GNU Project
klibc : a minimalistic subset implementation of the libc
...
LibC (http://www.gnu.org/s/libc/) is one particular implementation of the C library standard (http://en.wikipedia.org/wiki/C_standard_library#ISO_Standard).

Resources