2013-07-17 19:24:07 +02:00
|
|
|
.. highlight:: c
|
|
|
|
|
2013-09-12 15:06:00 +02:00
|
|
|
Writing you own C++ code with TRIQS
|
|
|
|
------------------------------------
|
2013-10-16 17:36:19 +02:00
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
Basically, this structure means that you have successfully installed TRIQS in
|
|
|
|
:file:`/home/triqs/install` and that you plan to have your new project under
|
|
|
|
:file:`/home/project`. Obviously you can choose any other directory but this
|
|
|
|
structure will be assumed below.
|
|
|
|
|
2013-09-08 16:13:53 +02:00
|
|
|
.. code-block :: bash
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-09-08 16:13:53 +02:00
|
|
|
/home/triqs/install --> TRIQS is installed here
|
|
|
|
/home/project/src --> the sources of my project
|
|
|
|
/home/project/build --> the directory where I will compile my code
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
As we just said, we will start our project in a directory
|
|
|
|
:file:`/home/project`. We will have the sources in :file:`/home/project/src`
|
|
|
|
and later build (compile) the project in :file:`/home/project/build`. Let's
|
|
|
|
start by writing some sources:
|
|
|
|
|
|
|
|
.. code-block :: bash
|
|
|
|
|
|
|
|
$ cd /home
|
|
|
|
$ mkdir project
|
|
|
|
$ cd project
|
|
|
|
$ mkdir src
|
|
|
|
$ cd src
|
|
|
|
|
|
|
|
OK, our project will be just one :file:`main.cpp` file, e.g.::
|
|
|
|
|
2013-09-04 17:05:33 +02:00
|
|
|
#include <triqs/arrays.hpp>
|
|
|
|
using namespace triqs::arrays;
|
2013-07-17 19:24:07 +02:00
|
|
|
int main(){
|
2013-09-04 17:05:33 +02:00
|
|
|
array<double,1> A {1,2,3}, B{10,20,30}, C;
|
|
|
|
C = A+B;
|
2013-07-17 19:24:07 +02:00
|
|
|
std::cout << "C = "<< C << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
As you can see, the code includes headers from TRIQS. Along with
|
|
|
|
:file:`main.cpp` we write a :file:`CMakeLists.txt` file to compile our project.
|
|
|
|
In order to make this easy, there is a file called :file:`TRIQSConfig.cmake`
|
|
|
|
in :file:`/home/triqs/install/share/triqs/cmake`. Including this file in
|
|
|
|
your :file:`CMakeLists.txt` automatically defines a certain number of useful
|
|
|
|
variables, especially the include directories related to the TRIQS headers and
|
|
|
|
the location of the TRIQS libraries. Here is what your simple
|
|
|
|
:file:`CMakeLists.txt` can be:
|
|
|
|
|
2013-09-04 17:05:33 +02:00
|
|
|
.. code-block :: cmake
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-10-16 17:36:19 +02:00
|
|
|
# Append triqs installed files to the cmake load path
|
|
|
|
list(APPEND CMAKE_MODULE_PATH ${TRIQS_PATH}/share/triqs/cmake)
|
2013-09-04 17:05:33 +02:00
|
|
|
|
2013-10-16 17:36:19 +02:00
|
|
|
# Start configuration
|
|
|
|
cmake_minimum_required(VERSION 2.8)
|
|
|
|
project(myproj CXX)
|
|
|
|
set(CMAKE_BUILD_TYPE Release)
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-10-16 17:36:19 +02:00
|
|
|
# Load TRIQS, including all predefined variables from TRIQS installation
|
|
|
|
find_package(TRIQS REQUIRED)
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-10-16 17:36:19 +02:00
|
|
|
# Linking and include info
|
|
|
|
link_libraries(${TRIQS_LIBRARY_ALL})
|
|
|
|
include_directories(${TRIQS_INCLUDE_ALL})
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-10-16 17:36:19 +02:00
|
|
|
# Create executable
|
|
|
|
add_executable(example main.cpp)
|
|
|
|
triqs_set_rpath_for_target(example)
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
We're all set! Everything is ready to compile our project. If we want to build
|
|
|
|
everything in :file:`/home/project/build`, we do as follows:
|
|
|
|
|
|
|
|
.. code-block :: bash
|
|
|
|
|
|
|
|
$ cd /home/project
|
|
|
|
$ mkdir build
|
|
|
|
$ cd build
|
|
|
|
$ cmake -DTRIQS_PATH=/home/triqs/install /home/project/src
|
|
|
|
$ make
|
|
|
|
$ ./example
|
|
|
|
|
|
|
|
That's it! You can modify your sources and then recompile with make. Obviously
|
|
|
|
with bigger projects your :file:`CMakeLists.txt` file will change, but the
|
|
|
|
principle remains the same.
|
|
|
|
|
2013-09-12 15:06:00 +02:00
|
|
|
A simple C++ project, with its tests and documentation
|
2013-09-08 16:13:53 +02:00
|
|
|
------------------------------------------------------
|
|
|
|
|
2013-09-12 15:06:00 +02:00
|
|
|
A mixed C++/Python project
|
2013-09-08 16:13:53 +02:00
|
|
|
------------------------------
|
|
|
|
|