В началоUnix Toolbox → 22. PROGRAMMING
Gentoo-doc HOME Пред.: 21. SCRIPTINGВ началоУровень выше: Unix ToolboxСлед.: 23. ONLINE HELP

22. 22. PROGRAMMING

22.1. 22.1 C basics

strcpy(newstr,str) /* copy str to newstr */

expr1 ? expr2 : expr3 /* if (expr1) expr2 else expr3 */

x = (y > z) ? y : z; /* if (y > z) x = y; else x = z; */

int a[]={0,1,2}; /* Initialized array (or a[3]={0,1,2}; */

int a[2][3]={{1,2,3},{4,5,6}}; /* Array of array of ints */

int i = 12345; /* Convert in i to char str */

char str[10];

sprintf(str, "%d", i);

22.2. 22.2 C example

A minimal c program simple.c:

#include <stdio.h>

main() {

int number=42;

printf("The answer is %i\n", number);

}

Compile with:

# gcc simple.c -o simple

# ./simple

The answer is 42

22.3. 22.3 C++ basics

*pointer // Object pointed to by pointer

&obj // Address of object obj

obj.x // Member x of class obj (object obj)

pobj->x // Member x of class pointed to by pobj

// (*pobj).x and pobj->x are the same

22.4. 22.4 C++ example

As a slightly more realistic program in C++, let's create a class in its own header (IPv4.h) and

implementation (IPv4.cpp) and create a program which uses the class functionality. The class has a member to convert an IP address in integer format to the known quad format. This is a minimal c++ program with a class and multi-source compile.

IPv4 class

IPv4.h:

#ifndef IPV4_H

#define IPV4_H

#include <string>

namespace GenericUtils { // create a namespace

class IPv4 { // class definition

public:

IPv4();

~IPv4();

std::string IPint_to_IPquad(unsigned long ip);// member interface

};

} //namespace GenericUtils

#endif // IPV4_H

IPv4.cpp:

#include "IPv4.h"

#include <string>

#include <sstream>

using namespace std; // use the namespaces

using namespace GenericUtils;

IPv4::IPv4() {} // default constructor/destructor

IPv4::~IPv4() {}

string IPv4::IPint_to_IPquad(unsigned long ip) { // member implementation

ostringstream ipstr; // use a stringstream

ipstr << ((ip &0xff000000) >> 24) // Bitwise right shift

<< "." << ((ip &0x00ff0000) >> 16)

<< "." << ((ip &0x0000ff00) >> 8)

<< "." << ((ip &0x000000ff));

return ipstr.str();

}

The program simplecpp.cpp

#include "IPv4.h"

#include <iostream>

#include <string>

using namespace std;

int main (int argc, char* argv[]) {

string ipstr; // define variables

unsigned long ipint = 1347861486; // The IP in integer form

GenericUtils::IPv4 iputils; // create an object of the class

ipstr = iputils.IPint_to_IPquad(ipint); // call the class member

cout << ipint << " = " << ipstr << endl; // print the result

return 0;

}

Compile and execute with:

# g++ -c IPv4.cpp simplecpp.cpp # Compile in objects

# g++ IPv4.o simplecpp.o -o simplecpp.exe # Link the objects to final executable

# ./simplecpp.exe

1347861486 = 80.86.187.238

Use ldd to check which libraries are used by the executable and where they are located. This command is also used to check if a shared library is missing or if the executable is static.

# ldd /sbin/ifconfig

22.5. 22.5 Simple Makefile

The corresponding minimal Makefile for the multi-source program is shown below. The lines with

instructions must begin with a tab! The back slash "\" can be used to cut long lines.

CC = g++

CFLAGS = -O

OBJS = IPv4.o simplecpp.o

simplecpp: ${OBJS}

${CC} -o simplecpp ${CFLAGS} ${OBJS}

clean:

rm -f ${TARGET} ${OBJS}

Пред.: 21. SCRIPTINGВ началоУровень выше: Unix ToolboxСлед.: 23. ONLINE HELP
В началоUnix Toolbox → 22. PROGRAMMING