Skip to content

wrapping cpp

test1

1.cpp for sum of 2 numbers. Sourse files in wrapping cpp directory

#include "math.h"
#include <iostream>
using namespace std;
int main()
{
cout << "sum of 1 and 2 equals " << 1+2 << endl;
return 0;
}

Compillation with g++ in terminal:

g++ 1.cpp -o 1

Execution in terminal:

./1

sum of 1 and 2 equals 3

test2

Creating a class that includes a function "sum"

Putting class definition in a separate header file test2.h

namespace test2	
{
	class sum_class
	{
		public:
			int a, b;
		public:
			int sum(int c, int d);	
	};
}

2.cpp with class declaration and call from main()

#include "test2.h"
#include "math.h"
#include <iostream>
using namespace std;
using namespace test2;
// declaration of class
int sum_class::sum(int a, int b)
{
	return a + b;
}

int main()
{
sum_class obj;
cout << "sum of 3 and 6 equals " << obj.sum(3,6) << endl;
return 0;
}

Compillation with g++ in terminal:

g++ 2.cpp -o 2

Execution in terminal:

./2

sum of 3 and 6 equals 9

test3

3.cpp raises to the 2nd power of input number example was taken from https://habr.com/ru/post/168083/

#include "test3.h"
#include "math.h"
#include <iostream>
#include <boost/python.hpp>
using namespace boost::python;
using namespace std;
using namespace test3;

// declaration of class
void power::power2(int a)
{
	cout << a << " power 2 = " << pow(a,2) << endl;	
}

BOOST_PYTHON_MODULE(power_2)
{
    class_<power>( "power" )
        .def( "power2", static_cast< void (power::*)(int) >( &power::power2 ), args( "a" ) )
    ;
}

int main()
{
return 0;
}

header file test3.h

namespace test3	
{
	class power
	{
		public:
			void power2(int a);
	};	
}

compilletion

g++ 3.cpp -I ../../opt/boost_1_72_0 -I /home/vitali79/anaconda3/include/python3.7m/ -o 3

part of errors raised after compillation

undefined reference to `boost::python::detail::init_module(PyModuleDef&, void (*)())'
undefined reference to `_Py_NoneStruct'
3.cpp(.text._ZN5boost6python9converter23expected_pytype_for_argIiE10get_pytypeEv[_ZN5boost6python9converter23expected_pytype_for_argIiE10get_pytypeEv]+0x1b): undefined reference to `boost::python::converter::registry::query(boost::python::type_info)'
...
Edited by Vitali Vistunou
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information