Compiling .cpp file as Objective-C ++ through binding.gyp when creating Node.js / Node -webkit addons

I have a previous C ++ static library that I am porting so that it can be compiled as an addon for Node.js / Node-webkit. To create this addon, I run Node -gyp / nw-gyp on the binding.gyp file I created. This static library is multiplatform and compiles for Windows, Mac and Linux via VisualStudio, Xcode and CMake, respectively. This static library is already in use in various applications (read, chances are it won't be refactored specifically for this one addon compilation case, which is still in the proof of concept phase).

All C ++ cross-platform files have the .cpp extension, although some are actually compiled on Mac as Objective-C ++ (to use some Cocoa niceties). In Xcode on Mac, I can compile .cpp files such as Objective-C ++ by switching the "type" for the file from "Default - C ++ Source" to "Objective-C ++ Source" in the "File Inspector" ". This is convenient as I can have the same file compiled as C ++ on Windows / Linux and Obj-C ++ on Mac, regardless of the .cpp file extension. To create the addon, I use the ObjectWrap paradigm. To wrap the classes Objective-C ++, I have to include their .h files, which forces Objective-C ++ script at the add-on level.

I am new to using Node -gyp and nw-gyp. Are there any additional qualifiers I can add to my binding.gyp file to explicitly indicate that a given .cpp file should actually be compiled as Objective-C ++, similar to the type setting in Xcode I mentioned above? As an intermediate step in developing a proof of concept, I can successfully get .mm files to compile as Objective-C ++. However, as mentioned above, many of these files are actually multiplatform and should compile as straight C ++ on Windows / Linux as soon as I translate my proof of concept to other platforms, and so I would prefer that they keep the extension. cpp file.

+3


source to share


1 answer


To compile all .cpp files as Objective-C ++ add the following to binding.gyp:

'conditions': [
    ['OS=="mac"', {
        'xcode_settings': {
            'OTHER_CFLAGS': [
                '-ObjC++'
            ]
        }
    }]
]

      

If you only need to compile some .cpp files as Objective-C ++, create a .mm file that will only contain the .cpp files that need to be compiled as Objective-C ++. In bind.gyp, use conditions to compile the .mm shell file or .cpp files depending on the platform.

In the .mm file:



#include "file.cpp"

      

In bind.gyp:

'conditions': [
    ['OS=="mac"', {
        'sources': [
            "file.mm"
        ]
    }],
    ['OS=="win"', {
        'sources': [
            "file.cpp"
        ]
    }]
]

      

0


source







All Articles