Hello,

 

I've been working on developing a small library in C++ that uses Project 
Chrono, however I want to be able to use this library with PyChrono. I’ve 
been having some trouble linking everything, and I hope you can help.

 

Here’s a summary of my current issue/what I’ve tried so far… Seeing as 
PyChrono is made with the help of SWIG, I tried to mimic the set up for my 
library: I am using CMake and Visual Studio 2019 to 
configure/generate/build a solution. So far, I have been able to 
successfully generate a Python library for a test function in my header 
file that requires no Project Chrono dependencies. I have done this with 
CMake files in a similar way to how Project Chrono generates PyChrono with 
SWIG. For the next step, I tried generating the python code for a function 
that takes a chrono::ChBody object as an argument. Now that some Project 
Chrono files are required, when I try to build my project, Visual Studio 
gives many errors about being unable to find many of the standard C++ 
libraries (cstdio, string, vector, iostream, etc) and cites where they are 
included in Chrono header files as the offending lines (screenshot below). 

 

Do you have any advice on how to properly link my library/SWIG files to 
Project Chrono/PyChrono? I tried writing out a minimal example below, but 
the full project is available on GitHub here (
https://github.com/zur-quin/HydroChrono/tree/add_python 
<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fzur-quin%2FHydroChrono%2Ftree%2Fadd_python&data=05%7C01%7CZuriah.Quinton%40nrel.gov%7C6a4adb8136e741b4a4a008da6cb873c9%7Ca0f29d7e28cd4f5484427885aee7c080%7C0%7C0%7C637941837158357539%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=kbAEzbGbw5qWqZ1dq9Q2ipUd4k17tix%2Bo7b7fbj7co4%3D&reserved=0>)
 
for your reference.

 

Thanks in advance for any advice you have!

 

Best, 

Zuriah Quinton

Researcher I-Software Engineering | Water Power R&D

 

National Renewable Energy Laboratory (NREL)
15013 Denver West Parkway | Golden, CO 80401

[email protected] | www.nrel.gov

 

 

--------------------------------------------------------------------------------------------------------------------------------------

// header file "hydro_forces.h"

// includes for all required Project Chrono .h files 

#include <vector>

#include "chrono/solver/ChSolverPMINRES.h"

#include "chrono/solver/ChIterativeSolverLS.h"

#include "chrono/timestepper/ChTimestepper.h"

#include "chrono/physics/ChForce.h"

#include "chrono/physics/ChLoadContainer.h"

#include "chrono/physics/ChLoadsBody.h"

#include "chrono/physics/ChSystemNSC.h"

#include "chrono/physics/ChBody.h"

#include "chrono/physics/ChBodyEasy.h"

#include "chrono/fea/ChMeshFileLoader.h"

#include "chrono/assets/ChPointPointDrawing.h"

#include "chrono_irrlicht/ChIrrApp.h"

#include "chrono_irrlicht/ChIrrMeshTools.h"

#include "H5Cpp.h"

 

using namespace chrono;

using namespace chrono::irrlicht;

using namespace chrono::fea;

 

void testFunctionNoChrono();

void testFunctionChrono(std::shared_ptr<chrono::ChBody> b);

--------------------------------------------------------------------------------------------------------------------------------------

// cpp file "hydro_forces.cpp"

#include "hydro_forces.h"

 

void testFunctionChrono(std::shared_ptr<chrono::ChBody> b) {

              std::cout << "this is a test" << std::endl;

}

--------------------------------------------------------------------------------------------------------------------------------------

// swig's .i interface file with more care

// hydro_forces.i - SWIG interface

 

%module hydro_forces

 

// Include other .i configuration files for SWIG. 

// These are divided in many .i files, each per a

// different c++ class, when possible.

 

%include "std_string.i"

%include "std_vector.i"

%include "typemaps.i"

 

// Turn on the exception handling to intercept C++ exceptions

%include "exception.i"

 

%exception {

  try {

    $action

  } catch (const std::exception& e) {

    SWIG_exception(SWIG_RuntimeError, e.what());

  }

}

 

// For optional downcasting of polimorphic objects:, note 
"chrono_downcast.i" is coppied into project file

%include "chrono_downcast.i" 

 

// For supporting shared pointers:

%include <std_shared_ptr.i>

 

// Include C++ headers...

