emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Compiling a C++ source block
@ 2022-09-26 19:13 Roger Mason
  2022-09-26 19:56 ` tbanelwebmin
  0 siblings, 1 reply; 6+ messages in thread
From: Roger Mason @ 2022-09-26 19:13 UTC (permalink / raw)
  To: orgmode

Hello,

I wish to compile & run this source block in org-mode:

#+name: gnc_cav
#+header: :includes  <iostream> <ginac.h>
#+header: :libs -L/usr/local/lib -lginac -lcln
#+header: :flags -std=c++17
#+header: :namespaces std GiNaC
# #+header: :var table=
#+header: :var job="vectors" :var vol=113.13115406160385926 :var r=1.0995524816924328312
#+header: :var a=4.916 :var c=5.4054
#+header: :main no
#+begin_src  C++
    ex vol(symbol a, symbol c)
    {
      ex v = a*a * c * sin(60*Pi/180);

      return v;
    }

    ex coa(symbol a, symbol c) {
      ex covera = c/a;

      return covera;
    }

    ex scl(symbol a, symbol c, double ad, double cd) {

      //  symbol v("v");
      // symbol r("r");
      ex v = vol(a,c);

      ex r = coa(a,c);
      numeric three(3);

      //  ex s = power(v.subs(lst{a==ad,c==cd})),1/three)/(power(sin(60*Pi/180),1/three) * power(r.subs(lst{a==ad,c==cd}),1/three));
      ex s = power(v.subs(lst{a==ad,c==cd}),1/three)/(power(sin(60*Pi/180),1/three) * power(r.subs(lst{a==ad,c==cd}),1/three));

      return s;
    }

    ex scl(double vd, double rd) {

      numeric three(3);

      ex s = power(vd,1/three)/(power(sin(60*Pi/180),1/three) * power(rd,1/three));

      return s;
    }

    int main () {
      if ( strncmp(job, "volume", 6) == 0) { // ./v volume a c, calculate volume

	symbol as("as");
	symbol cs("cs");

	ex v = vol(as, cs);
	cout << "Volume = " << v.subs(lst{as == a, cs == c}).evalf() << "\n";

	ex r = coa(as, cs);
	cout << "c/a = " << r.subs(lst{as == a, cs == c}).evalf() << "\n";

	ex a_calc = scl(as, cs, a, c);
	cout << "a_calc = " << a_calc.evalf() << "\n";
	cout << "c_calc = "
	     << a_calc.evalf() * r.subs(lst{as == a, cs == c}).evalf() << "\n";

      } else if ( job,"vectors",5) == 0 ) { // ./v vectors vol c/a

	//double vd = stod(argv[2]);
	//double rd = stod(argv[3]);
	ex a_calc = scl(v,r);
	cout << "a_calc = " << a_calc.evalf() << "\n";
	cout << "c_calc = " << a_calc.evalf() * rd << "\n";

	//double a_scale = a;
      } // vectors
    } // main

#+end_src

When I attempt compilation using 'C-c C-c' I get:

/tmp/babel-eqG2i0/C-src-3zG2ec.cpp:16:1: error: unknown type name 'ex'
ex vol(symbol a, symbol c)

and lots of similar errors.  I infer this is because ginac.h is not
found.  I have tried specifying <ginac/ginac.h> in ':includes' to no
avail.  I have tried specifying the full path to ginac.h:
</usr/local/lib/ginac/ginac.h> and specifying -I/usr/local/lib/ginac/ as
part of ':includes', but the error persists.

Maybe the C++ support is not set up to handle this use case, but perhaps
I am missing something.

Any help will be much appreciated.

Thanks,
Roger


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Compiling a C++ source block
  2022-09-26 19:13 Compiling a C++ source block Roger Mason
@ 2022-09-26 19:56 ` tbanelwebmin
  2022-09-27  8:26   ` Roger Mason
  2022-09-29  2:25   ` Ihor Radchenko
  0 siblings, 2 replies; 6+ messages in thread
From: tbanelwebmin @ 2022-09-26 19:56 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/html, Size: 3947 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Compiling a C++ source block
  2022-09-26 19:56 ` tbanelwebmin
@ 2022-09-27  8:26   ` Roger Mason
  2022-09-29  2:25   ` Ihor Radchenko
  1 sibling, 0 replies; 6+ messages in thread
From: Roger Mason @ 2022-09-27  8:26 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

tbanelwebmin <tbanelwebmin@free.fr> writes:

> You may try:
> #+header: :includes '("<iostream>" "<ginac.h>")
>
> Also, you have this commented line:
> # #+header: :var table= 
> It seems to break the #+header: chain
> Just remove it.
>
> By the way, to figure out future compilation problems, you may click on the error:
> /tmp/babel-eqG2i0/C-src-3zG2ec.cpp:16:1: error: unknown type name 'ex' 
> This will open the source file actually being compiled.
>
> Have fun!

Thank you.  For the record, this works:

#+name: gnc_cav
#+header: :includes  '("<iostream>" "<ginac/ginac.h>")
#+header: :libs -I/usr/local/include -L/usr/local/lib -lginac -lcln
#+header: :flags -std=c++17
#+header: :namespaces std GiNaC
#+header: :var job="vectors" :var vol=113.13115406160385926 :var r=1.0995524816924328312
#+header: :var a=4.916 :var c=5.4054
#+header: :main no

Note the :libs line specifies an include path.  A bit unintuitive, but
it does the job.  Specifying a path in the :includes does not work (and
nothing in the docs suggests that it should).

By the way, 'C-c C-v v' also provides access to the file sent for compilation.

Thanks again,
Roger


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Compiling a C++ source block
  2022-09-26 19:56 ` tbanelwebmin
  2022-09-27  8:26   ` Roger Mason
@ 2022-09-29  2:25   ` Ihor Radchenko
  2022-09-29 20:03     ` tbanelwebmin
  1 sibling, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2022-09-29  2:25 UTC (permalink / raw)
  To: tbanelwebmin; +Cc: emacs-orgmode

tbanelwebmin <tbanelwebmin@free.fr> writes:

> You may try:
> #+header: :includes '("<iostream>" "<ginac.h>")

I notice that
https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-C.html
only has examples of a single library being using in :includes
parameter.

Although the text description does mention list of strings, it would be
nice if there was an example using :includes with multiple libraries
and/or local libraries.

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Compiling a C++ source block
  2022-09-29  2:25   ` Ihor Radchenko
@ 2022-09-29 20:03     ` tbanelwebmin
  2022-11-25  7:45       ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: tbanelwebmin @ 2022-09-29 20:03 UTC (permalink / raw)
  To: Org Mode

[-- Attachment #1: Type: text/html, Size: 1220 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Compiling a C++ source block
  2022-09-29 20:03     ` tbanelwebmin
@ 2022-11-25  7:45       ` Ihor Radchenko
  0 siblings, 0 replies; 6+ messages in thread
From: Ihor Radchenko @ 2022-11-25  7:45 UTC (permalink / raw)
  To: tbanelwebmin; +Cc: Org Mode

tbanelwebmin <tbanelwebmin@free.fr> writes:

> Good point Ihor!
> I will change that

I pushed my version of the change onto master.
https://git.sr.ht/~bzg/worg/commit/7a1e9671

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-11-25  7:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-26 19:13 Compiling a C++ source block Roger Mason
2022-09-26 19:56 ` tbanelwebmin
2022-09-27  8:26   ` Roger Mason
2022-09-29  2:25   ` Ihor Radchenko
2022-09-29 20:03     ` tbanelwebmin
2022-11-25  7:45       ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).