Quantcast

New User and how to use octave in c code ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

New User and how to use octave in c code ?

Progressive
This post was updated on .
Hi,

I'm glad I stumbled over octave as it looks very promising. Just wanted to do my first steps by including octave into my c++ code but it does not work.
Sorry for the very general question, actually I'm facing too many impressions..

By starting octave in shell through "$ octave" I receive the following:

"Octave Critical Error
The settings file /home/ben/.config/octave/qt-settings does not exist and can not be created.
Make sure you have read and write permissions to /home/ben/.config/octave

Octave GUI must be closed now"

Hence, as mentioned on https://www.gnu.org/software/octave/doc/v4.0.3/Standalone-Programs.html#Standalone-Programs by using

#include <iostream>
#include <octave/oct.h>

it in a code I get

"/home/ben/analysis/analyse_tree/Filter/Filter.cxx:2:10: fatal error: 'octave/oct.h' file not found
#include <octave/oct.h>
         ^
Warning in <TInterpreter::TCling::RegisterModule>: Problems in compiling forward declarations for module libGraf3d: '
#line 1 "libGraf3d dictionary forward declarations' payload"
#pragma clang diagnostic ignored "-Wkeyword-compat"
#pragma clang diagnostic ignored "-Wignored-attributes"
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage""

According to my ubuntu software center it is installed successfully in /usr/share/octave
Version is 4.0.2

Can anyone please help ? Thank you very much in advance!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Progressive
This post was updated on .
By following https://unix.stackexchange.com/questions/292721/error-running-octave-in-ubuntu-16-04

I can at least call octave through the shell. But by calling octave via compiling does not work.

Thank you in advance!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Progressive
"The first critical line is #include <octave/oct.h> which makes available most of the definitions necessary for a C++ oct-file. Note that octave/oct.h is a C++ header and cannot be directly #include’ed in a C source file, nor any other language. "
https://www.gnu.org/software/octave/doc/v4.0.1/Getting-Started-with-Oct_002dFiles.html

Is this connected to my problem ? I'm not sure how to handle this..
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Mike Miller-4
On Wed, Apr 26, 2017 at 03:55:13 -0700, Progressive wrote:
> Is this connected to my problem ? I'm not sure how to handle this..

Your earlier message said

| both errors vanished

so it looked to me like your problem was resolved. Is it not? Can you
explain what problem you are still having? Are you using mkoctfile to
build your oct file or program?

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Progressive
Hi,

sorry, it was a confusion. I'm still busy with setting up octave but I'm indeed lost at the moment.
I'm not sure where to start: From my point of view I have already a code and would like to implement octave functions in there.
Many sources tell that it is better to create m-files, other say octave should be used as a standalone makefile, according to " mkoctfile --link-stand-alone standalone.cc -o standalone " which does not compile, too.

I'm really having problems to find a start though I didn't do anything else the last 24h.
I assume I would gain a lot by knowing which way I shall pursue. As I mentioned I already have a code so I thought I just have to include the octave header but obviously this does not work. So either I solve this (somehow) or, as adviced somehwere, I write a m-file and then I have to figure out how to adjust my makefile for octave. At the moment I'm somewhere in between. And I tried a lot of different stuff meanwhile.

I would appreciate any help a lot!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Mike Miller-4
On Wed, Apr 26, 2017 at 09:51:56 -0700, Progressive wrote:
> sorry, it was a confusion. I'm still busy with setting up octave but I'm
> indeed lost at the moment.
> I'm not sure where to start: From my point of view I have already a code and
> would like to implement octave functions in there.
> Many sources tell that it is better to create m-files, other say octave
> should be used as a standalone makefile, according to " mkoctfile
> --link-stand-alone standalone.cc -o standalone " which does not compile,
> too.

We can start there. You should be able to build the example standalone
program provided with the Octave source.

    $ curl -sO https://hg.savannah.gnu.org/hgweb/octave/raw-file/tip/examples/code/standalone.cc 
    $ mkoctfile --link-stand-alone standalone.cc -o standalone
    $ ./standalone
    Hello Octave world!
     11 12
     21 22

That should work out of the box. Does it not work for you? What part
fails and what error messages do you get?

> I'm really having problems to find a start though I didn't do anything else
> the last 24h.
> I assume I would gain a lot by knowing which way I shall pursue. As I
> mentioned I already have a code so I thought I just have to include the
> octave header but obviously this does not work. So either I solve this
> (somehow) or, as adviced somehwere, I write a m-file and then I have to
> figure out how to adjust my makefile for octave. At the moment I'm somewhere
> in between. And I tried a lot of different stuff meanwhile.

It would help if you explained more specifically what you are trying to
do with Octave. Octave can be used in many different ways, it's not
clear to me whether you are trying to use Octave's numerical library,
embed the full interpreter in another program, write a function to call
from Octave, or something else.

If your code is hosted somewhere publicly, providing a pointer to that
would help other help you a great deal.

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Progressive
> We can start there. You should be able to build the example standalone
> program provided with the Octave source.
>
>    $ curl -sO https://hg.savannah.gnu.org/hgweb/octave/raw-file/tip/examples/code/standalone.cc 
>    $ mkoctfile --link-stand-alone standalone.cc -o standalone
>    $ ./standalone
>    Hello Octave world!
>     11 12
>     21 22

Thanks! That worked fine. Now I also know that mkoctile is the makefile itself. I thought I have to enter this line in my existing Makefile.
But is this possible ? Because I need Cern ROOT. I feel free by posting my Makefile in advance:

CXX = g++
CXXFLAGS = -g -Wall `root-config --cflags --libs` -fPIC -O3

LD = g++
LDFLAGS = -g

SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
HEADERS=$(SOURCES:.cpp=.h)
EXECUTABLE=test_octave

