15 Replies
      Latest reply on Mar 20, 2020 7:41 AM by waynehend
      waynehend Level 1 Level 1 (5 points)

        I'm familiar only with Swift.  I have a running Swift app that I want to add other code to, from projects I've found online.  One project was in C and with just a few hoops to jump, I got that part working.  Yay me.

         

        Now I'm trying to add some C++ code and I'm hitting roadblocks.

         

        First question:  Is my current Objective C Bridging Header the only one I need, or do I need another bridging header?

         

        Second question.

        The problem I'm having is with this code in my bridging header:

        #include "IF97.h"  // The project I want to use
        #include <cmath>
        #include <vector>
        #include <algorithm>
        #include <iostream>
        #include <iomanip>
        #include <stdexcept>
        #include <stdio.h>
        
        

         

        All but the first and final lines produce "file not found" errors.

        • Re: Swift Project including both C and C++
          waynehend Level 1 Level 1 (5 points)

          Progress?

           

          I added this search path to my project's   TARGETs:myApp:Header Search Paths

          /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/

           

          The "file not found errors" are gone but I'm getting a blizzard of other errors such as

          Unknown type name "namepace"

          Unknown type name "template"

          Expected ';' after top level declarator

          ...and many more.

           

          Kinda lost.

          • Re: Swift Project including both C and C++
            waynehend Level 1 Level 1 (5 points)

            For the benefit of anyone following, here is some additional background.  The solution I need may be hiding in these excellent links but I just can't see it.  I feel like I've tried all the permutations of these that I can think of.

             

            Can I have Swift, Objective-C, C and C++ files in the same Xcode project?

            Can I mix Swift with C++? Like the Objective-C .mm files

            https://www.youtube.com/watch?v=0x6JbiphNS4

            https://github.com/SwiftArchitect/SO-32541268

             

            I'm trying to implement the C++ code from the following project into my Xcode project. I've had success adding a different project in C, but this one in C++  is stumping me.

            https://github.com/CoolProp/IF97

             

            One issue was all the #include files in that project. I added the following search path to my Xcode build settings and this seems to fix the "file not found" errors.

            /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/

             

            How do I cobble this together?

              • Re: Swift Project including both C and C++
                john daniel Level 4 Level 4 (690 points)

                None of your examples use a private, Objective-C header. For that reason, they aren't ever going to work in a real-world project.

                 

                See this forum post. Here is an example of what this would look like in the real world. All of this code, including the comments is generated. It doesn't do much, but it does show how to encapsulate a C++ object in Objective-C, from whence it can be used in Swift. You will have to be very careful with object hierarchies. If I remember correctly, I did come up with a way to handle that using the Swift bridging file. I don't have an example of that anywhere handy. That is the source of the underscore prefix in this example code. I would never use an underscore prefix in real life. It was for some kind of hack.

                 

                I'm not using the architecture I described in that post. It was just an experiment. I'm just using all Objective-C++. I strongly suggest that you do likewise. If you absolutely must use Swift, then you should write a custom Objective-C++ interface, using the model described above, that calls into your C++ project at a high level and performs the operations that you need.

                 

                Swift is truly the work of genius. But it is a genius of social engineering, not technical.

                • Re: Swift Project including both C and C++
                  john daniel Level 4 Level 4 (690 points)

                  Well, I guess I went too far. I guess the forums still do have moderation. Here is a slightly edited version of the moderated post:

                   

                  None of your examples use a private, Objective-C header. For that reason, they aren't ever going to work in a real-world project.

                   

                  See this forum post. Here is an example of what this would look like in the real world (See gist.github.com/etresoft/076679ecef38521b5283554dca62e70f). All of this code, including the comments is generated. It doesn't do much, but it does show how to encapsulate a C++ object in Objective-C, from whence it can be used in Swift. You will have to be very careful with object hierarchies. If I remember correctly, I did come up with a way to handle that using the Swift bridging file. I don't have an example of that anywhere handy. That is the source of the underscore prefix in this example code. I would never use an underscore prefix in real life. It was for some kind of hack.

                   

                  I'm not using the architecture I described in that post. It was just an experiment. I'm just using all Objective-C++. I strongly suggest that you do likewise. If you absolutely must use Swift, then you should write a custom Objective-C++ interface, using the model described above, that calls into your C++ project at a high level and performs the operations that you need.

                   

                  Swift is truly the work of genius. But it is a genius of social engineering, not technical.

                    • Re: Swift Project including both C and C++
                      waynehend Level 1 Level 1 (5 points)

                      "None of your examples use a private, Objective-C header. For that reason, they aren't ever going to work in a real-world project."

                       

                      Can you elaborate on that?  I'm specifically working on the Xcode project offered in the fourth, GitHub link.  Does your comment apply to that as well?  I've contacted the author of that project but no reply yet.

                       

                      I’m sad to say that your reply is over my head.  I have never used anything but Swift and so all this hierarchy stuff, headers and includes, is magic to me.  I stumbled through adding some C code but I haven't been so lucky with C++.

                        • Re: Swift Project including both C and C++
                          john daniel Level 4 Level 4 (690 points)

                          I can, and did, elaborate, but then I accidentally clicked your github link and lost it all. It's all a moot point anyway.

                           

                          I reviewed the github project and it is just barely C++. It is just some had-coded equations. My advice would be to just rename "IF97.cpp" to "IF97.Swift" and then fix the code. You'll have 10,000 errors until it stops counting, but just go through it and fix them one by one. It won't take as long as you think. If you are handy with regular expressions and/or BBEdit, you could automate a lot of it.That would be easier than trying to implement what I proposed above.

                            • Re: Swift Project including both C and C++
                              waynehend Level 1 Level 1 (5 points)

                              "My advice would be to just rename "IF97.cpp" to "IF97.Swift" and then fix the code. You'll have 10,000 errors until it stops counting, but just go through it and fix them one by one."

                               

                              I though of that.  Brute force I understand!  My reluctance is from not understanding C++ well enough to accomplish the tranlation but I suppose their are resources I can find to help.

                               

                              How to deal with all those includes?

                              #include <vector>
                              #include <cmath>
                              #include <algorithm>
                              #include <iostream>
                              #include <iomanip>      // std::setprecision
                              #include <stdexcept>
                              #include <stdio.h>
                              
                              • Re: Swift Project including both C and C++
                                waynehend Level 1 Level 1 (5 points)

                                What do you think of the following approach?

                                 

                                Use a Java version of the source code instead  ==>  sourceforge.net     /projects/if97/

                                 

                                Use Google's tool to port to Obj-C  ==>  developers.google.com       /j2objc

                                 

                                Might be easier than manually porting 5,000 lines of C++ code?

                                 

                                [edit]  altered the links to get this post out of moderation