VS2017 throws linker error when used with pybind11 - pybind11

I am using the VS2017 to use the C++ functions inside the python and I am using python 3.7.2. I get the linker error during the compilation. My target extension is .pyd.
My source code is as below
class Pet {
public:
Pet(const std::string &name) : name(name) { }
void setName(const std::string &name_) { name = name_; }
const std::string &getName() const { return name; }
std::string name;
};
namespace py = pybind11;
PYBIND11_MODULE(pyBindLibrary, m) {
py::class_<Pet>(m, "Pet")
.def(py::init<const std::string &>())
.def("setName", &Pet::setName)
.def("getName", &Pet::getName);
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol __imp_PyThread_tss_create cpybind C:\Users\pkumar\source\repos\ConsoleApplicationCPP\cpybind\Source.obj 1
Error LNK2001 unresolved external symbol __imp_PyUnicode_AsEncodedString cpybind C:\Users\pkumar\source\repos\ConsoleApplicationCPP\cpybind\Source.obj 1
Error LNK2001 unresolved external symbol __imp_PyBaseObject_Type cpybind C:\Users\pkumar\source\repos\ConsoleApplicationCPP\cpybind\Source.obj 1
Error LNK2001 unresolved external symbol __imp_PyMem_Calloc cpybind C:\Users\pkumar\source\repos\ConsoleApplicationCPP\cpybind\Source.obj 1
....

Related

How Can I Call Function From Object in dart:ffi (C++) .so file

This is the code inside my .so file.
RustGreetings g = new RustGreetings();
String r ;
try{
**r = g.sayHello("world");**
Log.d("tests","----------------"+r);
} catch (Exception ex){
Log.e("JNI","--------"+ex.getMessage());
}
I want to call this function, that is available inside my precompiled .so file. But when I call this function it shows the error below:
Invalid argument(s): Failed to lookup symbol 'g.sayHello': undefined symbol: g.sayHello
This is my lookup function
typedef HelloWorld = Pointer<Utf8> Function();
typedef HelloWorldDart = Pointer<Utf8> Function();
late HelloWorldDart _helloWorldDart;
final device = Platform.isAndroid
? DynamicLibrary.open(libGreetings)
: DynamicLibrary.process();
_helloWorldDart =
device.lookupFunction<HelloWorld, HelloWorldDart>('g.sayHello');

Running tests with VS Code (without Maven) using the "Test Runner for Java" extension

I'm trying to make a minimal working example of JUnit5 using Visual Studio code without Maven. The reason is that I'm trying to make a quick demo for 1st year students, and I don't have the time to explain Maven (it will be the subject of a later course).
My VS Code workspace has this structure (I'm on Linux so forward slashes are normal) :
The settings.json file contains:
{
"java.project.referencedLibraries": [
"demojunit/junit-platform-console-standalone-1.8.1.jar"
]
}
The DemoJUnit.java contains:
package demojunit;
import java.util.Scanner;
public class DemoJUnit {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int integer = -1;
do {
System.out.print("Enter an integer between 1 and 20 : ");
if (keyboard.hasNextInt())
integer = keyboard.nextInt();
keyboard.nextLine();
} while ((integer < 1) || (integer > 20));
keyboard.close();
long result = computeFactorial(integer);
System.out.println("The factorial " + integer + "! gives " + result);
}
public static long computeFactorial(int number) {
if (number < 1)
return -1;
if (number > 20)
return -2;
long factorial = 1;
for (int i = 2; i <= number; i++) {
factorial = factorial * i;
}
return factorial;
}
}
And the DemoJUnitTest.java contains:
package demojunit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DemoJUnitTest {
#Test
public static void computeFactorial_CleanTest() {
long expected_value = 120;
long real_value = DemoJUnit.computeFactorial(5);
Assertions.assertEquals(expected_value, real_value);
}
#Test public static void computeFactorial_DirtyTest() {
long expected_value = -1;
long real_value = DemoJUnit.computeFactorial(0);
Assertions.assertEquals(expected_value, real_value);
}
}
The settings.json seems to work since the imports and #Test are not marked as errors in VS Code.
As you can see in the folder image, I downloaded the latest JUnit5 jar file, and it works when running from the command line:
However, running from the command line is not convenient, so I installed the "Test Runner for Java" extension.
The problem is that when running tests, something fails and I don't have any error messages, nor any indication what went wrong. I changed the extensions' settings to be verbose but I still get no error messages in the Terminal, the Debug Console, nor anywhere else.
All I get is :
Any idea why the extension fails (and fails to give any indication on why it fails)?

Reading a char array of an SFunction Parameter

