Springen naar inhoud


PI

How To Create C And C++ Programs

Pi C++ RPI

  • Log in a.u.b. om te beantwoorden
Er zijn 1 reacties in dit onderwerp

#1 Vozzie

Vozzie

    Moderator

  • Moderators
  • 1854 berichten
    Laatst bezocht 19 Jul 2014 03:01
Inzender

Geplaatst op 04 January 2013 - 00:08


Create A C program.

  • Open a terminal (i used the pi user).

  • Create and go to a directory for your C programs.

    pi@raspberrypi ~ $ mkdir /home/pi/c
    pi@raspberrypi ~ $ mkdir /home/pi/c/hello
    pi@raspberrypi ~ $ cd /home/pi/c/hello

  • Create and edit a c file.

    pi@raspberrypi ~/c/hello $ nano hello.c

  • Enter this small c program.

    C Code:
    #include <stdio.h>
    
    int main() {
    		printf("hello universe");
    		return 0;
    }
    

  • Exit and save.

  • Compile and build the program. gcc = gnu compiler, -o = output filename

    pi@raspberrypi ~/c/hello $ gcc -o hello hello.c

  • Give execute rights.

    pi@raspberrypi ~/c/hello $ chmod 755 hello

  • Run the program.

    pi@raspberrypi ~/c/hello $ ./hello
    hello universe





Create a C program with headers and modules.

  • Open and edit hello.c

    pi@raspberrypi ~/c/hello $ nano hello.c

  • Change the code to

    C Code:
    #include "hello.h"
    #include <stdio.h>
    
    void hello()
    {
    		printf("hello universe\n");
    }
    

  • Create and edit hello.h

    pi@raspberrypi ~/c/hello $ nano hello.h

  • Enter this code. void hello() is a prototype for the hello function.

    C Code:
    #ifndef __HELLO_H__
    #define __HELLO_H__
    
    void hello();
    
    #endif
    

  • Create and edit main.c

    pi@raspberrypi ~/c/hello $ nano main.c

  • Enter this code.

    C Code:
    #include "hello.h"
    
    int main(){
    		hello();
    		return 0;
    }
    

  • Compile and build the program. gcc = gnu compiler, -o = output filename

    pi@raspberrypi ~/c/hello $ gcc -o hello hello.c main.c

  • Give execute rights

    pi@raspberrypi ~/c/hello $ chmod 755 hello

  • Run the program

    pi@raspberrypi ~/c/hello $ ./hello
    hello universe





Create a C++ program with headers and modules.

  • Create a directory for the cpp files

    pi@raspberrypi ~/c/hello $ mkdir /home/pi/cpp
    pi@raspberrypi ~/c/hello $ mkdir /home/pi/cpp/hello
    pi@raspberrypi ~/c/hello $ cd /home/pi/cpp/hello

  • Create and edit hello.h

    pi@raspberrypi ~/cpp/hello $ nano hello.h

  • Enter this code.

    C Code:
    #ifndef __CHELLO_H__
    #define __CHELLO_H__
    
    class CHello {
    public:
    		CHello();
    		~CHello();
    		void SayHello();
    };
    
    #endif
    

  • Create and edit hello.cpp

    pi@raspberrypi ~/cpp/hello $ nano hello.cpp

  • Enter this code (for VB users, note that the class member methods are defined outside the class defenition. Inside is possible too.)

    C Code:
    #include <iostream>
    #include "hello.h"
    
    // constructor
    CHello::CHello(){
    		std::cout << "constructor\n";
    }
    
    // destructor
    CHello::~CHello(){
    		std::cout << "destructor\n";
    }
    
    // member method
    void CHello::SayHello(){
    		std::cout << "hello universe\n";
    }
    

  • Create and edit main.cpp

    pi@raspberrypi ~/cpp/hello $ nano main.cpp

  • Enter this code

    C Code:
    #include "hello.h"
    
    int main(){
    		CHello * pHello = new CHello();
    		pHello->SayHello();
    		delete pHello;
    		return 0;
    }
    

  • Compile the files and build the program. gcc = gnu compiler, -lstdc++ = standard C++ flag, -o = output filename

    pi@raspberrypi ~/cpp/hello $ gcc -lstdc++ -o hello hello.cpp main.cpp

  • Give hello execute rights

    pi@raspberrypi ~/cpp/hello $ chmod 755 hello

  • Run the program.

    pi@raspberrypi ~/cpp/hello $ ./hello
    constructor
    hello universe
    destructor

Stappen, keuzes en herhaling combineren is programmeren.
Explicit, Strict and don't Infer

#2 RedThread

RedThread

    Beheerder VBIB

  • Beheerder
  • 3443 berichten
    Laatst bezocht 18 Sep 2014 00:08
  • LocatieTongeren,Belgium.
Inzender

Geplaatst op 04 January 2013 - 00:12

Going to try this in the weekend vozzie ! thx

*** If you like youre solution or appriciate the crew's effort, please click on the facebook like or the Google +1 button ***

 

- Any fool can know, the point is to understand -


email : RedThread@vbib.be | MSN : Red_Thread@Hotmail.com

Huisregels - Tutorials - VS 2013 Express - Keybinding Posters - Code Tags gebruiken - LessThenDot - MSDN






Ook met taq PI, Pi, C++, RPI voorzien

0 gebruiker(s) lezen dit onderwerp

0 lid(leden), 0 bezoeker(s), 0 anonieme gebruikers

Inloggen


[PI] Untitled 1

Hosting provided by Combell
Met dank aan Jürgen voor de jarenlange inzet van visualbasic.be (anno dec 2000)
Met dank aan Mike en Ronneke voor de jarenlange inzet van vbib.be (anno dec 2010)
vbib.be - vbnet.be - vbdotnet.be - visualbasic.be