34
1 Tillämpad programmering ID1218 Tillämpad programmering C++ make, configure och arrayer Johan Montelius

Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

1Tillämpad programmering ID1218

Tillämpad programmering

C++ make, configure och arrayerJohan Montelius

Page 2: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

2Tillämpad programmering ID1218

C++ sndcopy.cc

#include <stdio>

#include “sndreader.h”#incldue “sndwrite.h”

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

::

Page 3: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

3Tillämpad programmering ID1218

C++ sndreader.h

#include <iostream>

// Import a C header fileextern "C" {#include <sndfile.h>}

#ifndef __SNDREADER_H__#define __SNDREADER_H__ : :

Page 4: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

4Tillämpad programmering ID1218

C++ sndfile.h

enum { /* Major formats. */SF_FORMAT_WAV = 0x010000,/* Microsoft WAV format (little endian default). */SF_FORMAT_AIFF = 0x020000,/* Apple/SGI AIFF format (big endian). */SF_FORMAT_AU = 0x030000,/* Sun/NeXT AU format (big endian). */SF_FORMAT_RAW = 0x040000,/* RAW PCM data. */

: :

Page 5: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

5Tillämpad programmering ID1218

C++ sndfile.h

struct SF_INFO {sf_count_t frames ; /* Used to be called ...samples. */int samplerate ;int channels ;int format ;int sections ;int seekable ;

};

typedef struct SF_INFO SF_INFO ;

:

Page 6: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

6Tillämpad programmering ID1218

C++ sndfile.h

SNDFILE* sf_open (const char *path,int mode,SF_INFO *sfinfo);

Page 7: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

7Tillämpad programmering ID1218

g++

sndcopy.h

sndfile.h

sndcopy.cc

libsndfile.so

Page 8: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

8Tillämpad programmering ID1218

kompilera och länka

g++ -o ut-fil-c enbart kompilering-E enbart förbehandling-I sökväg för header-L sökväg för bibliotek-l bibliotekkällkodsfil

Page 9: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

9Tillämpad programmering ID1218

libsndfile-1.0.25.tar

>tar -xvf ibsndfile-1.0.25.tar libsndfile-1.0.25/libsndfile-1.0.25/NEWSlibsndfile-1.0.25/M4/

::

Page 10: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

10Tillämpad programmering ID1218

libsndfile-1.0.25

>lsAUTHORS :COPYING INSTALL :README :

Page 11: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

11Tillämpad programmering ID1218

README

LINUX-----Whereever possible, you should use the packages supplied by your Linuxdistribution.

If you really do need to compile from source it should be as easy as:

./configuremakemake install

Page 12: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

12Tillämpad programmering ID1218

INSTALL

The simplest way to compile this package is:

1. `cd' to the directory containing the package's source code and type

`./configure' 2. Type `make' to compile the package. 3. Optionally, type `make check' to run any

self-tests that come with the package. 4. Type `make install' to install the programs

and any data files and documentation.

Page 13: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

13Tillämpad programmering ID1218

INSTALL

By default, `make install' will install the package's files in

`/usr/local/bin', `/usr/local/man',

etc. You can specify an installation prefix other than `/usr/local' by giving `configure' theoption

`--prefix=PATH'.

Page 14: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

14Tillämpad programmering ID1218

/configure

>./configure --prefix=/home/user/tmpchecking build system type... x86_64-unknown-linux-gnuchecking host system type... x86_64-unknown-linux-gnuchecking target system type... x86_64-unknown-linux-gnuchecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /bin/mkdir -pchecking for gawk... gawk

::

Page 15: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

15Tillämpad programmering ID1218

libsndfile-1.0.25

>ls : Makefile :

Page 16: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

16Tillämpad programmering ID1218

make

>make :

:make[2]: Entering directory ../src CC sndfile.lo CC aiff.lo CC au.lo : :

Page 17: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

17Tillämpad programmering ID1218

make install

>make install : :Libraries have been installed in: /home/user/tmp/lib : :

Page 18: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

18Tillämpad programmering ID1218

/home/user/tmp

/home/user/tmp/bininclude/

sndfile.hlib/

libsndfile.so libsndfile-1.0.25 share

Page 19: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

19Tillämpad programmering ID1218

2-1.zip

>wget https:// ...... 2-1.zip : :>unzip 2-1.zip : :>ls 2-1

Makefile sndcopy.cc sndreader.h sndwriter.h wavfiles

Page 20: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

20Tillämpad programmering ID1218

Makefile

CXX = g++CXXFLAGS = -g LDFLAGS = -L/usr/local/lib \

-lsndfile

Page 21: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

21Tillämpad programmering ID1218

Makefile

CXX = g++CXXFLAGS = -g -I/home/user/tmp/includeLDFLAGS = -L/usr/local/lib \

-L/home/user/tmp/lib \ -lsndfile

Page 22: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

22Tillämpad programmering ID1218

Makefile

## Programs to be created...# PROGSRC = sndcopy.ccPROGEXE = $(PROGSRC:%.cc=%.exe)

all: $(PROGEXE)

Page 23: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

23Tillämpad programmering ID1218

Makefile

%.exe: %.cc sndreader.h sndwriter.h$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

g++ -g -I/home/user/tmp/include \sndcopy.cc \-o sndcopy.exe \-L/usr/local/lib \-L/home/user/tmp/lib

Page 24: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

24Tillämpad programmering ID1218

att bygga

• configure– söker efter rätta verktyg– skapar en Makefile som är skräddarsydd

• make – håller rätt på beroende– kompilerar endast det som är nödvändigt

• kompilering– förbehanding (preprocessor)– kompilering– länkning

Page 25: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

25Tillämpad programmering ID1218

primitiva arrayer (C)

int a[4];

int b[] = {1,2,3,4};

double c[100];

long d[10][10];

Page 26: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

26Tillämpad programmering ID1218

char a[]

char a[5];

char b[] = {'H', 'e', 'l', 'l', 'o'};

char c[] = “Hello”;

cout << a << endl;cout << b << endl;cout << c << endl;

Page 27: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

27Tillämpad programmering ID1218

som argument

void foo(int (& a)[10]) {a är en array

}

void bar(int (*a)[10]) {a är en pekare till en array

}

Page 28: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

28Tillämpad programmering ID1218

inte så bra

void zot(int a[]) {a är en pekare till förstaelementet i en array av okänd storlek

}

void grk(int *a) {a är en int pekare

}

void baz(int a[10]) {a är en pekare till förstaelementet i en array av storlek 10

}

Page 29: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

29Tillämpad programmering ID1218

new int[n]

int n = 10;

:int *a = new int a[n];

:zot(a, n);

:

Page 30: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

30Tillämpad programmering ID1218

array<Type, Size>

typedef array<int,10> MyArray;

int main() {

MyArray *arr = new MyArray;

foo(arr);

}

Page 31: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

31Tillämpad programmering ID1218

array<Type, Size>

void foo(MyArray *a) { int size = (a*).size()

MyArray::iterator begin = (a*).begin();MyArray::iterator end = (a*).end();MyArray::iterator p;

for(p = begin, p != end, p++) {cout << *p << endl;

}}

Page 32: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

32Tillämpad programmering ID1218

vector<Type>

typedef vector<int> MyVector;

int main() {

MyVector *vec = new MyVector;

(*vec).push_back(10);(*vec).push_back(11);(*vec).push_back(12);(*vec).push_back(13);

foo(vec);}

Page 33: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

33Tillämpad programmering ID1218

array<Type, Size>

void foo(MyVector *v) { int size = (v*).size()

for(int i = 0; i < size; i++) {cout << (*v)[i] << endl;

}}

Page 34: Tillämpad programmering · 2012. 11. 19. · Tillämpad programmering ID1218 INSTALL The simplest way to compile this package is: 1. `cd' to the directory containing the package's

34Tillämpad programmering ID1218

tumregler

• förstå c arrayer• använd vector