CXXFLAGS += $(shell root-config --cflags)
LIBS = $(shell root-config --libs)
NGLIBS = $(shell root-config --glibs)
GLIBS = $(filter-out -lNew, $(NGLIBS))


.SUFFIXES: .cpp

# ==========================================
all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
        $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ $(LIBS)

.cpp.o: $(SOURCES) $(HEADERS)
        $(CXX) -c $(CXXFLAGS) $< -o $@ -I$(PWD)

clean:
        rm -f $(EXECUTABLE) *.o



That's a good question. I have a .cpp file (Filter.cxx) written to use Cern ROOT: (attached)Filter.cxx to analyze data (Filter_trace_array.root) which is generated by another macro. The data is stored as a root file: Filter_traces_array.root.
In Filter.cxx I would like to implement octave. At the moment I only tried to compile it successfully by including the octave header. But after a lot of try and error I'm convinced that this is not possible?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Progressive
By taking http://www.mathias-michel.de/download/howto-octave-c++.pdf 

>To use the very powerful Octave functions in your C/C++ programs include the main octave header file
>
>#include <octave/oct.h>
>
>which is mostly located in \usr\local\include\octave-version-number. Mostly, it will not be necessary to include further header >files, since this file already includes mostof the octave functions.

does not work. My path is different but the correct one does not change anything.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Progressive
I tried now https://www.daniweb.com/programming/software-development/threads/297336/gnu-octave-for-c-how-to-start

and removed in all lines in octave.h the "OCTINTERP_API"

Now I gain different errors :) By using #include <octave.h> I get

> error: ‘Matrix’ was not declared in this scope
>  Matrix a_matrix = Matrix (n, n);
>  ^
>embedded.cpp:10:9: error: expected ‘;’ before ‘a_matrix’
>  Matrix a_matrix = Matrix (n, n);

and by using #include <oct.h> I get

> In function `Array<double>::Array(dim_vector const&)':
> /usr/include/octave-4.0.2/octave/Array.h:176: undefined reference to `dim_vector::safe_numel() const'
> embedded.o: In function `main':
> /home/analysis/Filter/Octave/embedded.cpp:16: undefined reference to `operator<<(std::ostream&, Matrix const&)'
> embedded.o:(.data.rel.ro._ZTV5ArrayIdE[_ZTV5ArrayIdE]+0x20): undefined reference to `Array<double>::resize_fill_value() > const'
> embedded.o:(.data.rel.ro._ZTV6MArrayIdE[_ZTV6MArrayIdE]+0x20): undefined reference to
> `Array<double>::resize_fill_value() const'
> embedded.o:(.data.rel.ro._ZTV7NDArray[_ZTV7NDArray]+0x20): undefined reference to `Array<double>::resize_fill_value() > const'

is this complete nonsense or something to work on ?
embedded.o:(.data.rel.ro._ZTV6Matrix[_ZTV6Matrix]+0x20): undefined reference to `Array<double>::resize_fill_value() const'
collect2: error: ld returned 1 exit status
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Progressive
Is there somewhere an official forum for octave ?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Mike Miller-4
In reply to this post by Progressive
On Wed, Apr 26, 2017 at 10:43:00 -0700, Progressive wrote:

> CXX = g++
> CXXFLAGS = -g -Wall `root-config --cflags --libs` -fPIC -O3
>
> LD = g++
> LDFLAGS = -g
>
> SOURCES=$(wildcard *.cpp)
> OBJECTS=$(SOURCES:.cpp=.o)
> HEADERS=$(SOURCES:.cpp=.h)
> EXECUTABLE=test_octave
>
> CXXFLAGS += $(shell root-config --cflags)
> LIBS = $(shell root-config --libs)
> NGLIBS = $(shell root-config --glibs)
> GLIBS = $(filter-out -lNew, $(NGLIBS))
>
>
> .SUFFIXES: .cpp
>
> # ==========================================
> all: $(SOURCES) $(EXECUTABLE)
>
> $(EXECUTABLE): $(OBJECTS)
> $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ $(LIBS)
>
> .cpp.o: $(SOURCES) $(HEADERS)
> $(CXX) -c $(CXXFLAGS) $< -o $@ -I$(PWD)
>
> clean:
> rm -f $(EXECUTABLE) *.o
>
>
>
> That's a good question. I have a .cpp file (Filter.cxx) written to use Cern
> ROOT: (attached) Filter.cxx
> <http://octave.1599824.n4.nabble.com/file/n4683049/Filter.cxx>   to analyze
> data (Filter_trace_array.root) which is generated by another macro. The data
> is stored as a root file:  Filter_traces_array.root
> <http://octave.1599824.n4.nabble.com/file/n4683049/Filter_traces_array.root>
> .
> In Filter.cxx I would like to implement octave. At the moment I only tried
> to compile it successfully by including the octave header. But after a lot
> of try and error I'm convinced that this is not possible?

The easiest approach to try would be to use mkoctfile as the compiler
and linker in the makefile that you have shown here. Simply change

    CXX = g++

to

    CXX = mkoctfile

and add --link-stand-alone to the $(EXECUTABLE) rule.

The purpose of mkoctfile is to act as a wrapper for the compiler and
linker, adding the appropriate options to build against Octave's
libraries. Most of the same options you would normally pass to gcc can
be passed to mkoctfile.

On Thu, Apr 27, 2017 at 05:56:35 -0700, Progressive wrote:
> Is there somewhere an official forum for octave ?

Yes, you're using it.

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New User and how to use octave in c code ?

Progressive
Ok, thank you very much!

I recently got in contact with some of the developers and they told me how to change the makefile. The new one is here: http://paste.lisp.org/display/345276

I will keep your workaround in mind, maybe I have to use this version instead somewhen!

Thanks a lot again!
Loading...