Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using c++11 with GYP project

Tags:

c++

c++11

gyp

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'
            ],
          },
        ],
      }
like image 427
liamo Avatar asked Mar 02 '13 05:03

liamo


2 Answers

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'
      ],
    },
  ],
}
like image 66
liamo Avatar answered Nov 09 '22 16:11

liamo


So I just tried this on the clang++ 6 OSX 10.10 and I ran into the same problem that drewish hit.

Adding -mmacosx-version-min=10.7 to the OTHER_CPLUSPLUSFLAGS and OTHER_LDFLAGS arrays fixed the issue.

EDIT

An even better way that I found to fix this is to add "MACOSX_DEPLOYMENT_TARGET": "10.7" into the xcode_settings array. This will override any defaults that Node sets in its common.gypi file.

So it should look something like this

{
  'targets': [
    {
      'target_name': 'myApp',
      'sources': [ 'myApp.cc' ]
      'conditions': [
        ['OS=="mac"', {
          'xcode_settings': {
            'MACOSX_DEPLOYMENT_TARGET': '10.7'
          }
        }]
      ]
    }
  ]
}
like image 36
johnhaley81 Avatar answered Nov 09 '22 15:11

johnhaley81