Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiling a #include <cstdlib> in Xcode - error during compilation: file not found

I have added one framework in my project, where to get the callbacks of framework I need to implement c++ code,for which I have added c++ classes which are .mm from .cpp

as per suggestions given in this link

I also came across few posts of StackOverflow, where I found either I have to go for C++ or Objective C source type(as suggestion of few members).

I am looking to compile c++ code along with Objective C classes,but I am getting file not found error (compile time) for #include <cstdlib> and #include <string>

Please let me know any one worked around it.or faced same Issue.

Thanks In Advance!!!

like image 398
Sandeep Khade Avatar asked Mar 25 '26 08:03

Sandeep Khade


1 Answers

I think problem is that your's .h-file, where you have added C++ includes is included in other .m-files, not just in .mm. There is several ways to solve this problem:

  1. Use C++ includes only in .mm files. (don't use them in .h)
  2. Change all files, that import yours header to .mm
  3. Include them in

    #ifdef __cplusplus
    #endif
    

block. Also use this block for methods where C++ classes mentioned. For example:

#ifdef __cplusplus
#include <string>
using namespace std;
#endif

@interface SomeClass

#ifdef __cplusplus
- (void) setName:(const string&) name;
#endif

@end

Also there is way to avoid #ifdef #endif block by using extensions and categories:

  1. declare C++'s instance variables only in extensions.
  2. create categories with additional .h-file for methods and properties, that use C++'s types.
like image 135
Cy-4AH Avatar answered Mar 27 '26 23:03

Cy-4AH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!