using c++11 with GYP project
Asked Answered
T

4

12

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'
            ],
          },
        ],
      }
Temperance answered 2/3, 2013 at 5:55 Comment(0)
T
8

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'
      ],
    },
  ],
}
Temperance answered 5/3, 2013 at 6:28 Comment(2)
Did you run into clang: error: invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later) at all?Paillette
I ran into that just now. Haven't been able to figure it out yet. On OSX 10.10.Hisakohisbe
H
1

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'
          }
        }]
      ]
    }
  ]
}
Hisakohisbe answered 19/10, 2014 at 4:29 Comment(0)
N
0

johnhaley81's solution is really the simplest one to get this working (at least on OSX), however I had to add a few more settings:

  "conditions": [
    [ 'OS=="mac"', {
      "xcode_settings": {
        'MACOSX_DEPLOYMENT_TARGET': '10.9',
        "CLANG_CXX_LIBRARY": "libc++",
        "GCC_ENABLE_CPP_RTTI": "YES",
        "GCC_ENABLE_CPP_EXCEPTIONS": "YES"
      },
    }],
  ],

As you can see, you can directly use the name/value pairs from an XCode project here. Pretty handy.

Nephron answered 4/9, 2016 at 10:2 Comment(0)
S
0

The previous answers were helpful, but all that must actually be added to xcode_settings are 'MACOSX_DEPLOYMENT_TARGET': '10.7' and 'CLANG_CXX_LIBRARY': 'libc++'

{
  'targets': [
    {
      'target_name': 'myApp',
      'sources': [ 'myApp.cc' ]
      'conditions': [
        ['OS=="mac"', {
          'xcode_settings': {
            'MACOSX_DEPLOYMENT_TARGET': '10.7',
            'CLANG_CXX_LIBRARY': 'libc++'
          }
        }]
      ]
    }
  ]
}

Specifically, the option 'CLANG_CXX_LIBRARY': 'libc++' is what's getting us what we want. If you add this option without 'MACOSX_DEPLOYMENT_TARGET': '10.7' then you might see the following error:

clang: error: invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later)

This error indicates that 10.7 is a more appropriate value than 10.9 for MACOSX_DEPLOYMENT_TARGET if you're interested in compatibility.

Saurian answered 30/9, 2017 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.