I'm trying to use swig to interface with c++, and I'm having problems using a std::string& parameter. This is on Fedora 19 with golang 1.1-2, swig 2.0.10-1, and gcc 4.8.1-1.
I don't know C++. I do know C, and had hoped to wing it. At any rate, _swig_wrap_pinput ends up with a null pointer.
What am I overlooking?
Here's my test case:
st.i
%module st
%{
#include <string>
extern void pinput(std::string& pstring);
%}
%include <std_string.i>
#include <string>
void pinput(std::string& pstring);
st.cxx
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
void pinput(std::string& pstring) {
std::cout<<pstring;
}
stmain.go
package main
import (
"st"
)
func main() {
myLit:="This is a test."
st.Pinput(&myLit)
}
Makefile
SWIGOBJS = st_gc.c st_wrap.cxx st.go
all: st.so st.a
$(SWIGOBJS): st.i
swig -go -c++ -intgosize 64 st.i
st.o: st.cxx
g++ -c -fpic -g -o st.o st.cxx
st_wrap.o: st_wrap.cxx
g++ -c -fpic -g -o st_wrap.o st_wrap.cxx
st.so: st_wrap.o st.o
g++ -shared st_wrap.o st.o -o st.so
st_gc.6: st_gc.c
/usr/lib64/golang/pkg/tool/linux_amd64/6c -I /usr/lib64/golang/pkg/linux_amd64 st_gc.c
st.6: st.go
/usr/lib64/golang/pkg/tool/linux_amd64/6g st.go
st.a: st.6 st_gc.6
go tool pack grc st.a st.6 st_gc.6
test: stmain
stmain: stmain.6
/usr/lib64/golang/pkg/tool/linux_amd64/6l stmain.6
stmain.6: stmain.go
/usr/lib64/golang/pkg/tool/linux_amd64/6g stmain.go
Since the question was originally asked both Go and SWIG have updated. I will post a solution that worked for me (Go 1.3.3, SWIG 3.0.2). As you can see, the difference to the original post is minimal.
The most recommended approach is to use go build. It knows nowadays to look for *.swig and *.swigcxx files, and runs both SWIG and GCC/G++ automatically based on the information found in the swig interface file.
stmain.go
package main
import (
"st" // This will be created by SWIG
)
func main() {
myLit:="This is a test."
st.Pinput(myLit)
}
st/st.h
ifndef ST_H
#define ST_H
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
void pinput(const std::string& pstring);
#endif
st/st.cpp
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
void pinput(const std::string& pstring) {
std::cout << pstring;
std::cout << std::endl; // Without endl the stdout doesn't get flushed for me -> no output
}
st/st.go
package st
// Without any Go file in the directory the go build will fail
st/st.swigcxx
%module st
%include "std_string.i"
%include "st.h"
%{
extern void pinput(const std::string& pstring);
%}
void pinput(const std::string& pstring);
To build & run
// Quick'n'dirty version
# go build stmain.go
# ./stmain
This is a test.
#
Alternatively you can build the same sources manually with 6g/c/l/swig/gotool, for example my simplified version:
Makefile:
all:
make -C st
/usr/local/go/pkg/tool/linux_amd64/6g -I st stmain.go
/usr/local/go/pkg/tool/linux_amd64/6l -o stmain -extldflags -lstdc++ -L st stmain.6
clean:
rm -f *.6 6.out
make clean -C st
test: stmain
st/Makefile:
all: clean
swig -go -intgosize 64 -c++ -soname st.so st.swigcxx
g++ -c st.cpp -o st.o
g++ -c st_wrap.cxx -o st_wrap.o
/usr/local/go/pkg/tool/linux_amd64/6c -I /usr/local/go/pkg/linux_amd64/ -D _64BIT st_gc.c
/usr/local/go/pkg/tool/linux_amd64/6g st.go
go tool pack grc st.a st.6 st_gc.6 st.o st_wrap.o
clean:
rm -f *.6
rm -f *.a
rm -f *.so
rm -f *.cxx
rm -f *.c
rm -f *.o
Related
I have a C++ constructor file (formatting_SQ.cpp) of a header file formatting_SQ.h which I want to link to other constructor files of header files (neat.cpp nnode.cpp link.cpp etc...-> neat.h nnode.h link.h) in order to have formatting_SQ.o.
Then, I want to link my main.cpp file with this formatting_SQ.o file. The problem is: formatting_SQ is embedded with python, and as far as my understanding goes, C++ embedded with Python needs the compiling flag -lpython3.6m on Linux: such flag requires a reference to a main() function, which I don't have in formatting_SQ.cpp because it's a constructor file meant to be an object file.
So I first tried to create object files for each constructor file and then link everything together at once:
g++ -c -O3 -Wall -fPIC -fopenmp -std=c++14 -lstdc++ `python3 -m pybind11 --includes` *.cpp
g++ -o the_executable neat.o nnode.o link.o trait.o gene.o network.o innovation.o organism.o species.o genome.o population.o formatting_SQ.o main.o -fopenmp -O3 -Wall -fPIC `python3 -m pybind11 --includes` -lpython3.6m
Here comes my first question: Are these command right or is there eventually a compilation flag missing ? This gives me a segmentation fault as I try to execute ./the_executable.
Then, I tried to compile formatting_SQ.cpp independently with all other constructor files, but as expected, this doesn't work because there is no reference to main in formatting_SQ.cpp.
g++ -o temp_formatting neat.o nnode.o link.o trait.o gene.o network.o innovation.o organism.o species.o genome.o population.o formatting_SQ.o -fopenmp -O3 -Wall -fPIC `python3 -m pybind11 --includes` -lpython3.6m
So here comes my second question: how could I create a python embedded object file linking formatting_SQ.cpp with all other constructor files without having this undefined reference to main error ?
formatting_SQ.cpp
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
#include <omp.h>
#include "formatting_SQ.h"
#include "neat.h"
#include "network.h"
#include "link.h"
#include "nnode.h"
#include "trait.h"
#include "gene.h"
#include "genome.h"
#include "innovation.h"
#include "organism.h"
#include "species.h"
#include "population.h"
namespace py = pybind11;
py::module compile_data = py::module::import("initialize");
main.cpp
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
#include "formatting_SQ.h"
#include <omp.h>
namespace py = pybind11;
int main(int argc, char** argv){
....
So after some long hours of research I can conclude that the compilation method is correct but BE EXTREMELY CAREFULL with where you declare your import modules from python, because this was the problem for me
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
#include <omp.h>
#include "formatting_SQ.h"
#include "neat.h"
#include "network.h"
#include "link.h"
#include "nnode.h"
#include "trait.h"
#include "gene.h"
#include "genome.h"
#include "innovation.h"
#include "organism.h"
#include "species.h"
#include "population.h"
namespace py = pybind11;
py::module compile_data = py::module::import("initialize"); DON'T DO THIS its wrong !!!
You must declare your modules locally otherwise there be some conflicts in the namespace as the same module may be imported more than once and this causes the segmentation fault.
I'm trying to wrap a C++ library into a Python3 interface using SWIG, and there is a problem I can't quite fix. This library has several namespaces, and I would like to make them modules of the library when wrapped in Python. Assume the following minimal example:
lib_class.hpp lib_class.cpp
lib_ops.hpp lib_ops.cpp
io_ops.hpp io_ops.cpp
The files lib_class define a very small class:
lib_class.hpp
#pragma once
namespace lib {
class dummy {
private:
int a;
public:
dummy();
dummy(int t_a);
~dummy();
void asdf();
};
}
lib_class.cpp
#include "lib_class.hpp"
namespace lib {
dummy::dummy() {}
dummy::dummy(int t_a) : a(t_a) {}
dummy::~dummy() {}
void dummy::asdf() { a = 3; }
}
The files lib_ops.hpp and lib_ops.cpp define a only one function:
lib_ops.hpp:
#pragma once
namespace lib {
void lib_operation();
}
lib_ops.cpp:
#include "lib_ops.hpp"
#include <iostream>
using namespace std;
namespace lib {
void lib_operation() {
cout << "LIBRARY TOP LEVEL" << endl;
}
}
and, finally, the files io_ops.hpp io_ops.cppdefine another function, this time within the namespacelib::io```:
io_ops.hpp
#pragma once
#include "lib_class.hpp"
namespace lib {
namespace io {
void io_operation(dummy& a);
}
}
io_ops.cpp
#include "io_ops.hpp"
#include <iostream>
using namespace std;
namespace lib {
namespace io {
void io_operation(dummy& a) {
cout << "LIBRARY SUBMODULE" << endl;
a.asdf();
}
}
}
I would like to wrap these files into a Python interface so that I can:
import lib
d = lib.dummy(10)
lib.ioop.io_operation(d)
lib.lib_operation()
In other words, I would like the organization of the Python wrapper to be:
lib.dummy # class
lib.lib_operation # function
lib.ioop # submodule
lib.ioop.io_operation # function within submodule
I've written the following *.i files:
lib.i:
%module lib
%import ioop.i
%{
#include "lib_ops.hpp"
#include "lib_class.hpp"
%}
%include "lib_ops.hpp"
%include "lib_class.hpp"
ioop.i:
%module ioop
%{
#include "io_ops.hpp"
using namespace lib;
%}
%include "io_ops.hpp"
This compiles without errors with:
g++ -c -fPIC io_ops.cpp
g++ -c -fPIC lib_ops.cpp
g++ -c -fPIC lib_class.cpp
swig -c++ -python -py3 lib.i
swig -c++ -python -py3 ioop.i
g++ -fPIC -c lib_wrap.cxx -I /usr/include/python3.6
g++ -fPIC -c ioop_wrap.cxx -I /usr/include/python3.6
g++ -fPIC -shared -o _lib.so lib_wrap.o lib_ops.o lib_class.o
g++ -fPIC -shared -o _ioop.so ioop_wrap.o io_ops.o lib_class.o
however, the python script above gives the following error:
Traceback (most recent call last):
File "test.py3", line 5, in <module>
lib.ioop.io_operation(d)
File "/home/lluis/Desktop/example.i/ioop.py", line 66, in io_operation
return _ioop.io_operation(a)
TypeError: in method 'io_operation', argument 1 of type 'dummy &'
Although I managed to "insert" the namespace lib::io into the "main" module lib as a submodule ioop, it looks as though it didn't know anything about the class lib::dummy.
Is it possible to do this? If so, how can I do it?
Thanks to all of you for your time (and sorry for such a lengthy post).
As #Flexo pointed out in one of the comments, the file ioops.i needs an %import. The correct contents of the file are:
%module ioop
%import lib.i
%{
#include "io_ops.hpp"
using namespace lib;
%}
%include "io_ops.hpp"
I have run into a problem while using C and C++ code together. The 'make' command returns "Undefined reference to function" for all functions in SPConfig.c and SPLogger.c, when called from SPImageProc.cpp
#include sections of these relevant files are given below:
SPLogger.c
#include "SPLogger.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
SPConfig.h
#ifndef SPCONFIG_H_
#define SPCONFIG_H_
#include <stdbool.h>
#include <stdio.h>
#include "SPLogger.h"
//Functions definitions
#endif /* SPCONFIG_H_ */
SPConfig.c
#include "SPConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
SPImageProc.h
#ifndef SPIMAGEPROC_H_
#define SPIMAGEPROC_H_
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <vector>
extern "C" {
#include "SPConfig.h"
#include "SPPoint.h"
}
namespace sp {
//Class and function definitions
}
SPImageProc.cpp
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <cstdio>
#include "SPImageProc.h"
extern "C" {
#include "SPLogger.h"
}
Makefile
CC = gcc
CPP = g++
#put all your object files here
OBJS = main.o SPImageProc.o SPPoint.o
#The executabel filename
EXEC = SPCBIR
INCLUDEPATH=/usr/local/lib/opencv-3.1.0/include/
LIBPATH=/usr/local/lib/opencv-3.1.0/lib/
LIBS=-lopencv_xfeatures2d -lopencv_features2d \
-lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
CPP_COMP_FLAG = -std=c++11 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG
C_COMP_FLAG = -std=c99 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG
.PHONY: all clean
all: $(EXEC)
$(EXEC): $(OBJS)
$(CPP) $(OBJS) -L$(LIBPATH) $(LIBS) -o $#
main.o: main.cpp #put dependencies here!
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c++ source file
#use g++ -MM SPImageProc.cpp to see dependencies
SPImageProc.o: SPImageProc.cpp SPImageProc.h SPConfig.h SPPoint.h SPLogger.h
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c source file
#use "gcc -MM SPPoint.c" to see the dependencies
SPPoint.o: SPPoint.c SPPoint.h
$(CC) $(C_COMP_FLAG) -c $*.c
clean:
rm -f $(OBJS) $(EXEC)
Some of Makefile errors:
SPImageProc.o: In function `sp::ImageProc::initFromConfig(sp_config_t*)':
SPImageProc.cpp:(.text+0xc8): undefined reference to `spConfigGetPCADim'
SPImageProc.cpp:(.text+0xf2): undefined reference to `spLoggerPrintError'
SPImageProc.cpp:(.text+0x12c): undefined reference to `spConfigGetNumOfImages'
I have implemented the functions in their respective C and CPP files. I have tried a lot to fix it myself and looked it up on Stack Overflow for similar problems but couldn't find a solution. Please help.
You are not linking SPLogger.o and SPConfig.o
Or even compiling them for that matter.
You need to add make rules for SPLogger.o and SPConfig.o similar to SPImageProc.o and you need to add them to OBJS.
I am trying to use XCppRefl lib to achieve reflections in c++. http://www.extreme.indiana.edu/reflcpp/. I could successfully install this library in linux and run the tests given with the source code of the library.
Here is the code that I have written --
#include <iostream>
using namespace std;
#include <reflcpp/ClassType_tmpl.hpp>
#include <reflcpp/BoundClassType_tmpl.hpp>
#include <reflcpp/Exceptions.hpp>
using namespace reflcpp;
#include "Complex.h"
int main()
{
//ClassType ct = ClassType::getClass( string("Complex") );
////PtrHolder_smptr_t obj = ct.createInstance();
//assert(ct.name() == "B");
Complex x;
int ret;
Complex a;
ClassType c = ClassType::getClass( string("Complex") );
//cout<<"name :: "<<c.name()<<endl;
}
It seems to compile just fine --
$ g++ -g -I /usr/local/include/reflcpp-0.2/ -L /usr/local/include/reflcpp-0.2/ -lreflcpp main.cpp
However when I execute the executable (a.out), I get a core-dump
a.out: Type.cpp:87: static const reflcpp::Type_body* reflcpp::Type_body::getType(const std::string&): Assertion `s_class_name_map' failed.
Aborted (core dumped)
Does anyone has used this lib before? Please help.
you have to link your main.o to libreflcpp.a . after compiling use this:
g++ -p -pg -o"project_name" ./A.o ./A_reflection.o ./main.o /usr/local/lib/libreflcpp.a
How can I use a scanner I've written using Flex as part of a program I'm designing? Specifically, within a c++ class as a method of the class, and from a separate file with just a main method to perform testing.
I don't wish to use the %option c++, but will compile with g++.
To answer the problem of how to test the scanner from a separate file's main I attempted with the following code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
extern "C" {
extern int yylex();
}
extern FILE* yyin;
int main(int argc, char *argv[]) {
if (argc > 1)
yyin = fopen(argv[1], "r");
yylex();
return 0;
}
I compile like so:
flex mylexer.l++
g++ lex.mylexer.C myDriver.C -o myLexer
I get:
undefined reference to yyin
undefined reference to yylex
What is the correct way to compile/setup the driver file?
Thank you for reading and contributing anything!
The simplist example I have is:
mylex.l
%option noyywrap
%%
: return ':';
%%
main.cpp
#include <stdio.h>
#include <iostream>
extern "C"
{
extern int yylex(void);
extern FILE* yyin;
}
int main()
{
yyin = fopen("plop", "r");
std::cout << yylex() << "\n";
}
Then to build:
> flex -o mylex.c mylex.l
> gcc -c mylex.c
> g++ -c main.cpp
> g++ main.o mylex.o
Notice the gcc to compile the mylex.c
If you compile mylex.c with g++ it will be compiled as C++ (not C) and your extern "C" declarations in main would be wrong. Thus you need to compile the mylex.c and main.cpp with different compilers then link them together in separate steps.
Version 2:
Alternatively you can compile the flex code as C++ and remove the extern "C" from main.
main.cpp
#include <stdio.h>
#include <iostream>
extern int yylex(void);
extern FILE* yyin;
int main()
{
yyin = fopen("plop", "r");
std::cout << yylex() << "\n";
}
Now Build like this:
> flex -o mylex.c mylex.l
> g++ -c mylex.c
> g++ -c main.cpp
> g++ main.o mylex.o
Notice this time I used g++ to compile mylex.c (which you could call mylex.cpp now).
Now that you are using the same compiler it can be a one liner:
> flex -o mylex.c mylex.l
> g++ mylex.c main.cpp
You have to link the flex library with your programm, that is, you have to add -lfl to your g++compiler invocation.
flex mylexer.l++
g++ lex.mylexer.C myDriver.C -o myLexer -lfl
You need to include the file generated by flex on your g++ command line -- both yyin and yylex are defined there. If lex.mylexer.C is supposed to be that file, its possible that you're getting a flex error you're ignoring, or otherwise not running flex properly to generate the file -- check it to make sure that it actually contains the flex output and isn't an empty file.
Check out boost Spirit