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

  • Leden
  • PipPipPipPipPipPipPip
  • 1855 berichten
    Laatst bezocht 12 jul 2017 13:15
Inzender

Geplaatst op 04 januari 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


#2 RedThread

RedThread

    Beheerder VBIB

  • Beheerder
  • 3577 berichten
    Laatst bezocht vandaag, 09:34
  • LocatieTongeren,Belgium.
Inzender

Geplaatst op 04 januari 2013 - 00:12

Going to try this in the weekend vozzie ! thx

*** Vergeet ons geen bezoekje te brengen op facebook ***

 

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


RedThread@vbib.be

 

Huisregels | Tutorials | VS 2017 Community | Keybinding Posters | Code Tags gebruiken | 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

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)
Met dank aan PascalBianca voor de jarenlange inzet van vbib.be (anno dec 2016)