%{

 

#include <vector>

#include "chrono/solver/ChSolverPMINRES.h"

#include "chrono/solver/ChIterativeSolverLS.h"

#include "chrono/timestepper/ChTimestepper.h"

#include "chrono/physics/ChForce.h"

#include "chrono/physics/ChLoadContainer.h"

#include "chrono/physics/ChLoadsBody.h"

#include "chrono/physics/ChSystemNSC.h"

#include "chrono/physics/ChBody.h"

#include "chrono/physics/ChBodyEasy.h"

#include "chrono/fea/ChMeshFileLoader.h"

#include "chrono/assets/ChPointPointDrawing.h"

#include "chrono_irrlicht/ChIrrApp.h"

#include "chrono_irrlicht/ChIrrMeshTools.h"

#include "H5Cpp.h"

 

using namespace chrono;

using namespace chrono::irrlicht;

using namespace chrono::fea;

 

%}

 

%include "../hydro_forces.h"

%include <vector>

%include "chrono/solver/ChSolverPMINRES.h"

%include "chrono/solver/ChIterativeSolverLS.h"

%include "chrono/timestepper/ChTimestepper.h"

%include "chrono/physics/ChForce.h"

%include "chrono/physics/ChLoadContainer.h"

%include "chrono/physics/ChLoadsBody.h"

%include "chrono/physics/ChSystemNSC.h"

%include "chrono/physics/ChBody.h"

%include "chrono/physics/ChBodyEasy.h"

%include "chrono/fea/ChMeshFileLoader.h"

%include "chrono/assets/ChPointPointDrawing.h"

%include "chrono_irrlicht/ChIrrApp.h"

%include "chrono_irrlicht/ChIrrMeshTools.h"

%include "H5Cpp.h"

 

--------------------------------------------------------------------------------------------------------------------------------------

#cmake for swig file (c++ library compiled in separate cmake file that 
calls this one)

 

mark_as_advanced(CLEAR SWIG_EXECUTABLE) 

 

# set package name

set(HYDROPY_PACKAGENAME py_hydrochrono)

 

find_package(PythonLibs)

set(Python_ADDITIONAL_VERSIONS 3.4)

find_package(Python3 REQUIRED)

 

get_filename_component(HYDRO_PYTHONDIR "${PYTHON_EXECUTABLE}" PATH)

set(HYDRO_PYTHONINC "${PYTHON_INCLUDE_DIR}")

set(HYDRO_PYTHONLIB "${PYTHON_LIBRARIES}")

 

set(HYDRO_PYTHONDIR  "${HYDRO_PYTHONDIR}"  PARENT_SCOPE)

set(HYDRO_PYTHONINC  "${HYDRO_PYTHONINC}"  PARENT_SCOPE)

set(HYDRO_PYTHONLIB  "${HYDRO_PYTHONLIB}"  PARENT_SCOPE)

 

include_directories(${HYDRO_PYTHONINC})

 

find_package(SWIG REQUIRED)

include(${SWIG_USE_FILE})

 

SET(CMAKE_SWIG_FLAGS "") 

SET_SOURCE_FILES_PROPERTIES(hydro_forces.i PROPERTIES CPLUSPLUS ON)

SET_SOURCE_FILES_PROPERTIES(hydro_forces.i PROPERTIES SWIG_FLAGS 
"-includeall")

 

# optionally set swig locations here 

set(CMAKE_SWIG_OUTDIR "${PROJECT_BINARY_DIR}/hydrochrono_python")

 

set(HYDROPY hydro_forces)

 

# interface files 

set(HYDROPY_MODULE_FILE

    hydro_forces.i

    )

 

SWIG_ADD_LIBRARY(${HYDROPY} TYPE SHARED LANGUAGE python SOURCES 
${HYDROPY_MODULE_FILE})

target_include_directories(${HYDROPY} PRIVATE ${HYDRO_PYTHONINC})

SWIG_LINK_LIBRARIES(${HYDROPY} ${PYTHON_LIBRARIES} ${LINK_LIBS} HydroChrono)

 

set_target_properties(${SWIG_MODULE_${HYDROPY}_REAL_NAME} PROPERTIES

                      PROJECT_LABEL "HydroChrono_python_${HYDROPY}"

                      OUTPUT_NAME   "${SWIG_MODULE_${HYDROPY}_REAL_NAME}"

                      LIBRARY_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"

                      )

 

set_property(TARGET ${HYDROPY} PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES 
TRUE)

ADD_DEPENDENCIES(${SWIG_MODULE_${HYDROPY}_REAL_NAME} HydroChrono)

 

 

 

-- 
You received this message because you are subscribed to the Google Groups 
"ProjectChrono" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/projectchrono/eb4c705e-4eb2-4ebe-94e8-464ba06c95e6n%40googlegroups.com.

Reply via email to