Let's say I have the following class:
class bar
{
public:
bar();
void helloworld(int date)
{ std::cout << "Hello world, the date is: " << date << std::endl; }
};
What would I have to do in the interface file to expose the inlined helloworld() method? I looked into the %inline, but it doesn't seem to do what I'm looking for, which is exposing this as a callable method.
Any help would be appreciated
You can do it like this:
/* bar_module.i */
%module bar_module
%{
#include "bar.h"
%}
%include "bar.h"
/* bar.h */
#include <iostream>
class bar
{
public:
bar();
void helloworld(int date)
{ std::cout << "Hello world, the date is: " << date << std::endl; }
};
/* bar.cc */
#include "bar.h"
bar::bar() {}
# test.py
import bar_module
bar_module.bar().helloworld(47)
# Build commands
swig -o bar_module_wrap.cc -python -c++ bar_module.i
g++ -o bar_module_wrap.os -c -fPIC -I/usr/include/python2.7 bar_module_wrap.cc
g++ -o bar.os -c -fPIC -I/usr/include/python2.7 bar.cc
g++ -o _bar_module.so -shared bar_module_wrap.os bar.os
# Test command
python test.py
# Output
Hello world, the date is: 47
Related
I can't use Rcpp as a 3rd party library for C++.
My sample code is as follows:
main.cpp
#include <Rcpp.h>
#include <iostream>
using namespace Rcpp;
int main() {
std::cout << 1;
Rcpp::ListMatrix a;
system("pause");
return 0;
}
Then I compile using g++
g++ main.cpp -E -o main.i -I "D:/R-4.1.0/include" -I "D:/R-4.1.0/library/Rcpp/include"
g++ -S main.i -o main.s
g++ -c main.s -o main.o
g++ main.o -L"D:\R-4.1.0\library\Rcpp\libs\x64" -l "Rcpp" -L"D:\R-4.1.0\bin\x64" -l "R" -o main.exe
It successfully spawned main.exe.
But when I run main.exe, the program throws an exception.
It shows that these codes in Rcpp cannot run.
Vector( const Dimension& dims) {
Storage::set__( Rf_allocVector( RTYPE, dims.prod() ) ) ;
init() ;
if( dims.size() > 1 ){
AttributeProxyPolicy<Vector>::attr( "dim" ) = dims;
}
}
Then I found that when the constructors of vector in Rcpp all have functions prefixed with Rf, exceptions are thrown.
why this happen?
I try this,and it it didn't throw exception.
#include <Rcpp.h>
#include <iostream>
using namespace Rcpp;
int main() {
std::cout << 1;
Rcpp::S4 a;
system("pause");
return 0;
}
I think it may have something to do with the function prefixed with Rf.
main.cpp
#include <iostream>
int main() {
std::cout << "main()" << std::endl;
foo();
return 0;
}
foo.cpp
#include <iostream>
extern "C" {
void foo() {
std::cout << "bar" << std::endl;
}
}
Compile static library:
$ g++ foo.cpp -static
Error:
undefined reference to `WinMain'
But this compiles:
$ g++ foo.cpp -shared -o foo.lib
Now I have a static library named foo.lib (supposedly).
I try to compile an executable that links to it:
$ g++ -L -lfoo main.cpp -o main.exe
And get this error:
'foo' was not declared in this scope
But foo is declared in the static library that I'm linking with. If the link works, I don't think I need to declare it in main.cpp also. So why isn't the link working?
Update.
I added void foo(); to main.cpp so it doesn't complain that foo needs to be declared.
#include <iostream>
void foo();
int main() {
std::cout << "main()" << std::endl;
foo();
return 0;
}
So I try to compile again and I get this new error:
undefined reference to `foo()'
Why would I need to define foo in main.cpp? It's already defined in foo.cpp which is the static library.
If I have to define foo in main.cpp that defeats the entire purpose of linking to the library foo.lib.
UPDATES
Removing all the extern "C" { ... } lines doesn't make the "foo is undefined" errors go away.
What follows are the magical incantations you seek:
main.cpp
#include <iostream>
extern void foo();
int main() {
std::cout << "main()" << std::endl;
foo();
}
foo.cpp
#include <iostream>
void foo() {
std::cout << "bar" << std::endl;
}
Console commands:
$ g++ -o foo.obj -c foo.cpp
$ ar rcs foo.lib foo.obj
$ g++ main.cpp foo.lib -o main.exe
These spells conjure up the static lib foo with the executable main statically linked to it.
I have implemented in C++ a simple module system (for demonstrative purpose only). Unfortunately the program is not working correctly (it produces not the expected output, it prints nothing out).
Here is my source code:
module.hpp
#ifndef _MODULE_HPP
#define _MODULE_HPP
#include <memory>
/// Interface for modules.
class Module {
public:
virtual void run() = 0;
virtual ~Module(){};
};
/// Symbol of the module construtor function.
const auto MODULE_CONSTRUCTOR_SYMBOL = "create_module";
/// Type of the module constructor function.
using MODULE_CONSTRUCTOR = std::unique_ptr<Module> (*)();
#endif /* _MODULE_HPP */
first.hpp
#ifndef _FIRST_HPP
#define _FIRST_HPP
#include <iostream>
#include "module.hpp"
class First : public Module
{
public:
First();
virtual ~First();
virtual void run();
std::unique_ptr<Module> create_module(void);
};
extern "C"
{
typedef std::unique_ptr<Module> create_first_module_t(void);
std::unique_ptr<Module> create_module(void);
}
#endif /* _FIRST_HPP */
first.cpp
#include "first.hpp"
First::First()
{
}
std::unique_ptr<Module> First::create_module()
{
auto ptr = std::unique_ptr<Module>{};
std::cout << "Creation of first module" << std::endl;
return ptr;
}
First::~First()
{
std::cout << "Destruction of first module" << std::endl;
}
void First::run()
{
std::cout << "Running the first module" << std::endl;
}
second.hpp
#ifndef _SECOND_HPP
#define _SECOND_HPP
#include <iostream>
#include "module.hpp"
class Second : public Module
{
public:
Second();
virtual ~Second();
virtual void run();
std::unique_ptr<Module> create_module(void);
};
extern "C"
{
typedef std::unique_ptr<Module> create_second_module_t(void);
std::unique_ptr<Module> create_module(void);
}
#endif /* _SECOND_HPP */
second.cpp
#include "second.hpp"
Second::Second()
{
}
std::unique_ptr<Module> Second::create_module()
{
auto ptr = std::unique_ptr<Module>{};
std::cout << "Creation of second module" << std::endl;
return ptr;
}
Second::~Second()
{
std::cout << "Destruction of second module" << std::endl;
}
void Second::run()
{
std::cout << "Running the second module" << std::endl;
}
main.cpp
#include <memory>
#include <dlfcn.h>
#include "first.hpp"
#include "second.hpp"
int main(void)
{
if (void* firstModule = dlopen("libFirst", RTLD_NOW))
{
if (create_first_module_t* createFirstModule = (create_first_module_t*)dlsym(firstModule, "create_module"))
{
std::unique_ptr<Module> firstModule(createFirstModule());
firstModule.get()->run();
}
dlclose(firstModule);
}
if (void* secondModule = dlopen("libSecond", RTLD_NOW))
{
if (create_second_module_t* createSecondModule = (create_second_module_t*)dlsym(secondModule, "create_module"))
{
std::unique_ptr<Module> secondModule(createSecondModule());
secondModule.get()->run();
}
dlclose(secondModule);
}
return 0;
}
Here are the instructions for the g++ compiler (on a linux machine):
g++ -Wall -Wextra -shared -fPIC first.cpp -o first.so -ldl
g++ -Wall -Wextra -shared -fPIC second.cpp -o second.so -ldl
g++ -Wall -Wextra -c main.cpp
g++ -o main main.o first.so -ldl
The expected output should be the following:
$ LD_LIBRARY_PATH=. ./main ./first.so
Creation of first module
Running the first module
Destruction of first module
$ LD_LIBRARY_PATH=. ./main ./first.so ./second.so
Creation of first module
Creation of second module
Running the first module
Running the second module
Destruction of first module
Destruction of second module
Something is wrong with my implementation, but I do not know where the mistakes are.
I hope that someone can help me with my problem.
In the beginning I had:
main.cpp
#include "something.h"
#include "util.h"
int main() {
sth::something();
utl::little_thing();
}
somehing.h
#ifndef SOMETHING_H
#define SOMETHING_H
namespace sth {
void something();
}
#endif
somehing.cpp
#include "something.h"
#include <string>
#include <iostream>
namespace sth {
void print_me(std::string txt) {
std::cout << txt << std::endl;
}
void something() {
std::cout << "this is something" << std::endl;
print_me("optional");
}
}
util.h
#ifndef UTIL_H
#define UTIL_H
namespace utl {
void little_thing();
}
#endif
util.cpp
#include "util.h"
#include <iostream>
#include <string>
namespace utl {
void little_thing() {
std::cout << "this is little thing" << std::endl;
}
}
Then I though it will be better to have print_me(std::string txt) out of sth namespace. And I put it on utl, declaration on .h file and definition on .cpp file.
At that point the lazy side of me said - would it be better to have it all in one file as was beofore. And I tried:
util.h
#ifndef UTIL_H
#define UTIL_H
#include <string>
#include <iostream>
namespace utl {
void little_thing();
void print_me(std::string txt) {
std::cout << txt << std::endl;
}
}
#endif
something.cpp
#include "something.h"
#include "util.h"
#include <string>
#include <iostream>
namespace sth {
void something() {
std::cout << "this is something" << std::endl;
utl::print_me("optional");
}
}
So I got:
c++ -std=gnu++14 -g -Wall -O3 -c -o main.o main.cpp
c++ -std=gnu++14 -g -Wall -O3 -c -o util.o util.cpp
c++ -std=gnu++14 -g -Wall -O3 -c -o something.o something.cpp
c++ main.o util.o something.o -o main
duplicate symbol __ZN3utl8print_meENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE in:
main.o
util.o
duplicate symbol __ZN3utl8print_meENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE in:
main.o
something.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
Which makes sense to me, since util.h is included in main.cpp and something.cpp there are duplicate symbols, right?
Question, is it possible to be as lazy to have it all in the header? Or no way, have to split decalration and definition? I don't care (in this case) to hide the implementation in the .cpp, I just want to move it out of sth.
Maybe move the definition of print_me down to util.cpp and only leave a declaration of it in the util.h file.
Else you get a copy of it in every object file, and then the linker gets confused as they all have the same name (symbol).
I have a small doubt in the compilation of a c++ code along with a shared library.
So I have two files main.cpp and sample.cpp.
main.cpp
#include <iostream>
using namespace std;
#include "sample.h"
myStruct obj;
void populateData() {
obj.s = "hello world";
}
myStruct giveData() {
cout << "Inside main: " << obj.s << endl;
return obj;
}
int main() {
populateData();
}
sample.h
#ifndef SAMPLE_H
#define SAMPLE_H
#include <string>
struct myStruct {
std::string s;
void populateData();
};
myStruct giveData();
#endif
sample.cpp
#include "sample.h"
#include <iostream>
#include <boost/python.hpp>
using namespace std;
void myStruct :: populateData() {
cout << giveData().s;
}
BOOST_PYTHON_MODULE(boosts) {
using namespace boost::python;
class_<myStruct>("struct")
.add_property("s", &myStruct::s)
.def("populateData", &myStruct::populateData)
;
}
I compile the program using
g++ -c -fPIC sample.cpp
g++ -c -fPIC main.cpp
g++ -shared -Wl,-soname,boosts.so -o boosts.so sample.o main.o -lpython2.7 -lboost_python
g++ -o main main.o
./main
Now, when I run the main, it populates the string inside the obj. But when I run a python script, that imports the boosts.so, the obj.s is empty.
I am guessing it is because the library boosts.so is not properly linked with the executable main.
How do I fix this?