Project „New Search Engine“

C++ - Linux Commands

 

Před spuštěním programu je nutno přidat pathname jeho adresáře do PATH.

C++ - GNU – gcc

installation

sudo apt-get install g++

sudo apt-get install g++4.1

sudo apt-get install g++4.2

sudo apt-get install g++4.3

sudo apt-get install build-essential

which will install gcc, cpp, c++, g++ and a few other i believe (it's a metapackage)

basic library

libstdc++6-4.2-dev

 

Directories

which g++ - shows, in which directory the program is placed

c++, g++ programs are in directory: /usr/bin

g++ libraries are in: /usr/include/c++/4.4

 

Batch Makefile for compilation

http://www.opussoftware.com/tutorial/TutMakefile.htm

Running - compiling

gcc nebo g++

use this one (!):

g++ hello.cpp -o hello

g++ -c hello.cpp -o hello

gcc –c example.c

gcc –c example.cc

compiles into file example.o

 

Compiling with library – correct in Linux (Ubuntu)

g++ example_012.cpp example_library.o -o example_012

 

Compiling with library simple

g++ -c example_012.cpp -o example_012 -L. –lexample


Compiling with library complete

Zmíněné chyby mají různé příčiny. Parametr -c u g++ znamená, že se pouze vytvoří objektový soubor, který ale není spustitelý, při pokusu o jeho spuštění systém vyhodí chybu. Když se pokoušíte provést úplnou kompilaci example_012.cpp, systém nemůže najít knihovnu libexample.so, protože tam je pod jiným názvem (libexample.so.1.0.1), proto potřebujete symbolický odkaz. Korektní posloupnost příkazů je tedy tahle:


Compiling of library
g++ -c -fPIC example_library.cpp -o example_library.o

g++ -shared -Wl,-soname,libexample.so.1 -o libexample.so.1.0.1

Zajištění, aby kompilátor a linker knihovnu našel (v minulém návodu jsem je opomenul, postup jsem předtím nezkoušel):

ln -s libexample.so.1.0.1 libexample.so

ln -s libexample.so.1.0.1 libexample.so.1


Another compiling library

g++ example_012.cpp -o example_012 -L. -lexample


Running program with library
LD_LIBRARY_PATH=. ./example_012

 

Another compiling with library simple

g++ -c example_012.cpp -o example_012 -L. –lexample


Another compiling with library

g++ -c example_012.cpp –o example_012 -L.

-L means, that also libraries in present directiry are ot be included

g++ -c example_012.cpp –o example_012 -L. –lexample_library


Another compiling library

g++ -c -fPIC example_library.cpp -o example_library.o

Toto vytvoří objektový soubor, který podporuje position-independent code.

g++ -shared -Wl,-soname,libexample.so.1 -o libexample.so.1.0.1 example_library.o

Objektový soubor získaný z minulého příkazu skompiluje ve sdílenou knihovnu.


Compiling library – another advices

g++ -c example_library.cpp

it makes example_library.o

libraries, functions

works: stdio.h

fstream.h -> fstream

plus add: using namespace std;

iostream.h -> iostream

plus add: using namespace std;

library

gcc -c -shared -fpic hello.c

anoter library

http://stackoverflow.com/questions/42770/writing-using-c-libraries
The code

r.cc:

#include "t.h"

int main()

{

 f();

 return 0;

}

t.h:

void f();

t.cc:

#include<iostream>

#include "t.h"

void f()

{

 std::cout << "OH HAI. I'M F." << std::endl;

}

But how, how, how?!

~$ g++ -fpic -c t.cc # get t.o

~$ g++ -shared -o t.so t.o # get t.so

~$ export LD_LIBRARY_PATH="." # make sure t.so is found when dynamically linked

~$ g++ r.cc t.so # get an executable

The export step is not needed if you install the shared library somewhere along the global library path.

another library

http://answers.yahoo.com/question/index?qid=20081112125751AApVBrX

make files called mylibrary.h and mylibrary.c

mylibrary.h just has prototype methods with no bodies

eg: void doSomeStuff(int* stuff);

mylibrary.c has all of these functions again but with their bodies

eg: void doSomeStuff(int* stuff){

printf("%i",stuff);

}

you then include the .h file when you want to use it

once you've compiled it once you will get a mylibrary.o, this and the .h file are all you need to use it, the .c is only needed to change it and recompile the .o file

another library

http://answers.yahoo.com/question/index?qid=20081112125751AApVBrX
Under Linux:

use the ar command to create your archive(lib), e.g.,

$ g++ -c test1.c create test1.o

$ g++ -c test2.c create test2.o

Now add the object files to the archive

$ ar cr libmydemo.a test2.o test1.o

Next step is to compile and link it:

$ g++ -o test3 test3.c -L. -lmydemo

Here, "-L." is used to tell the system that when the compiler searches for library files it should also include the current directory. The name of the library is passed using the -l command option, you call the library without the lib prefix and the .a extension..

anoter library

http://answers.yahoo.com/question/index?qid=20081112125751AApVBrX
Aerivium is correct as far as he goes. Obviously the first thing he mentioned -- you want to compile but not link your routines. Why not? Because you'll be linking them in with the other libraries. In fact, in gcc in linux which I use, you compile them with a command line like:

g++ -o myprogram.o -c myprogram.cc and then use ar as in ar mylib.a myprog.o. That is a static library which will be compiled into the final code. For a dynamic library -- sorry, I haven't done that.

Turbo C used to compile to object files (.obj) when I used them. Those are the same as .o files and you can link them into libraries.

If I were you, I would read the documentation on your compiler carefully.

Here are a couple of tutorials using versions of C I don't know much about.

http://www.hlrs.de/organization/tsc/serv...

http://docs.sun.com/source/819-3690/Buil...
http://www.openismus.com/documents/linux...

Another compiling library

gcc -shared -Wl,-soname,your_soname -o library_name file_list library_list

links to libraries

http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html
http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx

Puting libraries together

gcc -fPIC -g -c -Wall a.c

gcc -fPIC -g -c -Wall b.c

gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 a.o b.o -lc

library in Windows

1.

Knihovna se zápisem podprogramů musí mít koncovku .lib.

Tato knihovna se při kliknutí na Překlad nepřeloží, zřejmě se přeloží až při překladu hlavního programu.

2.

V hlavním programu musí být příkaz #include "example_library.lib",

který odkazuje na knihovnu se zápisem podprogramů.

Na rozdíl od toho je v Linuxu příkaz #include "example_library.h",

který odkazuje na knihovnu se zápisem (formální deklarací) hlaviček procedur.

Po překladu to vytvoří hlavní program s obvyklou koncovkou ve Windows .exe, který jde spustit.

Důkaz, že to funguje, viz

http://www.milionovastranka.net/documents/programming/c_plus_plus_windows_library_ok.jpg

Another compilers and means

KDevelop

qt3-designer

qt3 assistant

qt4-designer

qt4 assistant (?)

 

Help

gcc --help man pages