Page 1 of 1

Compiling C++ files

Posted: Wed Jul 06, 2016 3:59 pm
by fotoartist78
When compiling C++ files using the Terminal Window do you add a + sign to gcc vis gcc+. also where should any 'include' libraries be stored within Rpi3.

Re: Compiling C++ files

Posted: Wed Jul 06, 2016 4:00 pm
by MarkHaysHarris777
fotoartist78 wrote:When compiling C++ files using the Terminal Window do you add a + sign to gcc vis gcc+. also where should any 'include' libraries be stored within Rpi3.
try g++

marcus

edit: the include files are .hpp

Re: Compiling C++ files

Posted: Wed Jul 06, 2016 11:38 pm
by swampdog
fotoartist78 wrote:When compiling C++ files using the Terminal Window do you add a + sign to gcc vis gcc+. also where should any 'include' libraries be stored within Rpi3.
$ g++ -v
..and look for "gcc version" (last line) then install the matching documentation, probably 4.9 for jessie..

$ sudo apt-get install gcc-4.9-doc

..then "man" or "info" gcc.

gcc is actually the C++ compiler as well as the C compiler but "g++" puts it into C++ mode for you, which explains the "man gcc" bit above rather than "man g++".

c.hpp..

Code: Select all

void EchoArgs(int,const char*const[]);
c.cpp..

Code: Select all

#include <iostream>	//<system> headers
#include "c.hpp"	//"your" headers

int
main(int argc, char *argv[])
{
 EchoArgs(argc,argv);
 return 0;
}

void
EchoArgs(int na,const char*const av[])
{
 std::cout << "There are " << na << " arguments..\n";
 for	(
	int i	=0;
	i < na;
	i++
	)
	std::cout << av[i] << '\n'
 ;
}
$ g++ -o c -ansi -pedantic -Wall -Werror c.cpp
$ ./c one two three

That's a rough outline (you can in fact override just about anything you choose).

Re: Compiling C++ files

Posted: Thu Jul 07, 2016 9:44 am
by fotoartist78
Many thanks Marcus and 'swampdog' for replies, that has clarified things for me, but neither of you answered the last part of my question "Where should any Include libraries be installed within the Pi3 filing system."

Re: Compiling C++ files

Posted: Thu Jul 07, 2016 2:36 pm
by ghans
You should use package managment to install the "-dev"
(development) versions of your libraries precisely to
avoid having to mess around with include paths manually . e.g.

Code: Select all

sudo apt-get update
sudo apt-get install libudev-dev
To answer your question , the headers
will probably end up in /usr/local/include this way.

ghans

Re: Compiling C++ files

Posted: Thu Jul 07, 2016 2:47 pm
by swampdog
fotoartist78 wrote:Many thanks Marcus and 'swampdog' for replies, that has clarified things for me, but neither of you answered the last part of my question "Where should any Include libraries be installed within the Pi3 filing system."
Think of the system include (header) files as a description of the capabilities of your system and the system library files (libs) as the implementation of those descriptions. With that in mind, it is your decision.

If you're writing an app you'd just create a folder (eg: I use $HOME/usr/src/) and put everything in there, so if I was writing an app called "foo" I'd create $HOME/usr/src/foo/ and place all my source code within that folder. If it's a big app then foo/ might have subdirectories with little subprojects within it. When I installed the app I'd not bother with the headers.

However, if my app had some interest I would install the headers. I might use the "autotools" to do that. That way I could write..

$ cd ~/usr/src/foo
$ mkdir obj && cd obj
$ ../configure --prefix=/usr/local/sd
$ make
$ sudo make install

..and the app would end up installed in /usr/local/sd/{bin,etc,include,info,man,share}. If it became popular others could build it (typically with default --prefix=/usr/local) and it would become part of their system. If the app really took off and got distributed as part of a distro it could be made part of everybody's system by using --prefix=/usr.

This is why you'll find system headers in /usr/include. Libraries are a tad more complex because of 32/64 bit issues in most distros but hopefully I've answered your question. If not, you'll have to rephrase it. ;-)

Re: Compiling C++ files

Posted: Fri Jul 08, 2016 10:12 am
by fotoartist78
Once again 'swampdog' for your suggestions but I am afraid a lot of it was above my head. You see,I have only had my Pi3 for a few weeks so I am really a 'beginner' and am still struggling to understand many of the command line entries. Basically what I want to do try out an Ardiuno sketch which I wrote and which rubs OK on Arduino but on th Pi3. The sketch has an include line :-
#include <IRremote.h> . I have the file 'IRremote' buit I do not know where or how to install it on the Pi3 so that at when compiling the sketch on the Pi3 the system knows where to find the include file. Any help you can give in simple terms would be gtreatly appreciated.

Re: Compiling C++ files

Posted: Fri Jul 08, 2016 11:30 am
by swampdog
fotoartist78 wrote:Once again 'swampdog' for your suggestions but I am afraid a lot of it was above my head. You see,I have only had my Pi3 for a few weeks so I am really a 'beginner' and am still struggling to understand many of the command line entries. Basically what I want to do try out an Ardiuno sketch which I wrote and which rubs OK on Arduino but on th Pi3. The sketch has an include line :-
#include <IRremote.h> . I have the file 'IRremote' buit I do not know where or how to install it on the Pi3 so that at when compiling the sketch on the Pi3 the system knows where to find the include file. Any help you can give in simple terms would be gtreatly appreciated.
Ah. Now the tables are reversed. I know nothing of this. :-|

Fwiw (and I hope others will chip in with specifics), in gcc terms a header using <IRremote.h> (<>) will be looking somewhere down the inbuilt gcc system header paths whereas "IRremote.h" ("") will initially look on the current working directory but for our purposes I now realise this is a red herring.

I *think* what's happening is you're trying to use an Aduino header file. I'm out of my depth. I do not know what "sketch is" I'm afraid.

Re: Compiling C++ files

Posted: Fri Jul 08, 2016 12:20 pm
by ghans
You can't directly use Arduino sketches or source code on
Raspberries ... Arduinos and Raspberries are completely
different. Both platforms support C++ , but the
libraries and peripherials present and the way they are
used is completely incompatible.

Could you show and tell us which Arduino project/sketch/code
are you trying to "transfer" to the Pi ?

ghans