Оглавление
Programming
cbasics
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);
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
cppbasics
*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
C++ example
As a slightly more realistic program in C++: a class in its own header (IPv4.h) and implementation (IPv4.cpp) and a program which uses the class functionality. The class converts an IP address in integer format to the known quad format.
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. Also used to check if a shared library is missing or if the executable is static.
# ldd /sbin/ifconfig # list dynamic object dependencies
# ar rcs staticlib.a *.o # create static archive
# ar t staticlib.a # print the objects list from the archive
# ar x /usr/lib/libc.a version.o # extract an object file from the archive
# nm version.o # show function members provided by object
makefile
The 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}
Оглавление