0

I have an Objective C class with a C type method in it. I have to link a C++ library and a C library with my code. If I make the library calls from any Objective C function, I get linker errors, that it can't find the symbols.

refer using c++ static library in objective c

But I need to call that C function somehow. Is there any alternative? In case of only C libraries, I could make the library calls from objective C method, but C++ library doesn't allow me to do that. I don't know why.

Community
  • 1
  • 1
neeraj
  • 1,191
  • 4
  • 19
  • 47
  • So is the function you're trying to call C or C++? –  Oct 10 '12 at 14:41
  • Judging from the other question, you are seeking to call the exported `extern "C"` interface of C++ functions from an Objective C program. That is going to give the system headaches. – Jonathan Leffler Oct 10 '12 at 14:42
  • 3
    @JonathanLeffler Why that? If a function is declared as `extern "C"`, then no name mangling problems arise... And since Objective-C is a strict superset of C, calling a C function from Objective-C should not be a problem... –  Oct 10 '12 at 14:44
  • I am trying to call a function in c++ library, which in turn internally tries to call another function in the C library. For calling the function of c++ library I have to write all such function calls in a C-style function in Objective-C. I tried preceding the declaration of the c++ method (in header files) with extern "C", compiler shows error. – neeraj Oct 10 '12 at 19:22

2 Answers2

2

In my experience, if the program uses any C++ classes, you should be doing the linking with a C++ compiler so as to get the correct C++ libraries linked. With just C and C++, that's fairly straight-forward.

Throw Objective C into the mix and you could get into some difficulties — your question implies you are getting into some difficulties. I have not tried this, but...

You will need to be sure that the correct C++ libraries are linked, the correct Objective C libraries are linked, as well as any stray C libraries that you need. If you can determine the libraries that are linked when you build an Objective C program, you may be safe enough simply to link with the C++ compiler and list the Objective C and pure C libraries that you need. Fingers crossed, that's all. If there are complicated setup requirements for Objective C (as well as the complex requirements for C++), then you may still have difficulties. On the whole though, you are likely to be OK.

If someone with definitive experience says something different, go with their solution (if it works for you). But the best I can offer is 'link the program with the C++ compiler'.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I've heard of something called "Objective-C++" whose main use is to allow calling C++ code from Objective-C. Would that be useful here? – Mike DeSimone Oct 10 '12 at 14:57
  • 1
    @MikeDeSimone No. You're missing the build phase. Objective-C++ is a concept for *compiling* code. OP has difficulties *linking* it. –  Oct 10 '12 at 15:07
  • See, there were some 'namespace's in the c++ header files, so I had to convert my objective-C file types to from objective-c source files to objective-c++ source files. When I do that, some source says that linking is done through a c++ compiler. There is also an option in build settings using which I can link targets according to file types. If I do not change the file types, I would have to add a linker flag -lstd++ to compile against c++ – neeraj Oct 10 '12 at 19:16
1

I'm not quite sure what code is causing you problems but if you want to call a C-style function which is defined in a C++ file from a different C file, make sure to mark the function as extern "C" to avoid name mangling.

simonc
  • 41,632
  • 12
  • 85
  • 103
  • I don't have the source code of the c++ file. I only have the api declarations in header files. i tried marking the declaration of the functions with extern "C", but the compiler was cribbing about that. What I am doing is, including a c++ library, declaring its instances and calling its methods in a function in objective C. If this method of objective C is not of C-style, I get the linker errors. – neeraj Oct 10 '12 at 19:19