using c++11 with GYP project - c++

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'
],
},
],
}

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'
],
},
],
}

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'
}
}]
]
}
]
}

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.

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.

Related

Unit testing in browser for NativeScript fails because of .tns. files

I have an Angular flavoured Nativescript project, which must be tested with "vanila" Jasmine, in a browser (so not in mobile) with ng test.
By default, with "naked" tests, it works. But the problem is, if I try to test/import anything, that has a ".tns" alternative, in some cases it loads that, and the build fails.
My problem is similar to this thread but there were no good solution described there.
So for instance:
I have two files:
app.component.tns.ts
app.component.ts
and I try to import it for testing in app.component.spec.ts:
import {AppComponent} from "#src/app/app.component";
it loads the .tns. file, and the build fails, as it cannot load the mobile-specific libraries.
ERROR in ./src/app/app.component.tns.ts
Module not found: Error: Can't resolve 'nativescript-ui-sidedrawer' in '/home/..../src/app'
resolve 'nativescript-ui-sidedrawer' in '/home/...../src/app'
Parsed request is a module
using description file: /home/...../src/package.json (relative path: ./app)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
...
# ./src/app/app.component.tns.ts 25:35-72
# ./src/app/app.module.spec.ts
# ./src sync \.spec\.ts$
# ./src/test.ts
is there any solution to "remove" the .tns. files, just as if I were running a simple ng serve?
update: My tsconfig.spec.json should exclude these files, but it does not work either ...
"exclude": [
"**/*.tns.ts",
"**/*.android.ts",
"**/*.ios.ts"
]
}
it seems the problem was with tsconfig.json. Specificly this part:
"compilerOptions": {
...
"paths": {
"#src/*": [
"src/*.android.ts",
"src/*.ios.ts",
"src/*.tns.ts",
"src/*.web.ts",
"src/*.ts"
]
},
As this was extended by the tsconfig.spec.json.
I modified the tsconfig.spec.json to this:
{
"compilerOptions": {
"target": "es5",
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2017",
"dom",
"es6",
"es2015.iterable"
],
"baseUrl": ".",
"resolveJsonModule": true,
"esModuleInterop": true,
"paths": {
"#src/*": [
"src/*.ts"
]
},
"outDir": "../out-tsc/spec",
"types": [
"jasmine",
"node"
]
},
"files": [
"src/test.ts",
"src/polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
],
"exclude": [
"**/*.tns.ts",
"**/*.android.ts",
"**/*.ios.ts"
]
}
and now the tests run, and the correct components are imported.

polymer 3: code not work after polymer build

my polymer.json
{
"entrypoint": "index.html",
"shell": "src/boo-blogger/boo-blogger.js",
"sources": [
"images/*",
"fonts/*"
],
"extraDependencies": [
"manifest.json",
"node_modules/#webcomponents/webcomponentsjs/*.js",
"node_modules/web-animations-js/web-animations-next-lite.min.js",
"node_modules/#webcomponents/webcomponentsjs/bundles/*.js"
],
"builds": [
{
"name": "es6",
"bundle": true,
"browserCapabilities": [
"es2015",
"modules"
],
"js": {
"minify": true,
"transformModulesToAmd": true
},
"html": {
"minify": true
},
"css": {
"minify": true
},
"addServiceWorker": true
}
],
"npm": true,
"lint": {
"rules": [
"polymer-3"
]
}
}
I got the problem
article-list.js:5 Uncaught (in promise) TypeError: _booBlogger.html$1 is not a function
article-list.js imported dynamic by routing. So I tried to attempt some build option differently to build. I found when I build without bundle, built code can work. I don't know why my code can not bundle by polymer build. if anyone is curious the problem and need completely source code. contact me
tools summary
system: ubuntu 16.04
polymer: 1.7.7
node: 10.1.0
In your polymer.json you need to add:
"fragments": [
"src/article-list.js"
],
I think you forgot to add article-list.js to your sources. As it is written in docs you have to include your dynamically imported modules to "sources" in polymer.json. See https://polymer-library.polymer-project.org/3.0/docs/apps/build-for-production#dynamic

Nesting Angular component files into one in Visual Studio

