# C++ extensions issues

**URL:** https://forum.makecode.com/t/c-extensions-issues/6836
**Category:** PXT
**Created:** [March 17, 2021, 9:00pm UTC](https://forum.makecode.com/t/c-extensions-issues/6836 "2021-03-17T21:00:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![JeCa](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@JeCa](https://forum.makecode.com/u/JeCa)
#### Post date: [March 17, 2021, 9:00pm UTC](https://forum.makecode.com/t/c-extensions-issues/6836/1 "2021-03-17T21:00:44Z")

</div>

I am working on a C++ extension for a new hardware+editor, but need support. I get it to compile, but it fails with “Cannot read property \<\> of undefined” in the simulator/browser. I must be doing something wrong with namespaces?!

I used pxt-adafruit as a starting point, but I don’t think it is specific to that. It is not going to be a downloadable extension since it is tied to the hardware. So here are my steps:

setup files like this (content at the bottom, very short files)  
pxt-adafruit/libs/x1/pxt.json  
pxt-adafruit/libs/x1/x1.cpp  
pxt-adafruit/libs/x1/x1.ts  
pxt-adafruit/libs/x1/shims.d.ts (autogenerated)

add x1 to pxtarget.json, and libs/circuit-playground/pxt.json

It compiles, the shims.d.ts is successfully auto-generated, but I this simple test fails with "Cannot read property ‘readSomething’ of undefined.  
`
forever(function () {
console.logValue(“x”, x1namespace.readSomething())
})
`

What is wrong ? Thanks for your support !

pxt-adafruit/libs/x1/x1.cpp:  
`
#include “pxt.h”
namespace x1namespace {
/**
* Reads something
*/
//% blockId=x1readSomething block=“readSomething”
int readSomething() {
return 56;
}
}
`

pxt-adafruit/libs/x1/x1.ts:  
`
/**`

`
`
`
`- `access to x1
*/
//% color=190 weight=100 block=“x1”
namespace x1namespace {
function readSomething(): int32 {
return 57;
}
}
`

pxt-adafruit/libs/x1/pxt.json:  
`
{
“name”: “x1”,
“description”: “experimental x1”,
“files”: [
“x1.cpp”,
“x1.ts”,
“shims.d.ts”
],
“public”: true,
“dependencies”: {
“core”: “file:…/core”
}
}
`

---

<div class="post-metadata">

### Author: ![JeCa](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@JeCa](https://forum.makecode.com/u/JeCa)
#### Post date: [March 21, 2021, 5:56pm UTC](https://forum.makecode.com/t/c-extensions-issues/6836/2 "2021-03-21T17:56:52Z")

</div>

We finally figured this out. In case someone else stumbles. So we wanted to add a cpp lib to the project (using pxt-adafruit as a starting point). I think the main problem was that the simulator wouldn’t find a typescript counterpart. This is “solution 1”:

- create the cpp file with all block and function annotations using //%
- create the pxt.json file including the “shim.d.ts” and “enums.d.ts” files which will be autogenerated
- add the lib to pxtarget.json and also as a dependency to the (in this case) libs/circuit-playground/pxt.json (otherwise you lib/extension doesn’t show in the editor without first adding the extension)

and (this is what we were missing)

- add a /sim/\< libname \>.ts file in your extension folder with a namespace pxsim.\< your namespace \>, which exports the typescript counter part function to your cpp functions
- add a “///\<reference path=” to the simulator, in this case /sim/dalboard.ts

---

<div class="post-metadata">

### Author: ![JeCa](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@JeCa](https://forum.makecode.com/u/JeCa)
#### Post date: [March 21, 2021, 5:56pm UTC](https://forum.makecode.com/t/c-extensions-issues/6836/3 "2021-03-21T17:56:53Z")

</div>

“solution 2” was inspired by peli in an earlier version of pxt-midi. peli only

- created a cpp file and
- create the corresponding ts file with all the //% annotations in the extension, and
- manually added a //% shim= reference in the ts file to the cpp function, and
- 'export’ed the ts function for the simulator to find

This does not (need to) autogenerate any .d.ts files. Suprisingly (to us) the simulator finds the exported function without the addition of a /sim folder and without a namespace pxsim.\< your namespace \> .

@peli could you possibly shed some light on this for us?

---

<div class="post-metadata">

### Author: ![peli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/peli/32/12_2.png) [@peli](https://forum.makecode.com/u/peli)
#### Post date: [March 24, 2021, 10:04am UTC](https://forum.makecode.com/t/c-extensions-issues/6836/4 "2021-03-24T10:04:35Z")

</div>

@richard @jwunderl @shakao

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [February 7, 2022, 5:35pm UTC](https://forum.makecode.com/t/c-extensions-issues/6836/5 "2022-02-07T17:35:50Z")

</div>

I know this is an old thread, but I’m having the same sort of problem with a micro:bit extension and would appreciate any suggestions. I _think_ I’ve followed the solution described above, but maybe I’m missing something:

- The extension: [https://github.com/bsiever/microbit-pxt-blehid](https://github.com/bsiever/microbit-pxt-blehid)
  - [Example showing the error](https://makecode.microbit.org/_Fmf2HfP4siHR).

- The `startKeyboardService` block will show this sort of simulator error when used.
  - Here’s the [`startKeyboardService`](https://github.com/bsiever/microbit-pxt-blehid/blob/11164344efb06398aa3394b4af97637320ff0c93/keyboard.ts#L35) in TypeScript.
  - Here’s the corresponding [`startKeyboardService`](https://github.com/bsiever/microbit-pxt-blehid/blob/11164344efb06398aa3394b4af97637320ff0c93/keyboard.cpp#L35) in C++.

Thanks for any help!  
Bill

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [February 21, 2022, 11:54pm UTC](https://forum.makecode.com/t/c-extensions-issues/6836/6 "2022-02-21T23:54:13Z")

</div>

Answering my question for anyone else who encounters this problem (I think it’s also answered elsewhere).

My extension is entirely C++ (no simulation component). Many TypeScript functions were just place holders with empty bodies and the C++ complement was the important part.

The error was caused by the methods with the empty bodies. I added a simple `return` to all the empty bodies, which seems to have resolved the problem.
