liamo liamo - 2 months ago 9
C++ Question

using c++11 with GYP project

I am trying to create a simple cross platform C++ project with gyp. Currently I'm just trying this on a mac - but would like to get it to build for windows, Linux, ios and android eventually.
HEre is the simple gyp file that I'm using.
I would like to be able to use ninja as well as xcode/msvc projects from this gyp.
I know that I need to be able to add

-std=c++11 and -libstdc++ to the commandline for clang, but right now I only see the generated build files using g++ instead of clang.

This is my gyp file.

{
'targets': [

{
'target_name': 'libtest',
'product_name': 'test',
'type': 'static_library',
'sources': [
'./src/lib.cpp',
],
'include_dirs': [
'include',
],
},

{
'target_name': 'testapp',
'type': 'executable',
'sources': [
'./test/test.cpp',
],
'include_dirs': [
'src',
],
'dependencies': [
'libtest'
],
},
],
}

Answer

I've sort of figured out this to a certain extent. At lesast I got it working on the mac for a makefile build ( not ninja which was my original hope).

First I had to get gyp to use clang instead of g++ , to do this I had to add a make_global_settings to the gyp file as so. This doesn't seem like a good plan for a crossplatform build. I was also able to set these with environment variables, I'm guessing I can probably do something with conditions to make this specific to the mac.

'make_global_settings': [
    ['CXX','/usr/bin/clang++'],
    ['LINK','/usr/bin/clang++'],
  ],
  'targets': 
  [
    ......

The other thing I had to do was add an xcode_settings dictionary with OTHER_CPLUSPLUSFLAGS and OTHER_LDFLAGS depending on the target type. Full example below.

{

 'make_global_settings': [
    ['CXX','/usr/bin/clang++'],
    ['LINK','/usr/bin/clang++'],
  ],
  'targets': [

    {
      'target_name': 'mylib',
      'product_name': 'mylib',
      'type': 'static_library',
      'sources': [
        'src/implementation.cc',
      ],
      'include_dirs': [
         'include',
      ],
       'conditions': [
        [ 'OS=="mac"', {

          'xcode_settings': {
            'OTHER_CPLUSPLUSFLAGS' : ['-stdlib=libc++'],
            },

        }],
        ],
    },

    {
      'target_name': 'myapp',
      'type': 'executable',
      'sources': [
        './bin/myapp.cc',
      ],
      'conditions': [
        [ 'OS=="mac"', {

          'xcode_settings': {
            'OTHER_CPLUSPLUSFLAGS' : ['-std=c++11','-stdlib=libc++'],
            'OTHER_LDFLAGS': ['-stdlib=libc++'],
            },

        }],
      ],
      'include_dirs': [
         'include',
      ],
      'dependencies': [
        'mylib'
      ],
    },
  ],
}
Comments