Which setting in Visual Studio 2017 allows for bundling Angular component files into one file?
In the image below the files in the red square should be visually presented as one that then can be expanded.
Names of the files are:
navmenu.component.css
navmenu.component.html
navmenu.component.ts
They only differ by extension. The html file should be the main one with ts and css being presented as sub files.
It was working fine until I updated Visual Studio 2017 to version 15.3.3 (before it was 15.1).
I finally found a solution to that issue on that thread. It requires editing the toolSettings.json file.
It requires editing toolSettings.json, located in C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Web Tools\ProjectSystem
The following entries need to be added
".ts": [
".html"
],
under extensionToExtension.
And ".html" in the "css" array.
The whole file looks like that:
{
"dependentFileProviders": {
"addedExtension": {},
"pathSegment": {
"*": [
".js",
".css",
".html",
".htm",
".less",
".scss",
".coffee",
".iced",
".config",
".cs",
".vb",
".json"
]
},
"extensionToExtension": {
".js": [
".coffee",
".iced",
".ts",
".tsx",
".jsx"
],
".ts": [
".html"
],
".css": [
".less",
".scss",
".sass",
".styl",
".html"
],
".html": [
".md",
".mdown",
".markdown",
".mdwn",
],
".map": [
".js",
".css"
],
".svgz": [
".svg"
],
".designer.cs": [
".resx"
],
".cs.d.ts": [
".cs"
]
},
"fileToFile": {
".bowerrc": [
"bower.json"
],
".npmrc": [
"package.json"
],
"npm-shrinkwrap.json": [
"package.json"
],
"yarn.lock": [
"package.json"
],
".yarnclean": [
"package.json"
],
".yarnignore": [
"package.json"
],
".yarn-integrity": [
"package.json"
],
".yarnrc": [
"package.json"
]
},
"filePartToExtension": {
"-vsdoc.js": [
".js"
]
},
"allExtensions": {
"*": [
".tt"
]
}
}
}
The accepted answer got me on the right track, but I have a couple of improvements to contribute:
First, rather than editing the Visual Studio default rules, I would recommend creating your own nesting rules. You can either create one just for your personal use, or create a .filenesting.json file in your Project or Solution folder so your teammates get the same experience by default.
Second, since in Angular it's the typescript that defines the component and sometimes references HTML, CSS, etc., for me it makes more sense to reverse the relationship.
Here's the .filenesting.json file I ended up using for our web app project, in case it's useful as a minimalist starting point for other Angular and Visual Studio users.
{
"help": "https://go.microsoft.com/fwlink/?linkid=866610",
"root": true,
"dependentFileProviders": {
"add": {
"addedExtension": {},
"pathSegment": {
"add": {
".spec.ts": [
".ts"
]
}
},
"extensionToExtension": {
"add": {
".html": [
".ts"
],
".scss": [
".ts"
]
}
}
}
}
}

What is wrong with this GYP file?

I am using a simple GYP file to build a simple D program. The GYP file follows:
{
'targets': [
{
'target_name': 'bin/launchprogram',
'type': 'executable',
'sources': [
'src/launchprogram.d',
],
'actions': [
{
'action_name': 'gdc',
'inputs': [
'src/launchprogram.d'
],
'outputs': [
'bin/launchprogram'
],
'action': [
'gdc',
'-o', 'bin/launchprogram',
'-O3', '-march=native',
'src/launchprogram.d'
],
},
],
},
],
}
The compilation doesn't work -- it tries to run g++ on the produced executable and that of course fails. What did I do wrong?
This is my first attempt at using GYP, so please be patient.
It works when I change the executable type to 'none', to keep GYP from trying to link.

Invalid deployment target for -stdlib=libc++ on OSX 10.8

I am compiling through node-gyp a Node.JS package written in C++. When I compile it I receive the following error: clang: error: invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later). I'm running on OSX 10.8, and I have installed the XCode Command Line Tools. This is the file used by node-gyp to compile the package:
{
"targets": [
{
"target_name": "package_name",
'type': 'executable',
'xcode_settings': {
'OTHER_CFLAGS': [
"-std=c++11",
"-stdlib=libc++"
],
},
"sources": [ "package_src.cpp" ],
}
]
}
Basically it specifies the target of the compilation, the type and the flags, as well as the sources.
Any idea on how I can solve this problem?
Whilst you got OS X 10.8 clang will try to build it so that it can be run on other older OS X versions too. Now since -stdlib=libc++ requires a minimum version of 10.7 it won't compile it unless you explicitly tell it that you'll use 10.7 (or higher) as the deployment target by specifying
'MACOSX_DEPLOYMENT_TARGET': '10.7'
inside the xcode_settings.
In your case the complete settings should look like:
{
"targets": [
{
"target_name": "package_name",
'type': 'executable',
'xcode_settings': {
'MACOSX_DEPLOYMENT_TARGET': '10.7',
'OTHER_CFLAGS': [
"-std=c++11",
"-stdlib=libc++"
],
},
"sources": [ "package_src.cpp" ],
}
]
}
I don't know what worked for you, #drewish, but this is how I got it to work:
{
"targets": [
{
"target_name": "package_name",
'type': 'executable',
'xcode_settings': {
'OTHER_CFLAGS': [
"-std=c++11",
"-stdlib=libc++",
"-mmacosx-version-min=10.7"
],
},
"sources": [ "overcpu.cpp" ],
}
]
}