I am having trouble reading a string (file name : 'aFile.csv') from SFunction parameter. The error I get doesn't make sense to me
I am using MSVC++(2017).
The error appears to come from ssGetSFcParam inside mxGetString
Here is a snippet of the code that has the proble (in the mdlStart method)
static void mdlStart(SimStruct *S)
{
FILE* fp;
char buffer[255];
char* fileStr;
char* paramStr;
int_T fstatus;
int_T pstatus;
const int_T flen = (int_T)mxGetN((ssGetSFcnParam(S, 0)))*sizeof(char)+1;
const int_T plen = (int_T)mxGetN((ssGetSFcnParam(S, 1)))*sizeof(char)+1;
fileStr = mxMalloc(flen);
paramStr = mxMalloc(plen);
fstatus = mxGetString((ssGetSFcParam(S,0)),fileStr,flen);
pstatus = mxGetString((ssGetSFcParam(S,1)),paramStr,plen);
real_T* Defval = (real_T *)mxGetData(PARAM_DEF2(S));
fp = fopen(fileStr, "r");
// additional code here, but has nothing to do with the error
mxFree(flen);
mxFree(plen);
fclose(fp);
}
Error using mex
Creating library FileReader.lib and object FileReader.exp
FileReader.obj : error LNK2019: unresolved external symbol
ssGetSFcParam referenced in function mdlStart FileReader.mexw64 :
fatal error LNK1120: 1 unresolved externals
The error message says everything: there is no S-function method called ssGetSFcParam. You need to use ssGetSFcnParam, which you have used twice, and then also made a typo twice.

Eclipse will not compile

So. I'm currently writing this code:
https://www.youtube.com/watch?v=pHxtKDENDdE&list=PLFE2CE09D83EE3E28&index=30
Here is the code. This is identical to that in the Youtube video:
import java.util.Random;
class ArrayElementsasCounters {
public static void main(String[] args) {
Random rand = new Random();
int freq[] = new int[7];
for(int roll=1;roll<1000;roll++) {
++freq[1+rand.nextInt(6)];
}
System.out.println("Face\tFrequency");
for(int face=1;face<freq.length;face++); {
System.out.println(face+"\t"+freq[face]);
}
}
}
I get this compile error:
Exception in thread "main" java.lang.Error: Unresolved compilation
problems: face cannot be resolved to a variable face cannot be
resolved to a variable
at ArrayElementsasCounters.main(ArrayElementsasCounters.java:22)
Line 22 is: System.out.println(face+"\t"+freq[face]);
This is working perfectly for the guy in the Youtube video, but not for me. Why?
Remove the semi colon after for statement..
for(int face=1;face<freq.length;face++);
Remove this semicolon in your code..

Diagnosing netlogo extension command error

When I try to execute the first-n-integers extension example:
show noise:first-n-integers 5
I receive an error "ERROR: Expected command. " in the console or in the code tab.
I have mostly copied and pasted the class examples as is, just renaming them and putting them in a different package. I wish the error was a little more descriptive, as I suspect I am making a simple error somewhere.
I'm using 5.0.4 without the JRE and a 1.7.0_45 JRE/JDK on a x64 Windows 7 machine.
My manifest.txt file with fully-qualified class-manager and crlf at the end of the version line--
Manifest-Version: 1.0
Extension-Name: noise
Class-Manager: org.xyz.extensions.NoiseExtension
NetLogo-Extension-API-Version: 5.0 <--there is a crlf here
the jar is in a subfolder with my model file
test/
test.nlogo
noise/
noise.jar
This is my Class-Manager:
package org.xyz.extensions;
import org.nlogo.api.*;
public class NoiseExtension extends DefaultClassManager {
public void load(PrimitiveManager primitiveManager) {
primitiveManager.addPrimitive(
"first-n-integers", new org.xyz.extensions.NoiseGenerator());
}
}
This is the NoiseGenerator file:
package org.xyz.extensions;
import org.nlogo.api.*;
public class NoiseGenerator extends DefaultReporter {
public Syntax getSyntax() {
return Syntax.reporterSyntax(
new int[] {Syntax.NumberType()}, Syntax.ListType());
}
public Object report(Argument args[], Context context) throws ExtensionException {
// create a NetLogo list for the result
LogoListBuilder list = new LogoListBuilder();
int n ;
// use typesafe helper method from
// org.nlogo.api.Argument to access argument
try {
n = args[0].getIntValue();
}
catch(LogoException e) {
throw new ExtensionException( e.getMessage() ) ;
}
if (n < 0) {
// signals a NetLogo runtime error to the modeler
throw new ExtensionException
("input must be positive");
}
// populate the list. note that we use Double objects; NetLogo
// numbers are always Doubles
for (int i = 0; i < n; i++) {
list.add(Double.valueOf(i));
}
return list.toLogoList();
}
}
Thanks for any help.
AJB
This was asked and answered here:
https://groups.google.com/d/msg/netlogo-devel/eIq8drflsc8/7y_Ooh6R0sgJ
The answer was to correct the spelling of getSynax to getSyntax.