Can I import libsyntax in my code? - rust

I want to import the rust lexer in my code
use syntax;
fn main() { () }
but above code fails to compile
error: failed to resolve imports
testparse.rs:1:4: 1:11 error: unresolved import
testparse.rs:1 use syntax;
^~~~~~~
error: aborting due to 2 previous errors
Can I use libsyntax from user code? If I can, how can I import it?

You need to add the library:
extern mod syntax;

Related

How to load a npm package to wasm_bindgen

I tried searching internet, couldn't find details how can I load a npm package to yew.
I am trying to load #polkadot/extension-dapp ( code docs)
#[wasm_bindgen(module = "/node_modules/#polkadot/extension-dapp/index.js")]
extern "C" {
#[wasm_bindgen]
pub fn web3Enable(originName: String) -> JsValue;
}
It gives error:
Uncaught TypeError: Failed to resolve module specifier "#polkadot/util". Relative references must start with either "/", "./", or "../".

PyO3 linker error when using "extension-module" feature

When I try to build very simple a PyO3 module, the compilation stops at the link stage and returns a very long error message. I compile this code on Linux.
The Rust project looks like this:
├ Cargo.toml
└ src/
└ lib.rs
Cargo.toml:
[package]
name = "dytest"
version = "0.1.0"
edition = "2021"
[lib]
name = "dytest"
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.16", features = ["extension-module"] }
lib.rs:
use pyo3::prelude::*;
#[test]
fn test_print() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| py.run( "print('Hello World')", None, None ) );
}
When I now run cargo test, I get a linker error:
error: linking with `cc` failed: exit status: 1
|
= note: "cc" "-m64" [...many, many paths...]
= note: /usr/bin/ld: /home/user/dytest/target/debug/deps/libpyo3-c98e45c2daa36b66.rlib(pyo3-c98e45c2daa36b66.pyo3.b04632a8-cgu.2.rcgu.o): in function `pyo3_ffi::object::Py_DECREF':
/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-ffi-0.16.2/src/object.rs:407: undefined reference to `_Py_Dealloc'
/usr/bin/ld: /home/user/dytest/target/debug/deps/libpyo3-c98e45c2daa36b66.rlib(pyo3-c98e45c2daa36b66.pyo3.b04632a8-cgu.2.rcgu.o): in function `pyo3_ffi::object::Py_None':
/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-ffi-0.16.2/src/object.rs:472: undefined reference to `_Py_NoneStruct'
/usr/bin/ld: /home/user/dytest/target/debug/deps/libpyo3-c98e45c2daa36b66.rlib(pyo3-c98e45c2daa36b66.pyo3.b04632a8-cgu.2.rcgu.o): in function `pyo3::err::PyErr::take':
/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.16.2/src/err/mod.rs:264: undefined reference to `PyErr_Fetch'
[...many, many error messages...]
/usr/bin/ld: /home/user/dytest/target/debug/deps/libpyo3-c98e45c2daa36b66.rlib(pyo3-c98e45c2daa36b66.pyo3.b04632a8-cgu.15.rcgu.o): in function `pyo3::types::string::PyString::to_string_lossy':
/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.16.2/src/types/string.rs:199: undefined reference to `PyUnicode_AsEncodedString'
collect2: error: ld returned 1 exit status
= help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the `-l` flag to specify native libraries to link
= note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)
error: could not compile `dytest` due to previous error
If I replace
pyo3 = { version = "0.16", features = ["extension-module"] }
with
pyo3 = { version = "0.16" }
everything works, but the PyO3 User Guide explicitly states this feature.
Trying this code on a Windows machine, works.
I assume I am missing some kind of dependency, but I can't figure out, what is missing.
It looks like this is a known PyO3 issue. It is mentioned in their FAQ:
Cargo.toml:
[dependencies.pyo3]
version = "0.16.3"
[features]
extension-module = ["pyo3/extension-module"]
default = ["extension-module"]
cargo test fails with linking errors when the extension-module feature is activated. A workaround is to make the extension-module feature optional and running the tests with
cargo test --no-default-features.

Python/C++ extension. On import, I get an undefined symbol error

My setup.py and UserMethods.cpp file are below.
My issue is this: I am trying to create and install a python package using distutils and I am running into some issues.
When I run python3 setup.py install --user there is no issue. It compiles and creates a build/ directory with a file named lib.linux-x86_64-3.6. When I check my .local/lib/python3.6/site-pacages directory, there is a file named UserMethods.cpython-36m-x86_64-linux-gnu.so.
The issue comes when I try to import the package:
$ python3
>>> import UserMethods
which returns the following error:
ImportError: ~/.local/lib/python3.6/site-packages/UserMethods.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN12NA62Analysis4Core18AnalyzerIdentifierD1Ev
I do not know how or where such a symbol would be defined or why it would be created. Does anyone have insight as to where this error is coming from? Thanks in advance.
EDIT:
here is the setup.py file:
from distutils.core import setup, Extension
UM_module = Extension('UserMethods', sources=['UserMethodsModule.cpp'], language='C++',
include_dirs=[ ...many... ],
extra_compile_args=['-std=c++11'],
libraries=['stdc++'],)
setup(name='UserMethods',
version='1.0',
ext_modules=[UM_module],
)
and here is my UserMethods.cpp file:
#include <Python.h>
#define PY_SSIZE_T_CLEAN
#include "UserMethods.hh"
/* OUR FUNCTIONS GO HERE */
static PyObject* UM_test(PyObject *self, PyObject *args){
const char *command;
int sts;
if ( !PyArg_ParseTuple(args, "s", &command) ){
return NULL;
}
sts = system(command);
return PyLong_FromLong(sts);
}
static PyMethodDef UserMethods[] = {
{"system", UM_test, METH_VARARGS, "execute shell command."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef UserMethodsModule = {
PyModuleDef_HEAD_INIT,
"UserMethods",
NULL,
-1,
UserMethods
};
PyMODINIT_FUNC PyInit_UserMethods(void){
return PyModule_Create(&UserMethodsModule);
}
as per #Holt above, this error was caused by not importing the library that contains the type definition in the error.
I had to add the path to the library in my linking step, which I added to the 'extra_link_args' argument in the Extension function call in Setup.py.

Broken code generation for out function parameters

I made my own interface, simplistic version looks like this:
#ifndef _FOO_IDL_
#define _FOO_IDL_
module FOO {
typedef unsigned long Bar;
interface FOOInterface {
void getBar(out FOO::Bar b);
};
};
#endif
After that I made "REDHAWK IDL Project", used that IDL, compiled, installed.
Then I made Redhawk component, added output port and used that interface on it, did code generation. During compilation I got error:
port_impl.h:26:29: error: expected ‘,’ or ‘...’ before ‘&&’ token
void getBar(FOO::Bar&& b);
It looks like code generator adds excessive ampersand. What could I do about it?
Thank you.

Typescript import mechanism in Nodejs

I'm totally stuck right now. Using Nodejs.
Having the following setup:
Compile -target ES5 --module commonjs
/def/mongoose.d.ts:
export = M;
declare module M {
export class Collection {
name:string;
}
}
/model/users.ts:
///<reference path='..\def/mongoose.d.ts' />
export var foo:M.Collection;
Error: /model/users.ts(21,16): error TS2095: Could not find symbol 'M'.
Made it as simple as possible. I tried a lot but did not managed to access the class in the mongoose.d.ts
Instead of using a reference comment, you should import the module:
import M = require('./def/mongoose');
export var foo: M.Collection;
Usually, you would give the .d.ts file the same name (and location) as the .js file so the import statement would also load it at runtime.

Resources